diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java b/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java index 95f6d1dc..22cdab23 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java @@ -7,6 +7,7 @@ import cn.hutool.poi.excel.BigExcelWriter; import cn.hutool.poi.excel.ExcelUtil; import me.zhengjie.exception.BadRequestException; import org.springframework.web.multipart.MultipartFile; + import javax.activation.MimetypesFileTypeMap; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; @@ -19,6 +20,7 @@ import java.util.Map; /** * File工具类,扩展 hutool 工具包 + * * @author Zheng Jie * @date 2018-12-27 */ @@ -44,14 +46,15 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { /** * MultipartFile转File + * * @param multipartFile * @return */ - public static File toFile(MultipartFile multipartFile){ + public static File toFile(MultipartFile multipartFile) { // 获取文件名 String fileName = multipartFile.getOriginalFilename(); // 获取文件后缀 - String prefix="."+getExtensionName(fileName); + String prefix = "." + getExtensionName(fileName); File file = null; try { // 用uuid作为文件名,防止生成的临时文件重复 @@ -66,6 +69,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { /** * 删除 + * * @param files */ public static void deleteFile(File... files) { @@ -78,13 +82,14 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { /** * 获取文件扩展名 + * * @param filename * @return */ public static String getExtensionName(String filename) { if ((filename != null) && (filename.length() > 0)) { int dot = filename.lastIndexOf('.'); - if ((dot >-1) && (dot < (filename.length() - 1))) { + if ((dot > -1) && (dot < (filename.length() - 1))) { return filename.substring(dot + 1); } } @@ -93,13 +98,14 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { /** * Java文件操作 获取不带扩展名的文件名 + * * @param filename * @return */ public static String getFileNameNoEx(String filename) { if ((filename != null) && (filename.length() > 0)) { int dot = filename.lastIndexOf('.'); - if ((dot >-1) && (dot < (filename.length()))) { + if ((dot > -1) && (dot < (filename.length()))) { return filename.substring(0, dot); } } @@ -108,10 +114,11 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { /** * 文件大小转换 + * * @param size * @return */ - public static String getSize(long size){ + public static String getSize(long size) { String resultSize = ""; if (size / GB >= 1) { //如果当前Byte的值大于等于1GB @@ -130,12 +137,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { /** * inputStream 转 File + * * @param ins * @param name * @return * @throws Exception */ - public static File inputStreamToFile(InputStream ins, String name) throws Exception{ + public static File inputStreamToFile(InputStream ins, String name) throws Exception { File file = new File(System.getProperty("java.io.tmpdir") + name); if (file.exists()) { return file; @@ -166,13 +174,12 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { String nowStr = "-" + format.format(date); try { String fileName = name + nowStr + "." + suffix; - String path = filePath + fileName; - File dest = new File(path); + String path = filePath + File.separator + fileName; + File dest = new File(path).getCanonicalFile(); // 检测是否存在目录 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs();// 新建文件夹 } - String d = dest.getPath(); file.transferTo(dest);// 文件写入 return dest; } catch (Exception e) { @@ -183,32 +190,33 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { public static String fileToBase64(File file) throws Exception { FileInputStream inputFile = new FileInputStream(file); - String base64 =null; - byte[] buffer = new byte[(int)file.length()]; + String base64 = null; + byte[] buffer = new byte[(int) file.length()]; inputFile.read(buffer); inputFile.close(); - base64=new Base64().encode(buffer); + base64 = new Base64().encode(buffer); String encoded = base64.replaceAll("[\\s*\t\n\r]", ""); return encoded; } /** * 导出excel + * * @param list * @return * @throws Exception */ public static void downloadExcel(List> list, HttpServletResponse response) throws IOException { - String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx"; + String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx"; File file = new File(tempPath); - BigExcelWriter writer= ExcelUtil.getBigWriter(file); + BigExcelWriter writer = ExcelUtil.getBigWriter(file); // 一次性写出内容,使用默认样式,强制输出标题 writer.write(list, true); //response为HttpServletResponse对象 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码 - response.setHeader("Content-Disposition","attachment;filename=file.xlsx"); - ServletOutputStream out=response.getOutputStream(); + response.setHeader("Content-Disposition", "attachment;filename=file.xlsx"); + ServletOutputStream out = response.getOutputStream(); // 终止后删除临时文件 file.deleteOnExit(); writer.flush(out, true); @@ -221,13 +229,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { String music = "mp3 wav wma mpa ram ra aac aif m4a"; String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg"; String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg"; - if(image.indexOf(type) != -1){ + if (image.indexOf(type) != -1) { return "图片"; - } else if(documents.indexOf(type) != -1){ + } else if (documents.indexOf(type) != -1) { return "文档"; - } else if(music.indexOf(type) != -1){ + } else if (music.indexOf(type) != -1) { return "音乐"; - } else if(video.indexOf(type) != -1){ + } else if (video.indexOf(type) != -1) { return "视频"; } else return "其他"; } @@ -236,8 +244,9 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { String mimeType = new MimetypesFileTypeMap().getContentType("." + type); return mimeType.split("\\/")[0]; } + public static void checkSize(long maxSize, long size) { - if(size > (maxSize * 1024 * 1024)){ + if (size > (maxSize * 1024 * 1024)) { throw new BadRequestException("文件超出规定大小"); } }