<a href="fileupload.jsp">文件上传</a><br/>
<body>
<h2>文件上传</h2>
<center>
<form action="file/upload1" method="post" id="itmesForm" enctype="multipart/form-data" >
<input type="file" name="file" /><br/>
<input type="submit" value="提交"/><br/>
</form>
</center>
<center>
<form action="file/upload2" method="post" id="itmesForm" enctype="multipart/form-data" >
<input type="file" name="files" multiple="multiple" /><br/>
<input type="submit" value="提交"/><br/>
</form>
</center>
</body>
// 单文件上传
@RequestMapping("/upload1")
public String fileupload(@RequestParam(value = "file",required = false) MultipartFile file,
HttpServletRequest request, Model model) throws IOException {
System.out.println("进入upload1");
// System.out.println(file.getClass().getName());
//获取文件名
String fileName = file.getOriginalFilename();
// 通过UUID生成随机字符串
fileName= UUID.randomUUID().toString().substring(0,10)+fileName.substring(fileName.lastIndexOf("."),fileName.length());
//获取 服务器存放文件的 物理路径
String path = request.getSession().getServletContext().getRealPath("upload");
System.out.println(path);
// 通过日期生成文件夹
Date date = new Date();
// 创建文件夹
File target = new File(path+new SimpleDateFormat("/yyyy/MM/dd/HH/mm").format(date),fileName);
if (!target.exists()){
// 可以同时创建多个文件夹
target.mkdirs();
}
// 将内容写入到 target中
file.transferTo(target);
return "success";
}
// 多文件上传
@RequestMapping("/upload2")
public String fileupload2(@RequestParam(value = "files",required = false) MultipartFile[] files,
HttpServletRequest request, Model model) throws IOException {
for(MultipartFile file:files){
//获取文件名获取
String fileName = file.getOriginalFilename();
// 通过UUID生成随机字符串
fileName=UUID.randomUUID().toString().substring(0,10)+file.getOriginalFilename().substring(fileName.lastIndexOf('.'),fileName.length());
//获取 服务器存放文件的 物理路径
String path = request.getSession().getServletContext().getRealPath("upload");
Date date = new Date();
path+=new SimpleDateFormat("/yyyy/MM/dd").format(date);
// 创建文件夹
File target = new File(path,fileName);
if (!target.exists()){
// 可以同时创建多个文件夹
target.mkdirs();
}
// 将内容写入到 target中
file.transferTo(target);
}
return "success";
}
<a href="file/filedownload?fileName=甲.lst">文件下载</a><br/>
// 二进制流文件下载
// 文件下载
@RequestMapping(value = "/filedownload")
public ResponseEntity<byte[]> fileDownload(HttpServletRequest request,
@RequestParam("fileName") String fileName, ModelMap modelMap) throws IOException {
System.out.println("******");
// 文件下载路径
String path = request.getServletContext().getRealPath("/upload/");
// File.separator 分隔符 兼容 / // \ \\
File file = new File(path+File.separator+fileName);
设置响应头
HttpHeaders headers = new HttpHeaders();
// 下载显示的文件名字,解决中文乱码
String downloadFileName= new String(fileName.getBytes(),"ISO-8859-1");
// 通知浏览器 以下载方式(attachment)打开文件
headers.setContentDispositionFormData("attachment",downloadFileName);
// 以二进制流数据的形式下载返回文件数据
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// 使用SpringMVC 框架的 ResponseEntity对象 封装返回下载数据
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}