首页>代码>spring mvc+Maven+ZUI可拖拽多个文件上传和下载实例>/ssm Maven Webapp/src/main/java/com/java/controller/FileUploadController.java
package com.java.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/***
 * 文件上传控制类
 * @author swinglife
 *
 */
@Controller
@RequestMapping("/files")
public class FileUploadController {
	//通过Spring的autowired注解获取spring默认配置的request
	@Autowired
	private HttpServletRequest request;
	

	 @RequestMapping("/download/{fileName:.+}")
	 public void download(@PathVariable String fileName,String fileOldName, HttpServletRequest request, HttpServletResponse response) throws Exception {
           BufferedInputStream bis = null;
			BufferedOutputStream bos = null;  
			//获取下载文件露肩       转码方法   .getBytes("iso-8859-1"), "utf-8"
			String downLoadPath = request.getSession().getServletContext().getRealPath("/") + "upload\\"+ new String(fileName); 
			//获取文件的长度  
			long fileLength = new File(downLoadPath).length();  
			//设置文件输出类型  
			response.setCharacterEncoding("utf-8");  
	        response.setContentType("multipart/form-data"); 
			response.setHeader("Content-disposition", "attachment; filename="
			        + URLEncoder.encode(fileOldName, "UTF-8"));
			//设置输出长度
			response.setHeader("Content-Length", String.valueOf(fileLength));  
			//获取输入流  
			bis = new BufferedInputStream(new FileInputStream(downLoadPath));
			//输出流  
			bos = new BufferedOutputStream(response.getOutputStream());  
			byte[] buff = new byte[2048];  
			int bytesRead;  
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))){  
			    bos.write(buff, 0, bytesRead);  
			}
			//关闭流
			bis.close();  
			bos.close();
	} 
	/***
	 * 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping("fileUpload")
	public String fileUpload(@RequestParam("file") MultipartFile file) {
		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				// 文件保存路径
				String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
						+ file.getOriginalFilename();
				// 转存文件
				file.transferTo(new File(filePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 重定向
		return "redirect:/filesUpload.jsp";
	}

	/***
	 * 读取上传文件中得所有文件并返回
	 * 
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView list() {
		String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
		ModelAndView mav = new ModelAndView("list");
		File uploadDest = new File(filePath);
		String[] fileNames = uploadDest.list();
		for (int i = 0; i < fileNames.length; i++) {
			System.out.println(fileNames[i]);
		}
		return mav;
	}
}
最近下载更多
lcqlcl  LV11 2023年10月31日
smart123456  LV1 2023年3月13日
annazhang  LV29 2022年12月1日
yymmdm  LV6 2022年9月3日
a1677596408  LV23 2022年7月1日
王国平  LV7 2022年6月14日
240598911  LV10 2021年11月9日
zql666  LV9 2021年9月28日
小喜熊  LV1 2021年9月8日
我要赚钱  LV6 2021年6月3日
最近浏览更多
hkjhjad 2024年5月4日
暂无贡献等级
微信网友_6798246337744896 2023年12月28日
暂无贡献等级
微信网友_6773548257480704 2023年12月11日
暂无贡献等级
lcqlcl  LV11 2023年10月31日
smart123456  LV1 2023年3月13日
annazhang  LV29 2022年12月1日
h889526h11 2022年11月19日
暂无贡献等级
yymmdm  LV6 2022年9月3日
浅水111 2022年7月4日
暂无贡献等级
a1677596408  LV23 2022年7月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友