核查系统api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package com.iflytop.nuclear.controller;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.iflytop.nuclear.service.UploadService;
  4. import com.iflytop.nuclear.utils.ResponseData;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.access.prepost.PreAuthorize;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.*;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. import java.util.HashSet;
  16. import java.util.Set;
  17. /**
  18. * @author cool
  19. * @date 2023/6/27 09:53
  20. * @desc 上传接口
  21. */
  22. @Slf4j
  23. @RestController
  24. @CrossOrigin
  25. @PreAuthorize("hasRole('ADMIN')")
  26. public class UploadController {
  27. @Autowired
  28. UploadService uploadService;
  29. @PostMapping("/upload/{id}")
  30. public ResponseData uploadToLocal(MultipartFile file, @PathVariable(name = "id") int taskId) throws IOException {
  31. // 对当前文件中内容格式进行校验
  32. boolean isValid = uploadService.verifyFileFormat(file);
  33. if (!isValid) {
  34. return ResponseData.fail("上传文件内容未按照标准版式排版");
  35. }
  36. boolean isSuccess = uploadService.saveFileContentToDB(file, taskId);
  37. // 获取文件原本的名字
  38. String originName = file.getOriginalFilename();
  39. // 判断文件是否是pdf文件
  40. Set<String> set = new HashSet<>();
  41. set.add(".xlsx");
  42. // 取出文件的后缀
  43. int count = 0;
  44. for(int i = 0; i < originName.length(); i++){
  45. if(originName.charAt(i) == '.'){
  46. count = i;
  47. break;
  48. }
  49. }
  50. String endName = originName.substring(count); //取出文件类型
  51. String fileType = originName.substring(count + 1); //文件类型
  52. if(!set.contains(endName)){
  53. return ResponseData.fail("上传的文件类型错误,只能上传xlsx类型的文件");
  54. }
  55. // 创建保存路径 后期使用保存在数据库中的id进行取名
  56. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  57. String format = sdf.format(new Date());
  58. String savePath = System.getProperty("user.dir") + "/" + "uploadfiles" + "/" + fileType + "/" + format;
  59. // String savePath = "/home/zwsd/" + "uploadfiles" + "/" + fileType + "/" + format;
  60. // 保存文件的文件夹
  61. File folder = new File(savePath);
  62. // 判断路径是否存在,不存在则自动创建
  63. if(!folder.exists()){
  64. folder.mkdirs();
  65. }
  66. String saveName = originName.substring(0, count) + "-" + taskId + ".xlsx";
  67. try {
  68. file.transferTo(new File(folder,saveName));
  69. String filePath = savePath + "\\" + saveName;
  70. JSONObject res = new JSONObject();
  71. res.put("path", filePath);
  72. return ResponseData.success(res);
  73. } catch (IOException e){
  74. return ResponseData.fail(e.getMessage());
  75. }
  76. }
  77. }