From 274d27015f46a692015dbe0dfc8f8a453df3a2c2 Mon Sep 17 00:00:00 2001 From: lyh <925863403@qq.com> Date: 星期三, 25 六月 2025 14:07:14 +0800 Subject: [PATCH] 同步工控网 --- lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/util/FileUtils.java | 194 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 194 insertions(+), 0 deletions(-) diff --git a/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/util/FileUtils.java b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/util/FileUtils.java new file mode 100644 index 0000000..ba6c022 --- /dev/null +++ b/lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/util/FileUtils.java @@ -0,0 +1,194 @@ +package org.jeecg.modules.system.util; + +import cn.hutool.core.date.DateUtil; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.math.BigDecimal; + +/** + * Created by YangBin on 2017/9/15. + */ +public class FileUtils { + private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; + + /** + * 鑾峰彇鏈嶅姟鍣ㄤ复鏃惰矾寰� + * + * @param request + * @return + */ + public static String serverTempPath(HttpServletRequest request) { + String fsep = System.getProperty("file.separator"); + String path = request.getSession().getServletContext().getRealPath(""); + if (path.lastIndexOf(fsep) == path.length() - 1) { + path += "upload" + fsep + "voucher" + fsep; + } else { + path += fsep + "upload" + fsep + "voucher" + fsep; + } + return path; + } + + public static String serverTempPathIntactReg(HttpServletRequest request) { + String fsep = System.getProperty("file.separator"); + String path = request.getSession().getServletContext().getRealPath(""); + if (path.lastIndexOf(fsep) == path.length() - 1) { + path += "upload" + fsep + "log" + fsep + "error" + fsep; + } else { + path += fsep + "upload" + fsep + "log" + fsep + "error" + fsep; + } + return path; + } + + public static String serverTempPathProcess(HttpServletRequest request) { + String fsep = System.getProperty("file.separator"); + String path = request.getSession().getServletContext().getRealPath(""); + if (path.lastIndexOf(fsep) == path.length() - 1) { + path += "upload" + fsep + "process" + fsep; + } else { + path += fsep + "upload" + fsep + "process" + fsep; + } + return path; + } + + public static String changeFileFormatKb(String flow) { + BigDecimal flows = new BigDecimal(flow); + if (flows.compareTo(new BigDecimal(0)) > 0 && flows.compareTo(new BigDecimal(1024)) < 0) {//灏忎簬1M + return flows.toString() + "B"; + } else if (flows.compareTo(new BigDecimal(1024)) >= 0) { + BigDecimal result = flows.divide(new BigDecimal(1024), 2, BigDecimal.ROUND_HALF_UP); + return result.toString() + "KB"; + } else { + return "0"; + } + } + + + /** + * 寰楀埌椤圭洰鏍圭洰褰曚笅鐨勭粷瀵硅矾寰勶紙纾佺洏鐨勭墿鐞嗚矾寰勶級 + * + * @param request + * @param newPath + * @return + */ + public static String getFilePath(HttpServletRequest request, String newPath) { + String fsep = System.getProperty("file.separator"); + String path = request.getSession().getServletContext().getRealPath("/upload"); + path += fsep + newPath; + return path; + } + + /** + * 寰楀埌椤圭洰鏍圭洰褰曚笅鐨勭浉瀵硅矾寰� 锛堢浉瀵逛簬椤圭洰涓烘牴璺緞锛� + * + * @param newPath + * @return + */ + public static String getRelativePath(String newPath) { + return "/upload/" + newPath; + } + + + /** + * 鏂规硶涓�:浣跨敤 FileWriter 鍐欐枃浠� + * + * @param filepath 鏂囦欢鐩綍 + * @param content 寰呭啓鍏ュ唴瀹� + * @throws IOException + */ + public static void fileWriterSql(String filepath, String content) throws IOException { + OutputStreamWriter outputStreamWriter = null; + try { + File file = new File(filepath); + if (!file.exists()) { + file.createNewFile(); + } + FileOutputStream outputStream = new FileOutputStream(file); + if (outputStream != null) { + outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8"); + outputStreamWriter.write(content); + outputStreamWriter.flush(); + } + } catch (IOException e) { + e.getMessage(); + } finally { + try { + if (outputStreamWriter != null) { + outputStreamWriter.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + /* + File file=new File(filepath); + Writer writer = new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile()), "UTF-8"); + writer.append(content); + */ + } + + /** + * 鏂囦欢澶у皬鏅鸿兘杞崲 + * 浼氬皢鏂囦欢澶у皬杞崲涓烘渶澶ф弧瓒冲崟浣� + * + * @param size锛堟枃浠跺ぇ灏忥紝鍗曚綅涓築锛� + * @return 鏂囦欢澶у皬 + */ + public static String formatFileSize(Long size) { + String sizeName = null; + if (1024 * 1024 > size && size >= 1024) { + sizeName = String.format("%.2f", size.doubleValue() / 1024) + "KB"; + } else if (1024 * 1024 * 1024 > size && size >= 1024 * 1024) { + sizeName = String.format("%.2f", size.doubleValue() / (1024 * 1024)) + "MB"; + } else if (size >= 1024 * 1024 * 1024) { + sizeName = String.format("%.2f", size.doubleValue() / (1024 * 1024 * 1024)) + "GB"; + } else { + sizeName = size.toString() + "B"; + } + return sizeName; + } + + /** + * 涓婁紶鏂囦欢宸ュ叿绫� + * + * @param userPic + * @param path + * @param fileName + * @return + */ + public static boolean uploadFile(MultipartFile userPic, String path, String fileName) { + Long fileSize = userPic.getSize(); + if (fileSize > MAX_FILE_SIZE) { + return false; + } + File targetFile = new File(path, fileName); + if (!targetFile.exists()) { + targetFile.mkdirs(); + } + try { + userPic.transferTo(targetFile); + return true; + } catch (Exception e) { + return false; + } + } + + /** + * 閲嶅懡鍚嶆枃浠跺悕 鍔犲叆鏃堕棿鎴� + * + * @param fileName + * @return + */ + public static String newFileName(String fileName) { + String suffix = fileName.substring(fileName.lastIndexOf('.')); + String dateStr = "_" + DateUtil.now(); + String nameFile = fileName.substring(0, fileName.indexOf(".")); + //鏂版枃浠跺悕 + return nameFile + dateStr + suffix; + } +} -- Gitblit v1.9.3