From 96fdb916ed662f5f77ccc0277075858d54ac1334 Mon Sep 17 00:00:00 2001 From: ChenJiaHe <763432473@qq.com> Date: 星期三, 07 四月 2021 18:22:10 +0800 Subject: [PATCH] 新增下载文件方法File --- src/main/java/com/hx/util/FileUtils.java | 137 ++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 126 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..6911a87 100644 --- a/src/main/java/com/hx/util/FileUtils.java +++ b/src/main/java/com/hx/util/FileUtils.java @@ -220,6 +220,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 +303,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 +366,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 +490,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 +508,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); @@ -455,6 +550,26 @@ } /** + 聽聽 聽 * 鍐欐枃浠跺埌褰撳墠鐩綍鐨剈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; + } + + /** * * @param date 鏃堕棿 * @param format 鏃堕棿鏍煎紡 -- Gitblit v1.8.0