Browse Source

图片上传方法

Signed-off-by: XU <2440975820@qq.com>
XU 10 months ago
parent
commit
c983f0c108

+ 10 - 25
src/main/java/thyyxxk/webserver/controller/technologyArchives/TechnologyArchivesController.java

@@ -11,23 +11,8 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 import thyyxxk.webserver.entity.ResultVo;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives1;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives10;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives11;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives12;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives13;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives14;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives15;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives2;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives3;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives4;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives5;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives6;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives7;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives8;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives9;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchivesMain;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchivesVo;
+import thyyxxk.webserver.entity.technologyArchives.*;
+import thyyxxk.webserver.service.archive.ArchiveServer;
 import thyyxxk.webserver.service.technologyArchives.TechnologyArchivesService;
 
 import javax.servlet.http.HttpServletResponse;
@@ -89,12 +74,12 @@ public class TechnologyArchivesController {
 
     //上传图片
     @PostMapping("/putCertificateImage")
-    public ResultVo<JSONObject> putCertificateImage(@RequestBody MultipartFile file) {
+    public ResultVo<Map<String, Object>> putCertificateImage(@RequestBody MultipartFile file) {
         return service.putCertificateImage(file);
     }
-    @PostMapping("/setImage1")
-    public ResultVo<Map<String, Object>> setImage1(@RequestBody @Validated TechnologyArchives1 first) {
-        return service.setImage1(first);
+    @GetMapping("/setImage1")
+    public ResultVo<Map<String, Object>> setImage1(@RequestParam("socialNo") String socialNo, @RequestParam("getTime") String getTime, String url) {
+        return service.setImage1(socialNo, getTime, url);
     }
     @GetMapping("/delTechnologyArchives1ByCode")
     public ResultVo<Map<String, Object>> delTechnologyArchives1ByCode(@RequestParam("socialNo") String socialNo, @RequestParam("getTime") String getTime){
@@ -116,12 +101,12 @@ public class TechnologyArchivesController {
         return  service.selectImage2(socialNo, no);
     }
     @PostMapping("/putProImage")
-    public ResultVo<JSONObject> putProImage(@RequestBody MultipartFile file) {
+    public ResultVo<Map<String, Object>> putProImage(@RequestBody MultipartFile file) {
         return service.putProImage(file);
     }
-    @PostMapping("/setImage2")
-    public ResultVo<Map<String, Object>> setImage2(@RequestBody @Validated TechnologyArchives2 second) {
-        return service.setImage2(second);
+    @GetMapping("/setImage2")
+    public ResultVo<Map<String, Object>> setImage2(@RequestParam("socialNo") String socialNo,@RequestParam("no") Integer no, String url) {
+        return service.setImage2(socialNo, no, url);
     }
 
     @GetMapping("/delTechnologyArchives2ByCode")

+ 36 - 116
src/main/java/thyyxxk/webserver/service/technologyArchives/TechnologyArchivesService.java

@@ -1,55 +1,21 @@
 package thyyxxk.webserver.service.technologyArchives;
 
 import cn.hutool.core.io.file.FileNameUtil;
-import com.alibaba.fastjson.JSONObject;
+
 import lombok.extern.slf4j.Slf4j;
 import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
-import thyyxxk.webserver.config.envionment.ArchiveConfig;
 import thyyxxk.webserver.config.exception.ExceptionEnum;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives10Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives11Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives12Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives13Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives14Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives15Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives1Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives2Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives3Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives4Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives5Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives6Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives7Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives8Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchives9Dao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchivesDao;
-import thyyxxk.webserver.dao.his.technologyArchives.TechnologyArchivesMainDao;
+import thyyxxk.webserver.dao.his.technologyArchives.*;
 import thyyxxk.webserver.entity.HeadInfo;
 import thyyxxk.webserver.entity.ResultVo;
 import thyyxxk.webserver.entity.dictionary.CodeName;
-import thyyxxk.webserver.entity.dictionary.EmployeeMi;
 import thyyxxk.webserver.entity.login.UserInfo;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives1;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives10;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives11;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives12;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives13;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives14;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives15;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives2;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives3;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives4;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives5;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives6;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives7;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives8;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchives9;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchivesMain;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchivesType;
-import thyyxxk.webserver.entity.technologyArchives.TechnologyArchivesVo;
+import thyyxxk.webserver.entity.technologyArchives.*;
+import thyyxxk.webserver.service.archive.ArchiveServer;
 import thyyxxk.webserver.service.redislike.RedisLikeService;
 import thyyxxk.webserver.utils.ExcelUtil;
 import thyyxxk.webserver.utils.ResultVoUtil;
@@ -111,7 +77,8 @@ public class TechnologyArchivesService {
 
     private final TechnologyArchives15Dao fifteenthDao;
 
-    private final ArchiveConfig config;
+    private final ArchiveServer archiveServer;
+
 
     @Autowired
     public TechnologyArchivesService(TechnologyArchivesDao dao, TechnologyArchivesMainDao mainDao, RedisLikeService redisLikeService,
@@ -120,7 +87,7 @@ public class TechnologyArchivesService {
                                      TechnologyArchives7Dao seventhDao, TechnologyArchives9Dao ninthDao, TechnologyArchives10Dao tenthDao,
                                      TechnologyArchives12Dao twelfthDao, TechnologyArchives13Dao thirteenthDao, TechnologyArchives14Dao fourteenthDao,
                                      TechnologyArchives8Dao eighthDao, TechnologyArchives11Dao eleventhDao, TechnologyArchives15Dao fifteenthDao
-            , ArchiveConfig config) {
+            , ArchiveServer archiveServer) {
         this.dao = dao;
         this.mainDao = mainDao;
         this.redisLikeService = redisLikeService;
@@ -139,8 +106,7 @@ public class TechnologyArchivesService {
         this.eighthDao = eighthDao;
         this.eleventhDao = eleventhDao;
         this.fifteenthDao = fifteenthDao;
-        this.config = config;
-//        config.getUploadImageCertificateImage();
+        this.archiveServer = archiveServer;
     }
 
     /**
@@ -203,6 +169,8 @@ public class TechnologyArchivesService {
         // 文化程度
         List<CodeName> education = dao.getEducation();
         resultMap.put("education", education);
+        List<CodeName> education1 = dao.getEducation();
+        resultMap.put("education1", education1);
         return ResultVoUtil.success(resultMap);
     }
 
@@ -321,48 +289,25 @@ public class TechnologyArchivesService {
     //职称证书图片
     @Value("${is-prod}")
     private boolean isProd;
-    public String url1;
-
-    public ResultVo<JSONObject> putCertificateImage(MultipartFile file) {
-
-        String uploadDir;
-        if (isProd) {
-            uploadDir = "/mnt/archive/uploadimage/certificateImage";
-        } else {
-            //Z为网络映射盘(172.16.32.197\ uploadimage)
-//            uploadDir = config.getUploadImageCertificateImage();
-            uploadDir = "Z:\\certificateImage";
-        }
-        JSONObject js = new JSONObject();
 
-        String name = file.getOriginalFilename();
-        String newName = UUID.randomUUID().toString().replaceAll("-", "");
-        String fileName = newName.substring(0, 5) + name.substring(name.lastIndexOf("."));
-        Path uploadPath = Paths.get(uploadDir);
-
-        try {
-            Files.createDirectories(uploadPath);
-            Path filePath = uploadPath.resolve(fileName);
-            //
-            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
-            log.info("文件:{}", fileName);
-//            url1 = "http://172.16.30.8:8077/certificateImage/" + fileName;
-//            url1 = "http://172.16.32.167:8077/certificateImage/" + fileName;
-            url1 = "http://172.16.32.197:8077/certificateImage/" + fileName;
-//            firstDao.updateCertificateImage(url, socialNo, getTime);
-            js.put("url", url1);
-            js.put("name", fileName);
-            return ResultVoUtil.success(js);
-        } catch (IOException e) {
-            log.error("上传错误:{}", e.getMessage());
-            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "上传错误请重新上传。");
-        }
+    public ResultVo<Map<String, Object>> putCertificateImage(MultipartFile file) {
+        UserInfo user = redisLikeService.getUserInfoByToken();
+        String code = user.getCode();
+        String uploadDir = "/uploadimage/certificateImage/" + code;
+        Map<String, Object> map = new HashMap<>();
+        map.put("data",archiveServer.uploadFile(file,uploadDir,!isProd));
+        return ResultVoUtil.success(map);
     }
 
-    public ResultVo<Map<String, Object>> setImage1(TechnologyArchives1 first) {
+    /**
+     * @param socialNo  身份证号  getTime 批准时间 url 图片链接
+     * @Description 根据身份证号, 批准时间来保存上传图片
+     * @return map
+     */
+    public ResultVo<Map<String, Object>> setImage1(String socialNo, String getTime, String url) {
         Map<String, Object> map = new HashMap<>();
-        String socialNo = first.getSocialNo();
-        String getTime = first.getGetTime();
+        String url1 = "http://172.16.32.197:8077/upload" + url;
+        System.out.println(url1);
         int imgMap = firstDao.updateCertificateImage(url1, socialNo, getTime);
         map.put("data", imgMap);
         return ResultVoUtil.success(map);
@@ -451,46 +396,21 @@ public class TechnologyArchivesService {
     }
 
     //其他证书图片
-    public String url2;
-
-    public ResultVo<JSONObject> putProImage(MultipartFile file) {
-
-        String uploadDir;
-        if (isProd) {
-            uploadDir = "/mnt/archive/uploadimage/certificateImage";
-        } else {
-            uploadDir = "Z:\\proImage";
-        }
-        JSONObject js = new JSONObject();
-
-        String name = file.getOriginalFilename();
-        String newName = UUID.randomUUID().toString().replaceAll("-", "");
-        String fileName = newName.substring(0, 5) + name.substring(name.lastIndexOf("."));
-        Path uploadPath = Paths.get(uploadDir);
+    public ResultVo<Map<String, Object>> putProImage(MultipartFile file) {
 
-        try {
-            Files.createDirectories(uploadPath);
-            Path filePath = uploadPath.resolve(fileName);
-            //
-            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
-            log.info("文件:{}", fileName);
-//            url2 = "http://172.16.32.167:8077/proImage/" + fileName;
-            url2 = "http://172.16.32.197:8077/proImage/" + fileName;
-//            firstDao.updateCertificateImage(url, socialNo, getTime);
-            js.put("url", url2);
-            js.put("name", fileName);
-            return ResultVoUtil.success(js);
-        } catch (IOException e) {
-            log.error("上传错误:{}", e.getMessage());
-            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "上传错误请重新上传。");
-        }
+        UserInfo user = redisLikeService.getUserInfoByToken();
+        String code = user.getCode();
+        String uploadDir = "/uploadimage/proImage/" + code;
+        Map<String, Object> map = new HashMap<>();
+        map.put("data",archiveServer.uploadFile(file,uploadDir,!isProd));
+        return ResultVoUtil.success(map);
     }
 
-    public ResultVo<Map<String, Object>> setImage2(TechnologyArchives2 second) {
+    public ResultVo<Map<String, Object>> setImage2(String socialNo, Integer no, String url) {
         Map<String, Object> map = new HashMap<>();
-        String socialNo = second.getSocialNo();
-        Integer no = second.getNo();
-        int imgMap = secondDao.updateProImage(url2, socialNo, no);
+        String url1 = "http://172.16.32.197:8077/upload" + url;
+        System.out.println(url1);
+        int imgMap = secondDao.updateProImage(url1, socialNo, no);
         map.put("data", imgMap);
         return ResultVoUtil.success(map);
     }