Jelajahi Sumber

dxf检查表新增的时候对照片进行上传

wuhonghao 4 tahun lalu
induk
melakukan
d454ac4100

+ 11 - 1
railway-admin/src/main/java/com/railway/web/controller/business/catenary/BusJcbFdfxjyqController.java

@@ -9,6 +9,7 @@ import com.railway.common.core.domain.AjaxResult;
 import com.railway.common.core.page.TableDataInfo;
 import com.railway.common.utils.DateUtils;
 import com.railway.common.utils.StringUtils;
+import com.railway.common.utils.file.BASE64DecodedMultipartFile;
 import com.railway.common.utils.poi.WordUtils;
 import com.railway.system.service.ISysFileService;
 import io.swagger.annotations.Api;
@@ -29,6 +30,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * @author wuhonghao
@@ -56,10 +58,16 @@ public class BusJcbFdfxjyqController extends BaseController {
 
     @ApiOperation(value = "新增")
     @PostMapping("/add")
-    public AjaxResult add(@Validated @RequestBody BusJcbFdfxjyq busJcbFdfxjyq) {
+    public AjaxResult add(@Validated @RequestBody BusJcbFdfxjyq busJcbFdfxjyq) throws Exception {
         BaseDeptStation deptStation = stationService.getInfo(busJcbFdfxjyq.getDeptId(),
             busJcbFdfxjyq.getLineId(), busJcbFdfxjyq.getStationId());
         busJcbFdfxjyq.setDeptStationId(deptStation.getId());
+        //将文件转成base64
+        if(busJcbFdfxjyq.getPicUrl()!=null&&!busJcbFdfxjyq.getPicUrl().equals("")){
+            MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(busJcbFdfxjyq.getPicUrl());
+            String path = sysFileService.uploadFile(multipartFile);
+            busJcbFdfxjyq.setPicUrl(path);
+        }
         return toAjax(busJcbFdfxjyqService.create(busJcbFdfxjyq));
     }
 
@@ -168,6 +176,8 @@ public class BusJcbFdfxjyqController extends BaseController {
         }
         params.put("${header}",header);
 
+
+
         //生成word文件的文件名
         String fileName = "DXF分段绝缘器手册.docx";
         String template = "word/template/DXF分段绝缘器手册.docx";

+ 266 - 0
railway-common/src/main/java/com/railway/common/utils/file/BASE64DecodedMultipartFile.java

@@ -0,0 +1,266 @@
+package com.railway.common.utils.file;
+
+
+import org.springframework.web.multipart.MultipartFile;
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * @ClassName BASE64DecodedMultipartFile
+ * @Description TODO
+ * @Author Uncle
+ * @Date 2021/05/25 17:17
+ * @Version 1.0
+ */
+public class BASE64DecodedMultipartFile implements MultipartFile {
+
+    private final byte[] imgContent;
+    private final String header;
+
+    public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
+        this.imgContent = imgContent;
+        this.header = header.split(";")[0];
+    }
+
+    @Override
+    public String getName() {
+        // TODO - implementation depends on your requirements
+        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
+    }
+
+    @Override
+    public String getOriginalFilename() {
+        // TODO - implementation depends on your requirements
+        return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
+    }
+
+    @Override
+    public String getContentType() {
+        // TODO - implementation depends on your requirements
+        return header.split(":")[1];
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return imgContent == null || imgContent.length == 0;
+    }
+
+    @Override
+    public long getSize() {
+        return imgContent.length;
+    }
+
+    @Override
+    public byte[] getBytes() throws IOException {
+        return imgContent;
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        return new ByteArrayInputStream(imgContent);
+    }
+
+    @Override
+    public void transferTo(File dest) throws IOException, IllegalStateException {
+        new FileOutputStream(dest).write(imgContent);
+    }
+
+    /**
+     * base64转MultipartFile文件
+     *
+     * @param base64
+     * @return
+     */
+    public static MultipartFile base64ToMultipart(String base64) {
+        try {
+            String[] baseStrs = base64.split(",");
+
+            BASE64Decoder decoder = new BASE64Decoder();
+            byte[] b = new byte[0];
+            b = decoder.decodeBuffer(baseStrs[1]);
+
+            for (int i = 0; i < b.length; ++i) {
+                if (b[i] < 0) {
+                    b[i] += 256;
+                }
+            }
+
+            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+
+    /**
+     * base64转MultipartFile文件
+     *
+     * @param base64
+     * @return
+     */
+    public static MultipartFile base64ToMultipartOnt(String base64) {
+        try {
+
+            BASE64Decoder decoder = new BASE64Decoder();
+            byte[] b = new byte[0];
+            b = decoder.decodeBuffer(base64);
+
+            for (int i = 0; i < b.length; ++i) {
+                if (b[i] < 0) {
+                    b[i] += 256;
+                }
+            }
+
+            return new BASE64DecodedMultipartFile(b, base64);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    /**
+     * 本地图片转换成base64字符串
+     *
+     * @param imgFile 图片本地路径
+     * @return
+     * @author uncle
+     * @dateTime 2018-02-23 14:40:46
+     */
+    public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
+
+
+        InputStream in = null;
+        byte[] data = null;
+
+        // 读取图片字节数组
+        try {
+            in = new FileInputStream(imgFile);
+
+            data = new byte[in.available()];
+            in.read(data);
+            in.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        // 对字节数组Base64编码
+        BASE64Encoder encoder = new BASE64Encoder();
+
+        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
+    }
+
+
+    /**
+     * 在线图片转换成base64字符串
+     *
+     * @param imgURL 图片线上路径
+     * @return
+     * @author uncle
+     * @dateTime 2018-02-23 14:43:18
+     */
+    public static String ImageToBase64ByOnline(String imgURL) {
+        ByteArrayOutputStream data = getByteArrayOutputStream(imgURL);
+        // 对字节数组Base64编码
+        BASE64Encoder encoder = new BASE64Encoder();
+        return encoder.encode(data.toByteArray());
+    }
+
+    public static byte[] ImageToByteByOnline(String imgURL) {
+        ByteArrayOutputStream data = getByteArrayOutputStream(imgURL);
+        return data.toByteArray();
+    }
+
+    private static ByteArrayOutputStream getByteArrayOutputStream(String imgURL) {
+        ByteArrayOutputStream data = new ByteArrayOutputStream();
+        try {
+            // 创建URL
+            URL url = new URL(imgURL);
+            byte[] by = new byte[1024];
+            // 创建链接
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            conn.setRequestMethod("GET");
+            conn.setConnectTimeout(5000);
+            InputStream is = conn.getInputStream();
+            // 将内容读取内存中
+            int len = -1;
+            while ((len = is.read(by)) != -1) {
+                data.write(by, 0, len);
+            }
+            // 关闭流
+            is.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return data;
+    }
+
+
+    /**
+     * base64字符串转换成图片
+     *
+     * @param imgStr      base64字符串
+     * @param imgFilePath 图片存放路径
+     * @return
+     * @author uncle
+     * @dateTime 2018-02-23 14:42:17
+     */
+    public static boolean Base64ToImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
+
+        if (imgStr == null) {
+            // 图像数据为空
+            return false;
+        }
+
+
+        BASE64Decoder decoder = new BASE64Decoder();
+        try {
+            // Base64解码
+            byte[] b = decoder.decodeBuffer(imgStr);
+            for (int i = 0; i < b.length; ++i) {
+                if (b[i] < 0) {// 调整异常数据
+                    b[i] += 256;
+                }
+            }
+
+            OutputStream out = new FileOutputStream(imgFilePath);
+            out.write(b);
+            out.flush();
+            out.close();
+
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+
+    }
+
+    /**
+     * 功能描述 :MultipartFile转file对象
+     *
+     * @param
+     * @return
+     * @author uncle
+     * @date 2019/5/10 18:30
+     */
+    public static File MultipartFileToFile(MultipartFile multiFile) {
+        // 获取文件名
+        String fileName = multiFile.getOriginalFilename();
+        // 获取文件后缀
+        String prefix = fileName.substring(fileName.lastIndexOf("."));
+        // 用当前时间作为文件名,防止生成的临时文件重复
+        try {
+            File file = File.createTempFile(System.currentTimeMillis() + "", prefix);
+
+            multiFile.transferTo(file);
+
+            return file;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+}