From ab2f1039329448a9eec10dd33dbf70a3fe9ef7d6 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期一, 18 九月 2023 15:13:05 +0800 Subject: [PATCH] 新版excel优化 --- src/main/java/com/hx/util/FileUtils.java | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 209 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/hx/util/FileUtils.java b/src/main/java/com/hx/util/FileUtils.java index 074f4d1..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")+"/"; @@ -369,12 +444,58 @@ } /** + * 鏂囦欢涓婁紶 + * @param platformIconFile + * @param unifiedFolder + * @param saveFolder + * @param autoDateFolder + * @return + */ + public static String handleOtherFileUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder + ,boolean autoDateFolder) { + String fileName = ""; + try { + if(platformIconFile == null) { + throw new TipsException("璇蜂笂浼犳枃浠讹紒"); + } + + 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; + } + + /** * 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 { @@ -388,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); @@ -405,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; } @@ -464,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