From c1a9eee9c9aa0d3c7592fa75e70243e0297b3a0d Mon Sep 17 00:00:00 2001
From: fhx <308050795@qq.com>
Date: 星期四, 06 四月 2023 10:38:03 +0800
Subject: [PATCH] 1.新增文件工具类

---
 src/main/java/com/hx/util/FileConvertTool.java |  353 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 353 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/util/FileConvertTool.java b/src/main/java/com/hx/util/FileConvertTool.java
new file mode 100644
index 0000000..55c93c2
--- /dev/null
+++ b/src/main/java/com/hx/util/FileConvertTool.java
@@ -0,0 +1,353 @@
+package com.hx.util;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.io.IOUtils;
+import org.springframework.http.MediaType;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
+
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Base64;
+
+/**
+ * 鏂囦欢杞崲宸ュ叿绫�
+ * @USER: fhx
+ * @DATE: 2022/3/2
+ **/
+public class FileConvertTool {
+
+    private static final int  BUFFER_SIZE = 2 * 1024;
+
+
+    /** 鑾峰彇缃戠粶璺緞鏂囦欢娴� */
+    public static InputStream getUrlFile(String urlPath) throws IOException {
+        if(StringUtils.isEmpty(urlPath)){
+            return null;
+        }
+        // 鏋勯�燯RL
+        URL url = new URL(urlPath);
+        // 鎵撳紑杩炴帴
+        URLConnection con = url.openConnection();
+        //璁剧疆璇锋眰瓒呮椂涓�5s
+        con.setConnectTimeout(5*1000);
+        // 杈撳叆娴�
+        InputStream is = con.getInputStream();
+        return is;
+    }
+
+    /**
+     * 鑾峰彇鏂囦欢base64瀛楃涓�
+     * @param urlPath  鏂囦欢璺緞
+     * @return
+     * @throws IOException
+     */
+    public static String getFileBaseStrByUrl(String urlPath) throws IOException {
+        InputStream is = getUrlFile(urlPath);
+        byte[] bytes = IOUtils.toByteArray(is);
+        String encoded = Base64.getEncoder().encodeToString(bytes);
+        return encoded;
+    }
+
+    // inputStream杞琽utputStream
+    public static ByteArrayOutputStream parse(final InputStream in) throws Exception {
+        final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
+        int ch;
+        while ((ch = in.read()) != -1) {
+            swapStream.write(ch);
+        }
+        return swapStream;
+    }
+
+    // outputStream杞琲nputStream
+    public static ByteArrayInputStream parse(final OutputStream out) throws Exception {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        baos = (ByteArrayOutputStream) out;
+        final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
+        return swapStream;
+    }
+
+    // inputStream杞琒tring
+    public static String parse_String(final InputStream in) throws Exception {
+        final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
+        int ch;
+        while ((ch = in.read()) != -1) {
+            swapStream.write(ch);
+        }
+        return swapStream.toString();
+    }
+
+    // OutputStream 杞琒tring
+    public static String parse_String(final OutputStream out) throws Exception {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        baos = (ByteArrayOutputStream) out;
+        final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
+        return swapStream.toString();
+    }
+
+    // String杞琲nputStream
+    public static ByteArrayInputStream parse_inputStream(final String in) throws Exception {
+        final ByteArrayInputStream input = new ByteArrayInputStream(in.getBytes());
+        return input;
+    }
+
+    // String 杞琽utputStream
+    public static ByteArrayOutputStream parse_outputStream(final String in) throws Exception {
+        return parse(parse_inputStream(in));
+    }
+
+    /**
+     * 鏍规嵁byte鏁扮粍锛岀敓鎴愭枃浠�
+     */
+    public static File getFile(byte[] bfile, 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();
+            }
+            file = new File(filePath+"\\"+fileName);
+            fos = new FileOutputStream(file);
+            bos = new BufferedOutputStream(fos);
+            bos.write(bfile);
+        } 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;
+    }
+
+    /**
+     * 鏍规嵁byte鏁扮粍锛岀敓鎴愭枃浠�
+     */
+    public static File getFile(InputStream in, String filePath, String fileName) {
+        if(in == null){
+            return null;
+        }
+        BufferedOutputStream bos = null;
+        FileOutputStream fos = null;
+        File file = null;
+        try {
+            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;
+    }
+
+    /**
+     * 鏍规嵁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){
+        byte[] buffer = null;
+        try {
+            File file = new File(filePath);
+            FileInputStream fis = new FileInputStream(file);
+            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
+            byte[] b = new byte[1000];
+            int n;
+            while ((n = fis.read(b)) != -1) {
+                bos.write(b, 0, n);
+            }
+            fis.close();
+            bos.close();
+            buffer = bos.toByteArray();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return buffer;
+    }
+
+    public static MultipartFile getMultipartFile(File file) {
+        FileItem item = new DiskFileItemFactory().createItem("file"
+                , MediaType.MULTIPART_FORM_DATA_VALUE
+                , true
+                , file.getName());
+        try (InputStream input = new FileInputStream(file);
+             OutputStream os = item.getOutputStream()) {
+            // 娴佽浆绉�
+            IOUtils.copy(input, os);
+        } catch (Exception e) {
+            throw new IllegalArgumentException("Invalid file: " + e, e);
+        }
+
+        return new CommonsMultipartFile(item);
+    }
+
+    /**
+     *  鏍规嵁璺緞鍒犻櫎鎸囧畾鐨勭洰褰曟垨鏂囦欢锛屾棤璁哄瓨鍦ㄤ笌鍚�
+     *@param sPath  瑕佸垹闄ょ殑鐩綍鎴栨枃浠�
+     *@return 鍒犻櫎鎴愬姛杩斿洖 true锛屽惁鍒欒繑鍥� false銆�
+     */
+    public static boolean deleteFolder(String sPath) {
+        File file = new File(sPath);
+        // 鍒ゆ柇鐩綍鎴栨枃浠舵槸鍚﹀瓨鍦�
+        if (!file.exists()) {
+            // 涓嶅瓨鍦ㄨ繑鍥� true
+            return true;
+        } else {
+            // 鍒ゆ柇鏄惁涓烘枃浠�
+            if (file.isFile()) {  // 涓烘枃浠舵椂璋冪敤鍒犻櫎鏂囦欢鏂规硶
+                return deleteFile(sPath);
+            } else {  // 涓虹洰褰曟椂璋冪敤鍒犻櫎鐩綍鏂规硶
+                return deleteDirectory(sPath);
+            }
+        }
+    }
+
+    /**
+     * 鍒犻櫎鍗曚釜鏂囦欢
+     * @param   sPath    琚垹闄ゆ枃浠剁殑鏂囦欢鍚�
+     * @return 鍗曚釜鏂囦欢鍒犻櫎鎴愬姛杩斿洖true锛屽惁鍒欒繑鍥瀎alse
+     */
+    public static boolean deleteFile(String sPath) {
+        File  file = new File(sPath);
+        // 璺緞涓烘枃浠朵笖涓嶄负绌哄垯杩涜鍒犻櫎
+        if (file.isFile() && file.exists()) {
+            file.delete();
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * 鍒犻櫎鐩綍锛堟枃浠跺す锛変互鍙婄洰褰曚笅鐨勬枃浠�
+     * @param   sPath 琚垹闄ょ洰褰曠殑鏂囦欢璺緞
+     * @return  鐩綍鍒犻櫎鎴愬姛杩斿洖true锛屽惁鍒欒繑鍥瀎alse
+     */
+    public static boolean deleteDirectory(String sPath) {
+        //濡傛灉sPath涓嶄互鏂囦欢鍒嗛殧绗︾粨灏撅紝鑷姩娣诲姞鏂囦欢鍒嗛殧绗�
+        if (!sPath.endsWith(File.separator)) {
+            sPath = sPath + File.separator;
+        }
+        File dirFile = new File(sPath);
+        //濡傛灉dir瀵瑰簲鐨勬枃浠朵笉瀛樺湪锛屾垨鑰呬笉鏄竴涓洰褰曪紝鍒欓��鍑�
+        if (!dirFile.exists() || !dirFile.isDirectory()) {
+            return false;
+        }
+        boolean flag = true;
+        //鍒犻櫎鏂囦欢澶逛笅鐨勬墍鏈夋枃浠�(鍖呮嫭瀛愮洰褰�)
+        File[] files = dirFile.listFiles();
+        for (int i = 0; i < files.length; i++) {
+            //鍒犻櫎瀛愭枃浠�
+            if (files[i].isFile()) {
+                flag = deleteFile(files[i].getAbsolutePath());
+                if (!flag) break;
+            } //鍒犻櫎瀛愮洰褰�
+            else {
+                flag = deleteDirectory(files[i].getAbsolutePath());
+                if (!flag) break;
+            }
+        }
+        if (!flag) return false;
+        //鍒犻櫎褰撳墠鐩綍
+        if (dirFile.delete()) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+}

--
Gitblit v1.8.0