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(文件大小,单位为B) * @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; } }