1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@RequestMapping("upload")
@ResponseBody
public Map<String,Object> upload03(MultipartFile mf) throws IOException {
System.out.println(mf);
System.out.println(mf.getContentType());//文件的类型
System.out.println(mf.getName());//表单的name属性值
System.out.println(mf.getOriginalFilename());//文件名
System.out.println(mf.getSize());//文件大小
System.out.println(mf.getInputStream());//文件流
//文件上传的父目录
String parentPath=AppFileUtils.PATH;
//得到当前日期作为文件夹名称
String dirName=RandomUtils.getCurrentDateForString();
//构造文件夹对象
File dirFile=new File(parentPath,dirName);
if(!dirFile.exists()) {
dirFile.mkdirs();//创建文件夹
}
//得到文件原名
String oldName=mf.getOriginalFilename();
//根据文件原名得到新名
String newName=RandomUtils.createFileNameUseTime(oldName);
File dest=new File(dirFile,newName);
mf.transferTo(dest);

Map<String,Object> map=new HashMap<>();
map.put("code", 0);
map.put("msg", "");
Map<String,Object> data=new HashMap<>();
data.put("src", "file/downloadFile.action?path="+dirName+"/"+newName);
map.put("data", data);
return map;
}

1
2
3
4
5
6
7
8
/**
* 下载的方法
*/
@RequestMapping("downloadFile")
public ResponseEntity<Object> downloadFile(String path,HttpServletResponse response){
//3,拿到文件的老名字
return AppFileUtils.downloadFile(response, path, "");
}