-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpload.java
54 lines (53 loc) · 2.3 KB
/
Upload.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@RequestMapping(name = "报告总结附件上传", value = "/summaryUpload")
@ResponseBody
public SysFile summaryUpload(HttpServletRequest request) throws Exception {
String uploadpath = Constant.ASSESSMENT;
String businessId = request.getParameter("businessId");
SysFile sysFileResult = null;
try {
File savePath = new File(sysConfigInfo.getGplotSavePath()+uploadpath);
if (!savePath.exists()) {
savePath.mkdirs();
}
List<SysFile> sysFileList = new ArrayList<>();
MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest)request;
Map<String, MultipartFile> files = Murequest.getFileMap();//得到文件map对象
if(files!=null&&!files.isEmpty()){
//循环获取file数组中得文件
for(MultipartFile file:files.values()){
String fileName = fileUpload(request, file, sysConfigInfo.getGplotSavePath()+uploadpath);//文件上传
String filePath = uploadpath+fileName; //路径+新命名(uuid+后缀名)
SysFile sysFile = new SysFile();
sysFile.setId(Common.getUUID());
sysFile.setBusinessId(businessId);
sysFile.setFileName(file.getOriginalFilename());
sysFile.setFilePath(filePath);
sysFile.setSize(file.getSize());
sysFile.setCreatTime(new Date());
String suffix = file.getOriginalFilename().split("\\.")[file.getOriginalFilename().split("\\.").length-1];
sysFile.setExt(suffix);
sysFileList.add(sysFile);
sysFileResult = sysFile;
}
}
sysFileService.batchInsert(sysFileList);
}catch (Exception e) {
logger.error("【文件上传失败】"+e.getMessage());
}
return sysFileResult;
}
public String fileUpload(HttpServletRequest request, MultipartFile file, String filePath) throws Exception {
String fileName = "";
if (!file.isEmpty()) {
try {
String uuid = Common.getUUID();
String suffix = file.getOriginalFilename().split("\\.")[file.getOriginalFilename().split("\\.").length-1];
String fileSavePath = filePath + "/" + (uuid +"."+suffix);
fileName = (uuid+"."+suffix);
file.transferTo(new File(fileSavePath));
} catch (Exception e) {
throw e;
}
}
return fileName;
}