FileBaseController.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.hys.app.controller.base;
  2. import cn.hutool.core.collection.ListUtil;
  3. import com.hys.app.framework.exception.ResourceNotFoundException;
  4. import com.hys.app.framework.exception.ServiceException;
  5. import com.hys.app.framework.util.ArrayUtils;
  6. import com.hys.app.framework.util.FileUtil;
  7. import com.hys.app.model.base.dto.FileDTO;
  8. import com.hys.app.model.base.vo.FileVO;
  9. import com.hys.app.model.errorcode.SystemErrorCode;
  10. import com.hys.app.service.base.service.FileManager;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiImplicitParam;
  13. import io.swagger.annotations.ApiOperation;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.mock.web.MockMultipartFile;
  16. import org.springframework.web.bind.annotation.DeleteMapping;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import springfox.documentation.annotations.ApiIgnore;
  22. import javax.imageio.ImageIO;
  23. import java.awt.image.BufferedImage;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. import java.util.List;
  28. import java.util.Locale;
  29. import java.util.UUID;
  30. /**
  31. * 存储方案控制器
  32. *
  33. * @author zh
  34. * @version v7.0.0
  35. * @since v7.0.0
  36. * 2018-03-22 10:54:47
  37. */
  38. @RestController
  39. @RequestMapping("/uploaders")
  40. @Api(description = "上传图片api")
  41. public class FileBaseController {
  42. @Autowired
  43. private FileManager fileManager;
  44. @ApiOperation(value = "文件上传", response = FileVO.class)
  45. @ApiImplicitParam(name = "scene", value = "业务场景", allowableValues = "goods,shop,member,other", required = true, dataType = "String", paramType = "query")
  46. @PostMapping
  47. public FileVO list(MultipartFile file, String scene) throws IOException {
  48. if (file != null && file.getOriginalFilename() != null) {
  49. //文件类型
  50. String contentType = file.getContentType();
  51. //获取文件名称
  52. String ext = contentType.substring(contentType.lastIndexOf("/") + 1, contentType.length());
  53. //如果是图片类型 则验证不允许上传超过2M
  54. if (FileUtil.isAllowUpImg(ext)) {
  55. if (!FileUtil.checkFileSize(file.getSize(), 5, "M")) {
  56. throw new ServiceException(SystemErrorCode.E901.code(), "图片超过上传限制大小,请上传5M以内图片");
  57. }
  58. }
  59. if (!FileUtil.isAllowUpFile(ext)) {
  60. throw new ServiceException(SystemErrorCode.E901.code(), "不允许上传的文件格式,请上传gif,jpg,png,jpeg,mp4,pdf,mov格式文件。");
  61. }
  62. FileDTO input = new FileDTO();
  63. input.setName(file.getOriginalFilename());
  64. input.setStream(file.getInputStream());
  65. input.setExt(ext);
  66. return this.fileManager.upload(input, scene);
  67. } else {
  68. throw new ResourceNotFoundException("没有文件");
  69. }
  70. }
  71. @ApiOperation(value = "文件删除")
  72. @ApiImplicitParam(name = "file_path", value = "文件路径", required = true, dataType = "String", paramType = "query")
  73. @DeleteMapping
  74. public String delete(@ApiIgnore String filePath) {
  75. this.fileManager.deleteFile(filePath);
  76. return null;
  77. }
  78. @ApiOperation(value = "文件上传", response = FileVO.class)
  79. @ApiImplicitParam(name = "scene", value = "业务场景", allowableValues = "goods,shop,member,other", required = true, dataType = "String", paramType = "query")
  80. @PostMapping("uploadSmall")
  81. public FileVO uploadSmall(MultipartFile file, String scene) throws IOException {
  82. if (file != null && file.getOriginalFilename() != null) {
  83. //文件类型
  84. String contentType = file.getContentType();
  85. //获取文件名称
  86. String ext = contentType.substring(contentType.lastIndexOf("/") + 1, contentType.length());
  87. String jpg = ext.substring(contentType.lastIndexOf(".") + 1, ext.length());
  88. //如果是图片类型 则验证不允许上传超过2M
  89. if (FileUtil.isAllowUpImg(ext)) {
  90. if (!FileUtil.checkFileSize(file.getSize(), 2, "M")) {
  91. throw new ServiceException(SystemErrorCode.E901.code(), "图片超过上传限制大小,请上传2M以内图片");
  92. }
  93. }
  94. if (!FileUtil.isAllowUpFile(ext)) {
  95. throw new ServiceException(SystemErrorCode.E901.code(), "不允许上传的文件格式,请上传gif,jpg,png,jpeg,mp4,pdf,mov格式文件。");
  96. }
  97. String fileName = file.getOriginalFilename();
  98. String newName = UUID.randomUUID().toString() + fileName.substring(fileName.indexOf("."));
  99. // 获取当前操作系统
  100. String osName = System.getProperties().get("os.name").toString().toLowerCase(Locale.ROOT);
  101. String path = "";
  102. if (osName.startsWith("win")) {
  103. path = "D:\\Test\\";
  104. } else {
  105. path = "/mnt/test";
  106. }
  107. File saveFile = new File(path + newName);
  108. if (!saveFile.getParentFile().exists()) {
  109. saveFile.getParentFile().mkdirs();
  110. }
  111. file.transferTo(saveFile);
  112. String path1 = saveFile.getPath();
  113. File inputFile = new File(path1);
  114. BufferedImage inputImage = ImageIO.read(inputFile);
  115. // 创建一个新的缓冲图片
  116. BufferedImage outputImage = new BufferedImage(80, 80, inputImage.getType());
  117. // 绘制并缩放原图片到新图片中
  118. outputImage.getGraphics().drawImage(inputImage.getScaledInstance(80, 80, BufferedImage.SCALE_SMOOTH), 0, 0, null);
  119. // 写入到输出文件
  120. String newNameSmall ="small"+ newName ;
  121. File outputFile = new File(path + newNameSmall);
  122. ImageIO.write(outputImage, jpg, outputFile); // 根据原图片格式选择"jpg", "png"等
  123. File inputFile2 = new File(path + newNameSmall);
  124. FileInputStream input2 = new FileInputStream(inputFile2);
  125. MultipartFile multipartFile2 = new MockMultipartFile("file", newName, "text/plain", input2);
  126. FileDTO fileDTO2 = new FileDTO();
  127. fileDTO2.setName(multipartFile2.getOriginalFilename());
  128. fileDTO2.setStream(multipartFile2.getInputStream());
  129. fileDTO2.setExt(ext);
  130. FileVO upload2 = this.fileManager.upload(fileDTO2, scene);
  131. upload2.setType("small");
  132. inputFile2.delete();
  133. inputFile.delete();
  134. return upload2;
  135. } else {
  136. throw new ResourceNotFoundException("没有文件");
  137. }
  138. }
  139. }