SpringBoot + Axios

1、文件上传

  1. <form th:action="url" method="post" enctype="multipart/form-data">
  2. <input type="file" name="fileInformation"/>
  3. <input type="submit" value="上传文件"/>
  4. </form>
  1. /**
  2. * 上传文件
  3. * @return
  4. */
  5. @PostMapping("upload")
  6. public void uploadFile(MultipartFile fileInformation,HttpSession session) throws IOException {
  7. // 获取用户的 id
  8. User user = (User) session.getAttribute("user");
  9. // 获取文件的原始名称
  10. String oldFileName = fileInformation.getOriginalFilename();
  11. // 获取文件后缀
  12. String file_ext = "." + FilenameUtils.getExtension(fileInformation.getOriginalFilename());
  13. // 生成新的文件名称
  14. String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+oldFileName;
  15. // 文件大小
  16. long size = fileInformation.getSize();
  17. // 文件类型
  18. String type = fileInformation.getContentType();
  19. // 保存文件的路径
  20. String upload_path = ResourceUtils.getURL("classpath:").getPath() + "static/save";
  21. // 创建日期文件夹
  22. String dateFormat = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  23. String dateDirPath = upload_path + "/" + dateFormat;
  24. File dateDir = new File(dateDirPath);
  25. if(!dateDir.exists()){
  26. dateDir.mkdirs();
  27. }
  28. // 处理文件上传
  29. fileInformation.transferTo(new File(dateDir,newFileName));
  30. System.out.println("success");
  31. }

2、获取数据

  1. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  2. // 得到文件中的数据
  3. axios
  4. .get("http://localhost:8081/files/axios/getDataFromFile", {
  5. params: {
  6. name: "filename",
  7. ext : "ext" // 文件后缀
  8. },
  9. })
  10. .then((res) => {
  11. console.log("数据是:", res.data);
  12. })
  13. .catch((e) => {
  14. console.log("获取数据失败");
  15. });
  16. }
  17. // 或者
  18. axios({
  19. method: "post",
  20. url: "http://localhost:8081/files/axios/getDataFromFile",
  21. // changeOrigin: true,
  22. // responseType: "json",
  23. data: { // Controller中用 @RequestBody 接收 data
  24. name: "filename",
  25. ext : "ext" // 文件后缀
  26. },
  27. })
  28. .then(function (res) {
  29. console.log("数据是:" + res.data);
  30. })
  1. @GetMapping("getDataFromFile")
  2. public void getDataFromFile(@RequestParam String name, @RequestParam String ext,HttpServletResponse response) throws IOException {
  3. // 获取文件路径
  4. String filePath = ResourceUtils.getURL("classpath:").getPath() + "static/save/" + name + "." + ext;
  5. // 获取文件输入流
  6. FileInputStream is = new FileInputStream(new File(filePath));
  7. // attachment --> 文件下载 inline --> 在线打开
  8. // response.setHeader("content-disposition","attachment;fileName"+ URLEncoder.encode("application","UTF-8"));
  9. // 获得相应输出流
  10. ServletOutputStream os = response.getOutputStream();
  11. // 文件拷贝
  12. IOUtils.copy(is,os);
  13. IOUtils.closeQuietly(is);
  14. IOUtils.closeQuietly(os);
  15. System.out.println("success");
  16. }
  1. <!-- 需要如下依赖-->
  2. <dependency>
  3. <groupId>commons-fileupload</groupId>
  4. <artifactId>commons-fileupload</artifactId>
  5. <version>1.4</version>
  6. </dependency>