From 248c359cbfb8ab5f7e7d160de14d3f672d4d50be Mon Sep 17 00:00:00 2001
From: fhx <308050795@qq.com>
Date: 星期四, 06 四月 2023 15:30:22 +0800
Subject: [PATCH] 1.文件处理类修改

---
 src/main/java/com/hx/util/ImageBase64Util.java |   79 ++++++++++++++++++++++++++
 src/main/java/com/hx/util/FileConvertTool.java |   56 +-----------------
 2 files changed, 82 insertions(+), 53 deletions(-)

diff --git a/src/main/java/com/hx/util/FileConvertTool.java b/src/main/java/com/hx/util/FileConvertTool.java
index 55c93c2..a57a0fd 100644
--- a/src/main/java/com/hx/util/FileConvertTool.java
+++ b/src/main/java/com/hx/util/FileConvertTool.java
@@ -6,6 +6,7 @@
 import org.springframework.http.MediaType;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
+import sun.misc.BASE64Decoder;
 
 import java.io.*;
 import java.net.URL;
@@ -184,59 +185,6 @@
     }
 
     /**
-     * 鏍规嵁byte鏁扮粍锛岀敓鎴愭枃浠�
-     */
-    public static File getFile(String baseIn, String filePath, String fileName) {
-        if(baseIn == null){
-            return null;
-        }
-
-        BufferedOutputStream bos = null;
-        FileOutputStream fos = null;
-        File file = null;
-        try {
-            InputStream in = parse_inputStream(baseIn);
-            if(in == null){
-                return null;
-            }
-            File dir = new File(filePath);
-            //鍒ゆ柇鏂囦欢鐩綍鏄惁瀛樺湪
-            if(!dir.exists()){
-                dir.mkdirs();
-            }
-            file = new File(filePath+"/"+fileName);
-            fos = new FileOutputStream(file);
-            byte[] b = new byte[BUFFER_SIZE];
-//            while ((in.read(b)) != -1) {
-//                fos.write(b); // 鍐欏叆鏁版嵁
-//            }
-            int len;
-            while ((len = in.read(b)) != -1){
-                fos.write(b, 0, len);
-            }
-            in.close();
-        } catch (Exception e) {
-            e.printStackTrace();
-        } finally {
-            if (bos != null) {
-                try {
-                    bos.close();
-                } catch (IOException e1) {
-                    e1.printStackTrace();
-                }
-            }
-            if (fos != null) {
-                try {
-                    fos.close();
-                } catch (IOException e1) {
-                    e1.printStackTrace();
-                }
-            }
-        }
-        return file;
-    }
-
-    /**
      * 鑾峰緱鎸囧畾鏂囦欢鐨刡yte鏁扮粍
      */
     private static byte[] getBytes(String filePath){
@@ -350,4 +298,6 @@
             return false;
         }
     }
+
+
 }
diff --git a/src/main/java/com/hx/util/ImageBase64Util.java b/src/main/java/com/hx/util/ImageBase64Util.java
new file mode 100644
index 0000000..5dbfb5a
--- /dev/null
+++ b/src/main/java/com/hx/util/ImageBase64Util.java
@@ -0,0 +1,79 @@
+package com.hx.util;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import java.io.*;
+
+/**
+ * 鍥剧墖鍜宐ase64瀛楃杞崲宸ュ叿
+ * @USER: fhx
+ * @DATE: 2023/4/6
+ **/
+public class ImageBase64Util {
+
+    /**
+     * base64瀛楃涓茶浆鍖栨垚鍥剧墖
+     * @param base64
+     * @param fileName
+     * @param filePath
+     * @return
+     * @throws Exception
+     */
+    public static File base64ToFile(String base64, String fileName, String filePath) throws Exception {
+        if(base64.contains("data:image")){
+            base64 = base64.substring(base64.indexOf(",")+1);
+        }
+        base64 = base64.replace("\r\n", "");
+        //鍒涘缓鏂囦欢鐩綍
+        File file = File.createTempFile(base64, fileName, new File(filePath));
+        BufferedOutputStream bos = null;
+        FileOutputStream fos = null;
+        try {
+            BASE64Decoder decoder = new BASE64Decoder();
+            byte[] bytes =  decoder.decodeBuffer(base64);
+            fos = new FileOutputStream(file);
+            bos = new BufferedOutputStream(fos);
+            bos.write(bytes);
+        }finally {
+            if (bos != null) {
+                try {
+                    bos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return file;
+    }
+
+    /**
+     * 鍥剧墖杞寲鎴恇ase64瀛楃涓�
+     * @param imgFile
+     * @return
+     */
+    public static String GetImageStr(File imgFile) {//灏嗗浘鐗囨枃浠惰浆鍖栦负瀛楄妭鏁扮粍瀛楃涓诧紝骞跺鍏惰繘琛孊ase64缂栫爜澶勭悊
+        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();
+        }
+        //瀵瑰瓧鑺傛暟缁凚ase64缂栫爜
+        BASE64Encoder encoder = new BASE64Encoder();
+        return encoder.encode(data);//杩斿洖Base64缂栫爜杩囩殑瀛楄妭鏁扮粍瀛楃涓�
+    }
+
+}

--
Gitblit v1.8.0