hfxc226 2 years ago
parent
commit
1572073023

+ 4 - 1
platform-common/src/main/java/com/platform/common/constant/CommonConstants.java

@@ -131,7 +131,10 @@ public interface CommonConstants {
      * 文件获取请求地址
      */
     String RESOURCE_PREFIX = "/files";
-
+    /**
+     * 备件获取请求地址
+     */
+    String RESOURCE_PREFIX_SPARE = "/spare/upload";
     /**
      * 一年多少天计算
      */

+ 52 - 0
platform-rest/src/main/java/com/platform/rest/controller/upms/SysFileController.java

@@ -180,5 +180,57 @@ public class SysFileController {
         return R.success(fileVO, "上传成功");
     }
 
+    /**
+     * 备件图片上传
+     *
+     * @param file 文件
+     * @return :
+     */
+    @PostMapping("/upload/spare")
+    @SneakyThrows
+    public R doUploadSpare(@RequestParam("file") MultipartFile file) {
+        if (file.isEmpty()) {
+            return R.error("请选择一个上传文件");
+        }
+        FileVO fileVO = new FileVO();
+
+        String fileFullName = file.getOriginalFilename();
+
+        if (StringUtils.isNotBlank(fileFullName)) {
+            fileFullName = fileFullName.replace(",", ",");
+            fileVO.setName(fileFullName);
+            int endIndex = fileFullName.lastIndexOf(".");
+            fileVO.setFileName(fileFullName.substring(0, endIndex));
+            fileVO.setFileFormat(fileFullName.substring(endIndex + 1));
+            String fileType = fileFullName.substring(endIndex);
+            String id = IdGeneratorUtils.getObjectId();
+            String path = CommonConstants.RESOURCE_PREFIX + CommonConstants.RESOURCE_PREFIX_SPARE + "/";
+            String filePath = UPLOAD_ROOT_FOLDER + path;
+            fileVO.setUrl(path + id + fileType);
+            fileVO.setFileFormat(fileType);
+
+            try {
+                if (fileVO.getFileFormat().contains("png") || fileVO.getFileFormat().contains("JPEG")
+                        || fileVO.getFileFormat().contains("jpg") || fileVO.getFileFormat().contains("jpeg")
+                        || fileVO.getFileFormat().contains("PNG") || fileVO.getFileFormat().contains("JPG")) {
+                    File targetFile = new File(filePath);
+                    if (!targetFile.exists()) {
+                        targetFile.mkdirs();
+                    }
+                    FileOutputStream out = new FileOutputStream(filePath + id + fileType);
+                    Thumbnails.of(file.getInputStream()).scale(1).outputQuality(0.2)
+                            .outputFormat(fileFullName.substring(endIndex + 1)).toOutputStream(out);
+                    out.flush();
+                    out.close();
+                } else {
+                    FileUtils.uploadFile(file.getBytes(), filePath, id + fileType);
+                }
+            } catch (Exception e) {
+                throw new Exception("上传文件失败:" + e.getMessage());
+            }
+        }
+        return R.success(fileVO, "上传成功");
+    }
+
 
 }