001package top.cenze.utils.file; 002 003import cn.hutool.core.io.FileUtil; 004import cn.hutool.core.io.resource.ResourceUtil; 005import cn.hutool.core.util.ObjectUtil; 006import cn.hutool.core.util.StrUtil; 007import lombok.SneakyThrows; 008import lombok.extern.slf4j.Slf4j; 009import org.apache.commons.io.FileUtils; 010import org.springframework.web.multipart.MultipartFile; 011import top.cenze.utils.enums.ExcelTypeEnum; 012import top.cenze.utils.enums.ReportFileTypeEnum; 013 014import java.io.*; 015import java.util.UUID; 016 017/** 018 * @desc: 通用文件工具 019 * @author: chengze 020 * @createByDate: 2023/10/12 9:06 021 */ 022@Slf4j 023public class CZFileUtil { 024 private static final String TEMPLATE_DIR = "template/"; 025 private static final String REPORT_DIR = "report/"; 026 027 /** 028 * 获取当前操作系统名称. return 操作系统名称 例如:windows,Linux,Unix等. 029 */ 030 public static String getOSName() { 031 return System.getProperty("os.name").toLowerCase(); 032 } 033 034 /** 035 * 加载文件 036 * @param filePath 037 * @return 038 * @throws FileNotFoundException 039 */ 040 @SneakyThrows 041 public static InputStream readFile(String filePath) { 042 log.info("loadFile filePath: {}", filePath); 043 boolean exist = FileUtil.exist(filePath); 044 log.info("loadFile exist: {}", exist); 045 if (!exist) { 046 throw new Exception("file is not exist!"); 047 } 048 049 return new FileInputStream(filePath); 050 } 051 052 /** 053 * 获取项目resource目录结对路径 054 * @return 055 */ 056 public static String getResourcePath() { 057 String path = ResourceUtil.getResource("").getPath(); 058 log.info("getResourcePath path: {}", path); 059// String path = CZFileUtil.class.getResource("/").getPath(); 060// 061// String osName = getOSName().toLowerCase(); 062// if (StrUtil.isNotEmpty(path) && path.length() > 0) { 063// if ("windows".equals(osName)) { 064// if ("/".equals(path.substring(0, 1))) { 065// path = path.substring(1); 066// } 067// } else { 068// int i = path.indexOf(":"); 069// if (i >= 0) { 070// path = path.substring(i + 1); 071// } 072// } 073// } 074// path = StrUtil.trim(path); 075// 076// log.info("getResourcePath path: {}", path); 077 078 return path; 079 } 080 081 /** 082 * 在项目resource创建文件 083 * 默认目录:template/ 084 * @param directory 085 * @param fileName 086 */ 087 public static File mkFileToResource(String directory, String fileName){ 088 if (StrUtil.isEmpty(directory)) { 089 directory = TEMPLATE_DIR; 090 } 091 log.info("mkFileToResource dir: {}, fileName: {}", directory, fileName); 092 File file = null; 093 094 directory = getResourcePath() + directory; 095 if (!FileUtil.exist(directory)){ 096 FileUtil.mkdir(directory); 097 } 098 099 String filePath = directory + fileName; 100 if(!FileUtil.exist(filePath)){ 101 file = FileUtil.touch(filePath); 102 } else { 103 file = FileUtil.file(filePath); 104 } 105 106 return file; 107 } 108 109 /** 110 * 向项目resource写入文件 111 * @param directory 112 * @param fileName 113 * @param content 114 */ 115 public static void writeFileToResource(String directory, String fileName, String content){ 116 File file = mkFileToResource(directory, fileName); 117 118 writeFileToResource(file, content); 119 } 120 121 /** 122 * 向项目resource写入文件 123 * @param file 124 * @param content 125 */ 126 public static void writeFileToResource(File file, String content){ 127 128 try { 129 FileWriter fw = new FileWriter(file); 130 131 fw.write(content); 132 fw.close(); 133 } catch (IOException e) { 134 throw new RuntimeException(e); 135 } 136 } 137 138 /** 139 * 向项目resource写入文件 140 * @param directory 141 * @param fileName 142 * @param content 143 */ 144 public static void writeFileToResource(String directory, String fileName, byte[] content){ 145 File file = mkFileToResource(directory, fileName); 146 147 writeFileToResource(file, content); 148 } 149 150 /** 151 * 向项目resource写入文件 152 * @param file 153 * @param content 154 */ 155 public static void writeFileToResource(File file, byte[] content){ 156 157 try { 158 // 参数1:文件路径 159 // 参数2:是否追加写入(true为追加,false为覆盖) 160 FileOutputStream fos = new FileOutputStream(file, false); 161 162 fos.write(content); 163 fos.close(); 164 } catch (IOException e) { 165 throw new RuntimeException(e); 166 } 167 } 168 169 /** 170 * 创建一个临时Excel文件 171 * @return 172 */ 173 public static File createExcelTemplateFile() { 174 String templateFileName = UUID.randomUUID().toString().replaceAll("-", "") + ExcelTypeEnum.XLSX.getExtname(); 175 File tmpFile = CZFileUtil.mkFileToResource(TEMPLATE_DIR, templateFileName); 176 if (!FileUtil.exist(tmpFile)) { 177 throw new NullPointerException("创建模板文件失败"); 178 } 179 180 return tmpFile; 181 } 182 183 /** 184 * 创建一个临时Xml文件 185 * @return 186 */ 187 public static File createXmlTemplateFile() { 188 String templateFileName = UUID.randomUUID().toString().replaceAll("-", "") + ".xml"; 189 File tmpFile = CZFileUtil.mkFileToResource(TEMPLATE_DIR, templateFileName); 190 if (!FileUtil.exist(tmpFile)) { 191 throw new NullPointerException("创建模板文件失败"); 192 } 193 194 return tmpFile; 195 } 196 197 /** 198 * 创建一个临时报表文件 199 * @param fileType 200 * @return 201 */ 202 public static File createReportFIle(ReportFileTypeEnum fileType) { 203 String fileName = UUID.randomUUID().toString().replaceAll("-", "") + fileType.getExtname(); 204 File tmpFile = CZFileUtil.mkFileToResource(REPORT_DIR, fileName); 205 if (!FileUtil.exist(tmpFile)) { 206 throw new NullPointerException("创建报表文件失败"); 207 } 208 209 return tmpFile; 210 } 211 212 /** 213 * 创建一个临时pdf文件 214 * @return 215 */ 216 public static File createPdfFile() { 217 String templateFileName = UUID.randomUUID().toString().replaceAll("-", "") + ".pdf"; 218 File tmpFile = CZFileUtil.mkFileToResource(REPORT_DIR, templateFileName); 219 if (!FileUtil.exist(tmpFile)) { 220 throw new NullPointerException("创建PDF文件失败"); 221 } 222 223 return tmpFile; 224 } 225 226 /** 227 * 创建一个临时png文件 228 * @return 229 */ 230 public static File createPngFile() { 231 String templateFileName = UUID.randomUUID().toString().replaceAll("-", "") + ".png"; 232 File tmpFile = CZFileUtil.mkFileToResource(REPORT_DIR, templateFileName); 233 if (!FileUtil.exist(tmpFile)) { 234 throw new NullPointerException("创建PNG文件失败"); 235 } 236 237 return tmpFile; 238 } 239 240 /** 241 * 创建一个临时html文件 242 * @return 243 */ 244 public static File createHtmlFile() { 245 String templateFileName = UUID.randomUUID().toString().replaceAll("-", "") + ".html"; 246 File tmpFile = CZFileUtil.mkFileToResource(REPORT_DIR, templateFileName); 247 if (!FileUtil.exist(tmpFile)) { 248 throw new NullPointerException("创建HTML文件失败"); 249 } 250 251 return tmpFile; 252 } 253 254 /** 255 * 载入一个文件 256 * @param multiFile 257 * @return 258 * @throws IOException 259 */ 260 public static File loadFile(MultipartFile multiFile) throws IOException { 261 if ("".equals(multiFile) || multiFile.getSize() <=0) { 262 return null; 263 } 264 265 File tmpFile = CZFileUtil.mkFileToResource(REPORT_DIR, multiFile.getOriginalFilename()); 266 if (!FileUtil.exist(tmpFile)) { 267 throw new NullPointerException("创建文件失败"); 268 } 269 FileUtils.copyInputStreamToFile(multiFile.getInputStream(), tmpFile); 270 271 return tmpFile; 272 } 273 274 /** 275 * 载入一个文件 276 * @param filePath 277 * @return 278 */ 279 public static File loadFile(String filePath) { 280 if (StrUtil.isEmpty(filePath)) { 281 return null; 282 } 283 284 if (!FileUtil.exist(filePath)) { 285 return null; 286 } 287 288 return FileUtil.file(filePath); 289 } 290 291 /** 292 * 文件转byte[] 293 * @param file 294 * @return 295 */ 296 @SneakyThrows 297 public static byte[] file2Bytes(File file) { 298 if (ObjectUtil.isNull(file)) { 299 return null; 300 } 301 302 FileInputStream fis = new FileInputStream(file); 303 BufferedInputStream bis = new BufferedInputStream(fis); 304 byte[] buf = new byte[bis.available()]; 305 bis.read(buf); 306 bis.close(); 307 308 return buf; 309 } 310}