服务端接口
@RestController
public class UploadController {
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
@PostMapping("/upload")
//多文件上传 @RequestPart("files")MultipartFile[] files
public Map<String,Object> fileupload(MultipartFile file, HttpServletRequest req) {
Map<String, Object> result = new HashMap<>();
String format = sdf.format(new Date());
String realPath = req.getServletContext().getRealPath("/") + format;
File folder = new File(realPath);
if (!folder.exists()) {
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
try {
//保存到文件服务器,oSS服务器
file.transferTo(new File(folder, newName));//file.transferTo(new File("D:\\"+oldName));
String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + format + newName;
result.put("status", "OK");
result.put("name", oldName);
result.put("url", url);
} catch (IOException e) {
result.put("status", "ERROR");
result.put("msg", e.getMessage());
}
return result;
}
}
##接口测试
Vue前端
- Vue模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
</script>
</body>
</html>
参考链接
文件上传实现详细描述
Java 将 Word 文档转换为 PDF 的完美工具
使用工具:https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-JAVA.html(免费版)
参考文章