Springboot实现多文件上传
程序员文章站
2022-06-11 17:06:57
...
yml设置文件上传路径
systmp:
fileDisclaimerPath: /data/yanglao/disclaimer/
核心代码
@Value("${systmp.fileDisclaimerPath}")
private String filePaths;
@PostMapping("/uploading")
@ResponseBody
public JSONObject uploadMore(@RequestParam("file") MultipartFile[] files, Integer basicId, Integer tenantId) {
JSONObject result = new JSONObject();
String originalName = null;
String path = null;
String prefix = null;
Map map = new HashMap();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
if (files != null) {
String filePath;
if (basicId == null) {
filePath = filePaths + "file" + "/" + df.format(new Date()) + File.separator;
} else {
filePath = filePaths + "file" + "/" + basicId + "/" + df.format(new Date()) + File.separator;
}
for (int i = 0; i < files.length; i++) {
SysAttachment sysAttachment = new SysAttachment();
originalName = files[i].getOriginalFilename();
prefix = originalName.substring(originalName.lastIndexOf(".") + 1);
String newFileName = System.currentTimeMillis() + "." + prefix;
path = filePath + newFileName;
path = path.replace("\\", "/");
File tempFile = new File(path);
System.out.println(path);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
try {
files[i].transferTo(tempFile);
sysAttachment.setFileName(newFileName);
sysAttachment.setContentType(prefix);
sysAttachment.setFilePath(path);
//新增附件表
sysAttachmentService.save(sysAttachment);
map.put("url/"+i, "/ams/disclaimer/displayImg/" + sysAttachment.getId());
result.put("data/"+i, sysAttachment.getId());
UmsBasicDisclaimer umsBasicDisclaimer = new UmsBasicDisclaimer();
umsBasicDisclaimer.setAttachmentId(sysAttachment.getId());
umsBasicDisclaimer.setBasicId(basicId);
umsBasicDisclaimer.setTenantId(tenantId);
//新增老人免责声明关联表
umsBasicDisclaimerService.save(umsBasicDisclaimer);
} catch (IOException e) {
result.put("msg", "文件为空");
result.put("flag", false);
}
}
result.put("msg", "文件上传成功");
result.put("flag", true);
result.put("datas", map);
} else {
}
return result;
}
上一篇: 关于开发中的一些问题
下一篇: 项目开发需要整理的兼容文档