UploadLocalController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //package com.backendsys.controller.Upload;
  2. //
  3. //import com.backendsys.utils.CommonUtil;
  4. //import com.backendsys.utils.response.Result;
  5. //import com.backendsys.utils.response.ResultEnum;
  6. //import net.coobird.thumbnailator.Thumbnails;
  7. //import org.springframework.beans.factory.annotation.Value;
  8. //import org.springframework.security.access.prepost.PreAuthorize;
  9. //import org.springframework.web.bind.annotation.PostMapping;
  10. //import org.springframework.web.bind.annotation.RequestParam;
  11. //import org.springframework.web.bind.annotation.RestController;
  12. //import org.springframework.web.multipart.MultipartFile;
  13. //
  14. //import java.io.*;
  15. //import java.text.SimpleDateFormat;
  16. //import java.util.Date;
  17. //import java.util.LinkedHashMap;
  18. //import java.util.Map;
  19. //
  20. ///**
  21. // * 上传 (本地)
  22. // */
  23. //@RestController
  24. //public class UploadLocalController {
  25. //
  26. // @Value("${file.upload.directory}") // 配置保存文件的目录
  27. // private String uploadDirectory;
  28. //
  29. // @Value("${file.upload.url-prefix}") // 指定文件访问的 URL前缀
  30. // private String uploadUrlPrefix;
  31. //
  32. // @Value("${file.upload.max-size}")
  33. // private long maxSize;
  34. //
  35. // @PreAuthorize("@sr.hasPermission(1)")
  36. // @PostMapping("/api/upload")
  37. // public Result uploadLocal(@RequestParam("file") MultipartFile file) {
  38. //
  39. // // 检查上传的文件是否为空
  40. // if (file.isEmpty()) {
  41. // return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能为空");
  42. // }
  43. // // 判断文件大小是否超过
  44. // if (file.getSize() > maxSize) {
  45. // return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能超过 " + maxSize/1024/1024 + " MB,请使用大文件上传功能");
  46. // }
  47. //
  48. // try {
  49. // // 创建保存文件的目录,如果不存在
  50. // File directory = new File(uploadDirectory);
  51. // if (!directory.exists()) { directory.mkdirs(); }
  52. //
  53. // // 获取文件原始名
  54. // String originalFileName = file.getOriginalFilename();
  55. //
  56. // // 生成后缀名
  57. // String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
  58. //
  59. // // 生成新的文件名
  60. // String newFileName = CommonUtil.generateFilename(suffix);
  61. // String newFileNameThumb = newFileName.replaceAll(suffix, "-thumb" + suffix);
  62. //
  63. // // 按日期作为文件目录
  64. // String pathDir = new SimpleDateFormat("yyyyMMdd").format(new Date());
  65. // String dateDir = pathDir + File.separator;
  66. //
  67. // File dateDirectory = new File(uploadDirectory + dateDir);
  68. // if (!dateDirectory.exists()) { dateDirectory.mkdirs(); }
  69. //
  70. //// System.out.println("uploadDirectory = " + uploadDirectory);
  71. //// System.out.println("dateDir = " + dateDir);
  72. //// System.out.println("newFileName = " + newFileName);
  73. //
  74. // // 确定文件保存的路径
  75. // String sourceFilePath = uploadDirectory + dateDir + newFileName;
  76. // String sourceFilePathThumb = uploadDirectory + dateDir + newFileNameThumb;
  77. //
  78. // System.out.println(sourceFilePath);
  79. // System.out.println(sourceFilePathThumb);
  80. //
  81. // File sourceFile = new File(sourceFilePath);
  82. //// File sourceFileThumb = new File(sourceFilePathThumb);
  83. //
  84. // // 保存文件到本地
  85. // file.transferTo(sourceFile);
  86. //
  87. //
  88. // // 创建缩略图
  89. // // .scale(0.5) // 缩放
  90. // // .outputFormat("png") // 类型
  91. // // .sourceRegion(Positions.TOP_RIGHT, 1800, 1800) // 裁剪
  92. // // .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f) // 添加水印
  93. // // .outputQuality(0.2) // 质量
  94. // // 文件批量操作
  95. // // Thumbnails.of(new File("/images/202401/").listFiles()).size(400, 400).toFiles(Rename.PREFIX_DOT_THUMBNAIL);
  96. // Thumbnails.of(sourceFilePath).size(200, 200).toFile(sourceFilePathThumb);
  97. //
  98. //
  99. // // 返回文件的路径 (绝对路径)
  100. // String fileUrl = uploadUrlPrefix + pathDir + "/" + newFileName;
  101. // String fileThumbUrl = uploadUrlPrefix + pathDir + "/" + newFileNameThumb;
  102. //
  103. // Map<String, Object> map = new LinkedHashMap<>();
  104. // map.put("filename", newFileName);
  105. // map.put("url", fileUrl);
  106. // map.put("url_thumb", fileThumbUrl);
  107. // map.put("type", suffix.replace(".", ""));
  108. // map.put("size", sourceFile.length());
  109. //
  110. // // 返回响应
  111. // return Result.success(map);
  112. //
  113. // } catch (IOException e) {
  114. // e.printStackTrace();
  115. // return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "上传失败");
  116. // }
  117. // }
  118. //
  119. //}