From ce96649e79bf730aef73f3bec205c1c00a743635 Mon Sep 17 00:00:00 2001
From: yz_08 <yz_0812@outlook.com>
Date: 星期三, 03 三月 2021 22:08:01 +0800
Subject: [PATCH] 修改bug

---
 febs-common/febs-common-core/src/main/java/cc/mrbird/febs/common/core/utils/MyUtil.java |   97 ++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 81 insertions(+), 16 deletions(-)

diff --git a/febs-common/febs-common-core/src/main/java/cc/mrbird/febs/common/core/utils/MyUtil.java b/febs-common/febs-common-core/src/main/java/cc/mrbird/febs/common/core/utils/MyUtil.java
index 2b4b383..86a0f69 100644
--- a/febs-common/febs-common-core/src/main/java/cc/mrbird/febs/common/core/utils/MyUtil.java
+++ b/febs-common/febs-common-core/src/main/java/cc/mrbird/febs/common/core/utils/MyUtil.java
@@ -2,17 +2,16 @@
 
 import cc.mrbird.febs.common.core.exception.FebsException;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.lang.Dict;
-
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.multipart.MultipartFile;
-import cn.hutool.core.io.FileUtil;
+import sun.misc.BASE64Decoder;
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
-import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.zip.ZipEntry;
@@ -22,11 +21,7 @@
 public class MyUtil {
 
     public static void downloadFile(File file, HttpServletResponse response) throws IOException {
-        InputStream fin = null;
-        ServletOutputStream out = null;
-        try {
-            fin = new FileInputStream(file);
-            out = response.getOutputStream();
+        try (InputStream fin = new FileInputStream(file); ServletOutputStream out = response.getOutputStream()) {
             response.setCharacterEncoding("utf-8");
             response.setContentType("application/x-download");
             response.addHeader("Content-Disposition", "attachment;filename=resume.doc");
@@ -34,16 +29,12 @@
             byte[] buffer = new byte[1024];
             int bytesToRead = -1;
             // 通过循环将读入的Word文件的内容输出到浏览器中
-            while((bytesToRead = fin.read(buffer)) != -1) {
+            while ((bytesToRead = fin.read(buffer)) != -1) {
                 out.write(buffer, 0, bytesToRead);
             }
         } catch (Exception e) {
             log.error("下载文件异常" + e);
             e.printStackTrace();
-        } finally {
-            if(fin != null) fin.close();
-            if(out != null) out.close();
-
         }
     }
 
@@ -117,8 +108,8 @@
         if (file.getSize() <= 0) {
             throw new FebsException("上传的文件大小需要大于0kb");
         }
-        if (file.getSize() > 50 * 1024* 1024) {
-            throw new FebsException("上传的文件大于50M");
+        if (file.getSize() > 200 * 1024* 1024) {
+            throw new FebsException("上传的文件大于200M");
         }
         if (!FileUtil.exist(uploadPpath)) {
             FileUtil.mkdir(uploadPpath);
@@ -128,13 +119,87 @@
         String suffix = "";
         if (fileName.indexOf(".") > 0) {
             //后缀
-            suffix = fileName.substring(fileName.indexOf("."), fileName.length());
+            suffix = fileName.substring(fileName.indexOf("."));
         }
         //生成新的名字
         String newName = nextIdStr + suffix;
         //上传
         file.transferTo(new File(uploadPpath + newName));
         return Dict.create().set("fileName",fileName).set("suffix",suffix).set("newName",newName);
+    }
 
+    /**
+     *      对字节数组字符串进行Base64解码并生成图片
+      */
+    public static boolean generateImage(String imgStr,String url) {
+        if (imgStr == null) {
+            // 图像数据为空
+            return false;
+        }
+        imgStr = imgStr.split(",")[1];
+                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;
+                }
+            }
+            // 生成jpeg图片
+            String imgFilePath = url;
+            OutputStream out = new FileOutputStream(imgFilePath);
+            out.write(b);
+            out.flush();
+            out.close();
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+
+    /**
+     * 将base64字符串,生成文件
+     */
+    public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) {
+
+        BufferedOutputStream bos = null;
+        FileOutputStream fos = null;
+        File file = null;
+        try {
+            File dir = new File(filePath);
+            if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
+                dir.mkdirs();
+            }
+
+            BASE64Decoder decoder = new BASE64Decoder();
+            byte[] bfile = decoder.decodeBuffer(fileBase64String);
+
+            file = new File(filePath + File.separator + fileName);
+            fos = new FileOutputStream(file);
+            bos = new BufferedOutputStream(fos);
+            bos.write(bfile);
+            return file;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        } finally {
+            if (bos != null) {
+                try {
+                    bos.close();
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
+            }
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
+            }
+        }
     }
 }

--
Gitblit v1.8.0