From 6b6bf55bd752a477643c798d9e9bba36bef277ef Mon Sep 17 00:00:00 2001
From: wangrenhuang <wangren_huang@163.com>
Date: 星期三, 31 八月 2022 19:30:03 +0800
Subject: [PATCH] 导出文件工具类优化

---
 src/main/java/com/hx/util/FileUtils.java |  174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 163 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/hx/util/FileUtils.java b/src/main/java/com/hx/util/FileUtils.java
index 1de2fa1..7da1769 100644
--- a/src/main/java/com/hx/util/FileUtils.java
+++ b/src/main/java/com/hx/util/FileUtils.java
@@ -7,6 +7,7 @@
 
 import java.io.*;
 import java.net.FileNameMap;
+import java.net.URL;
 import java.net.URLConnection;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -220,6 +221,29 @@
         }
     }
 
+    /**
+     *  2020-06-29
+     *  cjh
+     * 鍥剧墖鏍煎紡鍒ゆ柇
+     * */
+    public static boolean imageFormatJudge(File firs) {
+        String imageName = firs.getName();
+        //鎴彇鏍煎紡
+        String suffix =imageName.substring(imageName.lastIndexOf(".") + 1);
+        //鏍煎紡瀛楁瘝杞皬鍐�
+        suffix = suffix.toLowerCase();
+        //杩涜鍒ゆ柇
+        if(suffix.equals("png")) {
+            return true;
+        }else if(suffix.equals("jpg")){
+            return true;
+        }else if(suffix.equals("jpeg")){
+            return true;
+        }else {
+            return false;
+        }
+    }
+
     /**瑙嗛涓婁紶鐨勬柟娉�
      * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨�
      * @param platformIconFile 瑙嗛鏂囦欢
@@ -280,6 +304,57 @@
      * @return 鍥剧墖璺緞
      * 2020-06-29 ChenJiaHe
      */
+    public static String handleFileUpload(File platformIconFile,String unifiedFolder,String saveFolder
+            ,boolean autoDateFolder) {
+        String fileName = "";
+        try {
+            if(platformIconFile == null) {
+                throw new TipsException("璇蜂笂浼犲浘鐗囷紒");
+            }
+            if(!imageFormatJudge(platformIconFile)) {
+                throw new TipsException("璇蜂笂浼爌ng銆乯pg鍜宩peg鏍煎紡鐨勫浘鐗囷紒");
+            }
+
+            //璁剧疆鍥剧墖澶у皬
+            // String.format("%.1f",platformIconFile.getSize()/1024.0);
+            if(autoDateFolder){
+                if(saveFolder.endsWith("/")){
+                    saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/";
+                }else{
+                    saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/";
+                }
+            }
+            fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS");
+            if(unifiedFolder.endsWith("/")){
+                if(saveFolder.startsWith("/")){
+                    saveFolder = saveFolder.replaceFirst("/","");
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+saveFolder;
+                }
+            }else{
+                if(saveFolder.startsWith("/")){
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+"/"+saveFolder;
+                }
+            }
+            fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName);
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+        }
+        return fileName;
+    }
+
+    /**鍥剧墖涓婁紶鐨勬柟娉�
+     * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨�
+     * @param platformIconFile 鍥剧墖鏂囦欢
+     * @param unifiedFolder NG鎸囧悜鐨勫墠绔枃浠跺す(缁熶竴鏂囦欢澶�)锛屽锛歶ser/local/images/
+     * @param saveFolder 淇濆瓨鍒扮殑鏂囦欢澶�,濡傦細/bananer/
+     * @param autoDateFolder 鏄惁鐢熸垚鏃ユ湡鏂囦欢澶�
+     * @return 鍥剧墖璺緞
+     * 2020-06-29 ChenJiaHe
+     */
     public static String handleFileUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder
             ,boolean autoDateFolder) {
         String fileName = "";
@@ -292,7 +367,7 @@
             }
 
             //璁剧疆鍥剧墖澶у皬
-           // String.format("%.1f",platformIconFile.getSize()/1024.0);
+            // String.format("%.1f",platformIconFile.getSize()/1024.0);
             if(autoDateFolder){
                 if(saveFolder.endsWith("/")){
                     saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/";
@@ -416,11 +491,11 @@
 
     /**
      * 2020-06-29 ChenJiaHe
-聽聽 聽 * @param file 聽聽 聽聽聽 聽聽聽 聽//鏂囦欢瀵硅薄
-聽聽 聽 * @param filePath聽聽 聽聽聽 聽//涓婁紶璺緞
-聽聽 聽 * @param fileName聽聽 聽聽聽 聽//鏂囦欢鍚�
-聽聽 聽 * @return聽 鏂囦欢鍚�
-聽聽 聽 */
+     聽聽 聽 * @param file 聽聽 聽聽聽 聽聽聽 聽//鏂囦欢瀵硅薄
+     聽聽 聽 * @param filePath聽聽 聽聽聽 聽//涓婁紶璺緞
+     聽聽 聽 * @param fileName聽聽 聽聽聽 聽//鏂囦欢鍚�
+     聽聽 聽 * @return聽 鏂囦欢鍚�
+     聽聽 聽 */
     public static String fileUp(MultipartFile file, String filePath, String fileName){
         String extName = ""; // 鎵╁睍鍚嶆牸寮忥細
         try {
@@ -434,13 +509,34 @@
         return fileName+extName;
     }
 
+
     /**
-     聽聽 聽 * 鍐欐枃浠跺埌褰撳墠鐩綍鐨剈pload鐩綍涓�
-     聽聽 聽 *
-     聽聽 聽 * @param in
-     聽聽 聽 * @param fileName
-     聽聽 聽 * @throws IOException
+     * 2020-06-29 ChenJiaHe
+     聽聽 聽 * @param file 聽聽 聽聽聽 聽聽聽 聽//鏂囦欢瀵硅薄
+     聽聽 聽 * @param filePath聽聽 聽聽聽 聽//涓婁紶璺緞
+     聽聽 聽 * @param fileName聽聽 聽聽聽 聽//鏂囦欢鍚�
+     聽聽 聽 * @return聽 鏂囦欢鍚�
      聽聽 聽 */
+    public static String fileUp(File file, String filePath, String fileName){
+        String extName = ""; // 鎵╁睍鍚嶆牸寮忥細
+        try {
+            if (file.getName().lastIndexOf(".") >= 0){
+                extName = file.getName().substring(file.getName().lastIndexOf("."));
+            }
+            copyFile(file, filePath, fileName+extName).replaceAll("-", "");
+        } catch (IOException e) {
+            System.out.println(e);
+        }
+        return fileName+extName;
+    }
+
+    /**
+ 聽聽 聽 * 鍐欐枃浠跺埌褰撳墠鐩綍鐨剈pload鐩綍涓�
+ 聽聽 聽 *
+ 聽聽 聽 * @param in
+ 聽聽 聽 * @param fileName
+ 聽聽 聽 * @throws IOException
+ 聽聽 聽 */
     private static String copyFile(InputStream in, String dir, String realName)throws IOException {
         File file = new File(dir, realName);
         file.setWritable(true);
@@ -451,6 +547,26 @@
             file.createNewFile();
         }
         org.apache.commons.io.FileUtils.copyInputStreamToFile(in, file);
+        return realName;
+    }
+
+    /**
+     聽聽 聽 * 鍐欐枃浠跺埌褰撳墠鐩綍鐨剈pload鐩綍涓�
+     聽聽 聽 *
+     聽聽 聽 * @param in
+     聽聽 聽 * @param fileName
+     聽聽 聽 * @throws IOException
+     聽聽 聽 */
+    private static String copyFile(File fileIn, String dir, String realName)throws IOException {
+        File file = new File(dir, realName);
+        file.setWritable(true);
+        if (!file.exists()) {
+            if (!file.getParentFile().exists()) {
+                file.getParentFile().mkdirs();
+            }
+            file.createNewFile();
+        }
+        org.apache.commons.io.FileUtils.copyFile(fileIn,file);
         return realName;
     }
 
@@ -510,5 +626,41 @@
         return type;
     }
 
+    public static File httpUrlFile(String netUrl) throws IOException {
+        File file = File.createTempFile("temp123", ".xls");
+        InputStream inStream = null;
+        FileOutputStream os = null;
+
+        try {
+            file = File.createTempFile("net_url", ".jpg");
+            URL urlfile = new URL(netUrl);
+            inStream = urlfile.openStream();
+            os = new FileOutputStream(file);
+
+            byte[] buffer = new byte[8192];
+
+            int bytesRead;
+            while((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
+                os.write(buffer, 0, bytesRead);
+            }
+        } catch (Exception var15) {
+            var15.printStackTrace();
+        } finally {
+            try {
+                if (null != os) {
+                    os.close();
+                }
+
+                if (null != inStream) {
+                    inStream.close();
+                }
+            } catch (Exception var14) {
+                var14.printStackTrace();
+            }
+
+        }
+
+        return file;
+    }
 
 }

--
Gitblit v1.8.0