Selaa lähdekoodia

修改上传图片

Tiroble 3 vuotta sitten
vanhempi
commit
ddd1f55e3e

+ 12 - 0
src/main/java/com/steerinfo/dil/component/FileUtils.java

@@ -0,0 +1,12 @@
+package com.steerinfo.dil.component;
+
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
+
+public interface FileUtils {
+
+    public List<String> updateFiles(MultipartFile files[])throws Exception;
+
+     public Object downloadFile(String url) throws Exception;
+}

+ 234 - 0
src/main/java/com/steerinfo/dil/component/ImageFileUtils.java

@@ -0,0 +1,234 @@
+package com.steerinfo.dil.component;
+
+import com.steerinfo.framework.utils.misc.IdGenerator;
+import com.steerinfo.framework.utils.upload.UploadUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.*;
+
+@Component
+public class ImageFileUtils implements FileUtils {
+
+    @Value(value = "${piction.path:/shared}" )
+    private String path;
+
+    public static final HashMap fileTypes = new HashMap();
+
+    static {
+
+        // images
+
+        fileTypes.put("FFD8FF", "jpg");
+
+        fileTypes.put("89504E47", "png");
+
+        fileTypes.put("47494638", "gif");
+
+        fileTypes.put("49492A00", "tif");
+
+        fileTypes.put("424D", "bmp");
+
+        // CAD
+
+        fileTypes.put("41433130", "dwg");
+
+        fileTypes.put("38425053", "psd");
+
+        // 日记本
+
+        fileTypes.put("7B5C727466", "rtf");
+
+        fileTypes.put("3C3F786D6C", "xml");
+
+        fileTypes.put("68746D6C3E", "html");
+
+        // 邮件
+
+        fileTypes.put("44656C69766572792D646174653A", "eml");
+
+        fileTypes.put("D0CF11E0", "doc");
+
+        //excel2003版本文件
+
+        fileTypes.put("D0CF11E0", "xls");
+
+        fileTypes.put("5374616E64617264204A", "mdb");
+
+        fileTypes.put("252150532D41646F6265", "ps");
+
+        fileTypes.put("255044462D312E", "pdf");
+
+        fileTypes.put("504B0304", "docx");
+
+        //excel2007以上版本文件
+
+        fileTypes.put("504B0304", "xlsx");
+
+        fileTypes.put("52617221", "rar");
+
+        fileTypes.put("57415645", "wav");
+
+        fileTypes.put("41564920", "avi");
+
+        fileTypes.put("2E524D46", "rm");
+
+        fileTypes.put("000001BA", "mpg");
+
+        fileTypes.put("000001B3", "mpg");
+
+        fileTypes.put("6D6F6F76", "mov");
+
+        fileTypes.put("3026B2758E66CF11", "asf");
+
+        fileTypes.put("4D546864", "mid");
+
+        fileTypes.put("1F8B08", "gz");
+
+    }
+
+
+    /**
+     * 上传多张图片
+     * @param files
+     * @return
+     * @throws Exception
+     */
+    @Override
+    public synchronized List<String> updateFiles(MultipartFile files[]) throws Exception {
+        //首先通过Calendard对象获得年月日
+        Calendar calendar= Calendar.getInstance();
+        int year = calendar.get(Calendar.YEAR);
+        int month = calendar.get(Calendar.MONTH);
+        int day= calendar.get(Calendar.DAY_OF_MONTH);
+        //上传文件夹路径
+        List<String> urls=new ArrayList<>();
+        for (int i=0;i<files.length;i++){
+            String url = UploadUtils.uploadFile(files[i], new IdGenerator(i, 10), path, File.separator+year+ File.separator+month+ File.separator+day+ File.separator);
+            urls.add(url);
+        }
+        return urls;
+    }
+
+
+    /**
+     * 上传单张图片
+     * @param file
+     * @return
+     * @throws Exception
+     */
+    public synchronized String updateFile(MultipartFile file) throws Exception {
+        //首先通过Calendard对象获得年月日
+        Calendar calendar= Calendar.getInstance();
+        int year = calendar.get(Calendar.YEAR);
+        int month = calendar.get(Calendar.MONTH);
+        int day= calendar.get(Calendar.DAY_OF_MONTH);
+        String url = UploadUtils.uploadFile(file, new IdGenerator(0, 10), path, File.separator+year+ File.separator+month+ File.separator+day+ File.separator);
+        return url;
+    }
+
+    /**
+     * 下载图片获得字节码
+     * @param filePath
+     * @return
+     * @throws Exception
+     */
+    @Override
+    public Object downloadFile(String filePath) throws Exception {
+        File file = new File(filePath);
+        if(file.isDirectory()){
+
+            throw new RuntimeException("当前路径是目录");
+
+        }
+        byte[] b = bytes(file);
+        String type =getFileHeader(b);
+        String src="data:image/"+type+";base64,"+ Base64.getEncoder().encodeToString(b);
+        return src;
+    }
+
+
+
+
+    /**
+     * @return 文件头信息
+     * @author liang.pan
+     * <p>
+     * 方法描述:根据输入流获取文件头信息
+     */
+    public static String getFileHeader(byte[] b) {
+        String value = bytesToHexString(b);
+
+        if (StringUtils.startsWith(value, "FFD8FF")) {
+            value = value.substring(0, 6);
+        }
+
+        //判断什么类型的
+        Set set = fileTypes.keySet();
+        Iterator iterator = set.iterator();
+        while(iterator.hasNext()){
+            String key=iterator.next().toString();
+            if (value.contains(key)){
+                return fileTypes.get(key).toString();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * @param src 要读取文件头信息的文件的byte数组
+     * @return 文件头信息
+     * @author liang.pan
+     * <p>
+     * 方法描述:将要读取文件头信息的文件的byte数组转换成string类型表示
+     */
+    private static String bytesToHexString(byte[] src) {
+        StringBuilder builder = new StringBuilder();
+        if (src == null || src.length <= 0) {
+            return null;
+        }
+        String hv;
+        for (int i = 0; i < src.length; i++) {
+            // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
+            hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
+            if (hv.length() < 2) {
+                builder.append(0);
+            }
+            builder.append(hv);
+        }
+        return builder.toString();
+    }
+
+
+    public byte[] bytes(File file) throws Exception {
+        FileInputStream fin = new FileInputStream(file);
+        try {
+            //可能溢出,简单起见就不考虑太多,如果太大就要另外想办法,比如一次传入固定长度byte[]
+            byte[] bytes  = new byte[fin.available()];
+            //将文件内容写入字节数组,提供测试的case
+            fin.read(bytes);
+            fin.close();
+            return bytes;
+        }catch (Exception ex){
+            throw ex;
+        }
+        finally {
+            if (null != fin) {
+
+                try {
+
+                    fin.close();
+
+                } catch (IOException e) {
+
+                }
+
+            }
+        }
+    }
+}

+ 43 - 8
src/main/java/com/steerinfo/dil/controller/RmsCarrierController.java

@@ -1,6 +1,7 @@
 package com.steerinfo.dil.controller;
 
 import com.alibaba.fastjson.JSON;
+import com.steerinfo.dil.component.ImageFileUtils;
 import com.steerinfo.dil.feign.ESFeign;
 import com.steerinfo.dil.mapper.RmsCarrierMapper;
 import com.steerinfo.dil.service.impl.RmsBidAreaServiceImpl;
@@ -53,6 +54,8 @@ public class RmsCarrierController extends BaseRESTfulController {
     @Autowired
     ESFeign esFeign;
 
+    @Autowired
+    private ImageFileUtils imageFileUtils;
 
     /**
      * 展示承运商信息
@@ -149,7 +152,7 @@ public class RmsCarrierController extends BaseRESTfulController {
     @ApiOperation(value = "获取详细信息", notes = "根据url的id来获取详细信息")
     @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "BigDecimal")
     @PostMapping(value = "/getCarrierById/{id}")
-    public RESTfulResult getCarrierById(@PathVariable("id") BigDecimal id) {
+    public RESTfulResult getCarrierById(@PathVariable("id") BigDecimal id) throws Exception {
         List<Map<String, Object>> list = rmsCarrierService.getCarrierById(id);
         for (Map<String, Object> map : list) {
             String carrierTransportType = (String) map.get("carrierTransportType");
@@ -161,6 +164,29 @@ public class RmsCarrierController extends BaseRESTfulController {
                     map.put("carrierTransportType", 2);
                 }
             }
+            //通过路径获得base64编码
+            //许可证
+            if (map.containsKey("carrierBusinessAblelicense")&&map.get("carrierBusinessAblelicense")!=null){
+                String carrierBusinessAblelicense = imageFileUtils.downloadFile(map.get("carrierBusinessAblelicense").toString()).toString();
+                if (carrierBusinessAblelicense!=null){
+                    map.put("carrierBusinessAblelicense",carrierBusinessAblelicense);
+                }
+            }
+
+            //证明书
+            if (map.containsKey("carrierTransportCertificate")&&map.get("carrierTransportCertificate")!=null){
+                String carrierTransportCertificate = imageFileUtils.downloadFile(map.get("carrierTransportCertificate").toString()).toString();
+                if (carrierTransportCertificate!=null){
+                    map.put("carrierTransportCertificate",carrierTransportCertificate);
+                }
+            }
+            //许可证
+            if (map.containsKey("carrierBusinessLicense")&&map.get("carrierBusinessLicense")!=null){
+                String carrierBusinessLicense = imageFileUtils.downloadFile(map.get("carrierBusinessLicense").toString()).toString();
+                if (carrierBusinessLicense!=null){
+                    map.put("carrierBusinessLicense",carrierBusinessLicense);
+                }
+            }
         }
         return success(list);
     }
@@ -224,13 +250,22 @@ public class RmsCarrierController extends BaseRESTfulController {
 
     //    处理承运商照片
     @PostMapping("/uploadCarrier1")
-    public RESTfulResult uploadCarrier1(@RequestParam("file") MultipartFile multipartFile) {
-        String str="运输证";
-        int result=rmsCarrierService.uploadCarrier(multipartFile,str);
-        if (result<1){
-            return failed();
-        }
-        return success(result);
+    public RESTfulResult uploadCarrier1(@RequestParam("file") MultipartFile multipartFile) throws Exception {
+//        String str="运输证";
+//        int result=rmsCarrierService.uploadCarrier(multipartFile,str);
+//        if (result<1){
+//            return failed();
+//        }
+//        return success(result);
+        String url = imageFileUtils.updateFile(multipartFile);
+        return success(url);
+    }
+
+    @PostMapping(value = "/getFile")
+    @ResponseBody
+    public String  getImgBytes(@RequestBody String url) throws Exception {
+        String b = imageFileUtils.downloadFile(url).toString();
+        return b;
     }
     //    处理承运商照片
     @PostMapping("/uploadCarrier2")

+ 5 - 0
src/main/resources/application-dev.yml

@@ -15,6 +15,11 @@ openFeign:
       url: ${COLUMNDATAFEIGN_URL:172.16.33.166:8083}
     AmsFeign:
       url: ${AMSFEIGN_URL:172.16.33.162:8015}
+piction:
+  # path: /usr/share/nginx/html/image
+  #  path: /test/data/nginx/html/image
+#    path: C:\Users\24390\Desktop\work\a
+  path: /shared
 server:
     port: 8014
 

+ 6 - 0
src/main/resources/application-prod.yml

@@ -15,6 +15,12 @@ openFeign:
       url: ${COLUMNDATAFEIGN_URL:172.16.33.166:8083}
     AmsFeign:
       url: ${AMSFEIGN_URL:172.16.33.166:8079}
+
+piction:
+  # path: /usr/share/nginx/html/image
+  #  path: /test/data/nginx/html/image
+  #    path: C:\Users\24390\Desktop\work\a
+  path: /shared
 server:
     port: 8060