Ver Fonte

文件上传优化

WXF há 3 anos atrás
pai
commit
d16c29ab51

+ 1 - 0
api-common/src/main/java/api/common/pojo/constants/DictConstants.java

@@ -34,6 +34,7 @@ public class DictConstants {
 
 
     public static final String ALGORITHM_FILE = "algorithmFile";    //算法文件上传type
+    public static final String VEHICLE_IMG_FILE = "vehicleImg";    //车辆图片上传type
 
     // 算法镜像上传类型
     public static final String ALGORITHM_UPLOAD_MODE_FILE = "1";    // 文件上传

+ 0 - 31
simulation-resource-common/src/main/java/com/css/simulation/resource/common/controller/MinioController.java

@@ -63,41 +63,10 @@ public class MinioController {
                 objectName
         );
         String previewUrl = MinioUtil.getPreviewUrl(minioClientPrivate, Method.GET, bucketName, objectName);
-
         return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "请求成功!", previewUrl);
     }
 
 
-    @PostMapping(value = "/uploadProcessBar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
-    public ResponseBodyVO<String> uploadProcessBar(
-            @RequestPart("file") MultipartFile file,
-            HttpServletRequest request
-    ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
-        String type = request.getParameter("type");
-        String objectPath = request.getParameter("objectPath");
-        if (file != null) {
-            String objectName = "";
-            Integer nowTime = TimeUtil.getRq(new Date(), 0);
-            if (type.equals(DictConstants.ALGORITHM_FILE)) {
-                objectName = "algorithm/" + nowTime + "/" + objectPath + "/" + file.getOriginalFilename();
-            }
-
-            MinioUtil.uploadProcessBar(
-                    minioClientPrivate,
-                    file,
-                    bucketName,
-                    objectName,
-                    objectPath,
-                    redisController
-            );
-            Map map = new HashMap();
-            map.put("fileName", objectName);
-
-            return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS, map);
-        }
-        return new ResponseBodyVO(ResponseBodyVO.Response.CLIENT_FAILURE, "文件流file为空,请检查!");
-    }
-
     @PostMapping("/list")
     public ResponseBodyVO<List<String>> list(
             @RequestBody @Validated MinioParameter minioParameter

+ 1 - 59
simulation-resource-common/src/main/java/com/css/simulation/resource/common/util/MinioUtil.java

@@ -1,10 +1,5 @@
 package com.css.simulation.resource.common.util;
 
-import api.common.pojo.constants.DictConstants;
-import api.common.pojo.param.RedisParameter;
-import api.common.util.ObjectUtil;
-import com.css.simulation.resource.common.controller.RedisController;
-import com.css.simulation.resource.common.util.processbar.ProgressInputStream;
 import io.minio.*;
 import io.minio.errors.*;
 import io.minio.http.Method;
@@ -12,7 +7,6 @@ import io.minio.messages.Item;
 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.InvalidKeyException;
@@ -167,7 +161,7 @@ public class MinioUtil {
     ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
         InputStream inputStream = multipartFile.getInputStream();
         long objectSize = multipartFile.getSize();
-//        long partSize = 5 * 1024 * 1024L; // 分片最小 5M
+        //long partSize = 5 * 1024 * 1024L; // 分片最小 5M
         long partSize = -1; // 不分片
         minioClient.putObject(PutObjectArgs.builder()
                 .stream(inputStream, objectSize, partSize)
@@ -176,41 +170,6 @@ public class MinioUtil {
                 .build());
     }
 
-    /**
-     * 文件上传,保存进度
-     */
-    public static void uploadProcessBar(
-            MinioClient minioClient,
-            MultipartFile multipartFile,
-            String bucketName,
-            String objectName,
-            String objectPath,
-            RedisController redisController
-    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
-        InputStream inputStream = multipartFile.getInputStream();
-        long fileSize = multipartFile.getSize();
-        //进度计算参数:传输量,进度标尺,文件大小,进度值
-        long[] longs = {0L, 0L, fileSize, 0L};
-        InputStream pis = new BufferedInputStream(
-                new ProgressInputStream(inputStream){
-                    @Override
-                    public void getStep(int readBytes){
-                        RedisParameter redisParameter = getProgress(readBytes,longs);
-                        if(ObjectUtil.isNotNull(redisParameter)){
-                            redisParameter.setKey(DictConstants.ALGORITHM_KEY + objectPath);//进度缓存的 key
-                            redisController.set(redisParameter);
-                        }
-                    }
-                });
-        minioClient.putObject(PutObjectArgs.builder()
-                .bucket(bucketName)
-                .object(objectName)
-                .stream(pis, fileSize, -1)
-                .build());
-        pis.close();
-
-    }
-
     /**
      * 下载文件
      */
@@ -241,23 +200,6 @@ public class MinioUtil {
                 .build());
     }
 
-    public static RedisParameter getProgress(int readBytes,long[] longs){
-        double progressStep = 0.05;//进度条的步长
-        longs[0] += readBytes;//累加
-        if(longs[0] >= longs[1]){
-            longs[3] += 1;
-            double progress = progressStep * longs[3];//进度
-            longs[1] = (long) (longs[2] * progress);//标尺增长
-            RedisParameter redisParameter = new RedisParameter();
-            redisParameter.setValue(String.format("%.2f",progress-progressStep));
-            redisParameter.setMinutes(1);
-            if(longs[0] == longs[2]){
-                redisParameter.setValue("1.00");
-            }
-            return redisParameter;
-        }
-        return null;
-    }
     /**
      * 删除文件
      *

+ 0 - 5
simulation-resource-server/src/main/java/com/css/simulation/resource/feign/RedisService.java

@@ -5,9 +5,7 @@ import api.common.pojo.param.RedisParameter;
 import com.css.simulation.resource.common.config.FeignConfiguration;
 import com.css.simulation.resource.feign.fallback.RedisServiceFallback;
 import org.springframework.cloud.openfeign.FeignClient;
-import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
 
 import java.util.List;
 import java.util.Map;
@@ -33,7 +31,4 @@ public interface RedisService {
     @PostMapping("/redis/getDictMaps")
     ResponseBodyVO<Map<String,String>> getDictMaps(List<String> keyList);
 
-    @PostMapping(value = "/redis/get")
-    ResponseBodyVO<String> getProgress(RedisParameter redisParameter);
-
 }

+ 0 - 4
simulation-resource-server/src/main/java/com/css/simulation/resource/feign/fallback/RedisServiceFallback.java

@@ -32,8 +32,4 @@ public class RedisServiceFallback implements RedisService {
         return new ResponseBodyVO<>(ResponseBodyVO.Response.SERVER_FAILURE);
     }
 
-    @Override
-    public ResponseBodyVO<String> getProgress(RedisParameter redisParameter) {
-        return new ResponseBodyVO<>(ResponseBodyVO.Response.SERVER_FAILURE);
-    }
 }

+ 20 - 46
simulation-resource-server/src/main/java/com/css/simulation/resource/scene/ctrl/FileController.java → simulation-resource-server/src/main/java/com/css/simulation/resource/system/ctrl/FileController.java

@@ -1,13 +1,11 @@
-package com.css.simulation.resource.scene.ctrl;
+package com.css.simulation.resource.system.ctrl;
 
 import api.common.pojo.common.ResponseBodyVO;
 import api.common.pojo.constants.DictConstants;
 import api.common.pojo.param.MinioParameter;
-import api.common.pojo.param.RedisParameter;
-import api.common.util.ObjectUtil;
+import api.common.util.StringUtil;
 import api.common.util.TimeUtil;
 import com.css.simulation.resource.feign.FileDownService;
-import com.css.simulation.resource.feign.RedisService;
 import feign.Response;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.MediaType;
@@ -35,10 +33,7 @@ public class FileController {
     @Resource
     private FileDownService fileDownService;
 
-    @Resource
-    private RedisService redisService;
-
-    @PostMapping(value = "/uploadWj", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
+    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     public ResponseBodyVO<Map> uploadWj(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
         //获取文件类型
         String type = request.getParameter("type");
@@ -46,52 +41,33 @@ public class FileController {
         if (multipartFile != null) {
             String fileName = "";
             Integer nowTime = TimeUtil.getRq(new Date(), 0);
-            if (type.equals(DictConstants.SCENE_NATURAL)) {
-                fileName = "自然驾驶场景/" + nowTime + "/" + objectPath + "/" + multipartFile.getOriginalFilename();
-            } else if (type.equals(DictConstants.SCENE_STANDARD)) {
-                fileName = "标准法规场景/" + nowTime + "/" + objectPath + "/" + multipartFile.getOriginalFilename();
-            } else if (type.equals(DictConstants.SCENE_ACCIDENT)) {
-                fileName = "交通事故场景/" + nowTime + "/" + objectPath + "/" + multipartFile.getOriginalFilename();
-            } else if (type.equals(DictConstants.SCENE_GENERAL)) {
-                fileName = "泛化场景/" + nowTime + "/" + objectPath + "/" + multipartFile.getOriginalFilename();
+            String randomCode = StringUtil.getRandomCode();
+            if (type.equals(DictConstants.VEHICLE_IMG_FILE)) {
+                fileName = DictConstants.VEHICLE_IMG_FILE + "/" + objectPath + "/" + nowTime + "/" + randomCode + "/" + multipartFile.getOriginalFilename();
             } else {
-                fileName = multipartFile.getOriginalFilename();
+                new ResponseBodyVO(ResponseBodyVO.Response.CLIENT_FAILURE, "文件类型错误!");
             }
-            ResponseBodyVO<String> respon;
-            respon = fileDownService.upload(multipartFile, fileName);
-            String videoPreview = respon.getMessage();
-            Map map = new HashMap();
-           // map.put("videoPreview", videoPreview);
-            map.put("fileName", fileName);
-
-            return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS, map);
+            ResponseBodyVO<String> respon = fileDownService.upload(multipartFile, fileName);
+            if(respon.isStatus()){
+                String previewUrl = respon.getInfo();
+                Map map = new HashMap();
+                map.put("previewUrl", previewUrl);
+                map.put("fileName", fileName);
+                return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS, map);
+            }
+            new ResponseBodyVO(ResponseBodyVO.Response.CLIENT_FAILURE, "上传失败!");
         }
         return new ResponseBodyVO(ResponseBodyVO.Response.CLIENT_FAILURE, "文件流file为空,请检查!");
     }
 
     @PostMapping(value = "/download")
-    public void download(
-            HttpServletResponse response, @RequestBody @Validated MinioParameter parms
-    ) throws IOException {
+    public void download( HttpServletResponse response, @RequestBody @Validated MinioParameter parms ) throws IOException {
         Response download = fileDownService.download(parms);
         Response.Body body = download.body();
         InputStream inputStream = body.asInputStream();
         downloadForHttp(parms.getObjectName(), inputStream, response, 1024);
     }
 
-    @PostMapping(value = "/getProgress")
-    public ResponseBodyVO getProgress(@RequestBody Map<String,String> paramMap){
-        String objectPath = paramMap.get("objectPath");
-        RedisParameter redisParameter = new RedisParameter();
-        redisParameter.setKey(DictConstants.ALGORITHM_KEY + objectPath);
-        ResponseBodyVO resp = redisService.getProgress(redisParameter);
-        if(ObjectUtil.isNull(resp.getInfo())){
-            resp.setInfo("0.00");
-        }
-        return resp;
-    }
-
-
 
     public void downloadForHttp(String fileName, InputStream in, HttpServletResponse response, int bufferSize) throws IOException {
         int idx = fileName.lastIndexOf("/");
@@ -110,17 +86,15 @@ public class FileController {
         }
     }
 
-//从minio获取文件列表
+    //从minio获取文件列表
     @PostMapping(value = "/queryList")
-    public List<String> queryList(@RequestBody @Validated MinioParameter parms
-    ) throws IOException {
+    public List<String> queryList(@RequestBody @Validated MinioParameter parms ) throws IOException {
         List<String> list = fileDownService.list(parms).getInfo();
         return list;
     }
 
     @PostMapping(value = "/queryList1")
-    public List<String> queryList1(@RequestBody @Validated MinioParameter parms
-    ) throws IOException {
+    public List<String> queryList1(@RequestBody @Validated MinioParameter parms ) throws IOException {
         List<String> list = fileDownService.listDeepOne(parms).getInfo();
         return list;
     }