From 2166c1a14f9629aa49a5f3bb849ce878df4c4892 Mon Sep 17 00:00:00 2001 From: yangbin <yangbin@qq.com> Date: 星期三, 28 八月 2024 17:23:25 +0800 Subject: [PATCH] 2 --- lxzn-module-ai/src/main/java/org/jeecg/modules/utils/file/FileUtils.java | 327 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 327 insertions(+), 0 deletions(-) diff --git a/lxzn-module-ai/src/main/java/org/jeecg/modules/utils/file/FileUtils.java b/lxzn-module-ai/src/main/java/org/jeecg/modules/utils/file/FileUtils.java new file mode 100644 index 0000000..04611c0 --- /dev/null +++ b/lxzn-module-ai/src/main/java/org/jeecg/modules/utils/file/FileUtils.java @@ -0,0 +1,327 @@ +package org.jeecg.modules.utils.file; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.jeecg.common.api.vo.FileUploadResult; +import org.jeecg.common.util.SHA256Util; +import org.jeecg.modules.utils.DateUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.util.Date; + +@Slf4j +@Component +public class FileUtils { + + private static String fileUploadFolder; + + @Value("${jeecg.path.upload}") + public void setFileUploadFolder(String fileUploadFolder) { + FileUtils.fileUploadFolder = fileUploadFolder; + } + + /** + * 鏂囦欢璺緞涓婁紶淇濆瓨 + * @param filePath + * @return + */ + public static FileUploadResult uploadFilePath(String filePath) { + File file = new File(filePath); + if(file == null) { + return null; + } + String fileName = file.getName(); + String suffix = getFileSuffix(fileName); + Date currentDate = DateUtil.getNow(); + String monthStr = DateUtil.format(currentDate, DateUtil.STR_YEAR_MONTH); + //鐩稿璺緞 + String relativePath = "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/"; + //缁濆璺緞 + String absolutePath = fileUploadFolder + "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/"; + String fileNameNonSuffix = getFilenameNonSuffix(fileName); + if(fileNameNonSuffix == null) + return null; + String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()); + if(StringUtils.isNotBlank(suffix)) { + encodeFileName = encodeFileName + "." + suffix; + } + Long b = uploadFileInput(file, absolutePath, encodeFileName); + FileUploadResult dto = new FileUploadResult(); + dto.setFileName(fileNameNonSuffix); + dto.setFilePath(encodeFileName); + dto.setFilePath(relativePath); + dto.setFileSize(b); + dto.setFileSuffix(suffix); + return dto; + } + + /** + * 鏂囦欢File涓婁紶淇濆瓨 + * @param file + * @return + */ + public static FileUploadResult uploadFileInFile(File file) { + if(file == null) { + return null; + } + String fileName = file.getName(); + String suffix = getFileSuffix(fileName); + Date currentDate = DateUtil.getNow(); + String monthStr = DateUtil.format(currentDate, DateUtil.STR_YEAR_MONTH); + //鐩稿璺緞 + String relativePath = "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/"; + //缁濆璺緞 + String absolutePath = fileUploadFolder + "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/"; + String fileNameNonSuffix = getFilenameNonSuffix(fileName); + if(fileNameNonSuffix == null) { + return null; + } + String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()); + if(StringUtils.isNotBlank(suffix)) { + encodeFileName = encodeFileName + "." + suffix; + } + Long b = uploadFileInput(file, absolutePath, encodeFileName); + FileUploadResult dto = new FileUploadResult(); + dto.setFileName(fileNameNonSuffix); + dto.setFileEncodeName(encodeFileName); + dto.setFilePath(relativePath); + dto.setFileSize(b); + dto.setFileSuffix(suffix); + return dto; + } + + + /** + * MultipartFile 鏂瑰紡淇濆瓨 + * @param file + * @return + */ + public static FileUploadResult uploadMultipartFile(MultipartFile file) { + if(file == null || file.isEmpty()) { + return null; + } + String fileName = file.getOriginalFilename(); + String suffix = getFileSuffix(fileName); + Date currentDate = DateUtil.getNow(); + String monthStr = DateUtil.format(currentDate, DateUtil.STR_YEAR_MONTH); + //鐩稿璺緞 + String relativePath = "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/"; + //缁濆璺緞 + String absolutePath = fileUploadFolder + "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/"; + String fileNameNonSuffix = getFilenameNonSuffix(fileName); + if(fileNameNonSuffix == null) + return null; + String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()); + Long fileSize = file.getSize(); + if(StringUtils.isNotBlank(suffix)) { + encodeFileName = encodeFileName + "." + suffix; + } + boolean b = uploadFile(file, absolutePath, encodeFileName); + if(!b) + return null; + FileUploadResult dto = new FileUploadResult(); + dto.setFileName(fileNameNonSuffix); + dto.setFilePath(encodeFileName); + dto.setFilePath(relativePath); + dto.setFileSize(fileSize); + dto.setFileSuffix(suffix); + return dto; + } + + public static void downLoadFile(HttpServletResponse response, String fileName, String filePath, String toFileName) { + String absolutePath = fileUploadFolder + filePath; + File file = new File(absolutePath , fileName); + if(file.exists()) { + byte[] buffer = new byte[1024]; + FileInputStream fis = null; + BufferedInputStream bis = null; + try { + response.setHeader("Content-Type", "application/octet-stream;charset=utf-8"); // 鍛婅瘔娴忚鍣ㄨ緭鍑哄唴瀹逛负娴� + response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(toFileName, "UTF-8")); + fis = new FileInputStream(file); + bis = new BufferedInputStream(fis); + OutputStream os = response.getOutputStream(); + int i = bis.read(buffer); + while (i != -1) { + os.write(buffer, 0, i); + i = bis.read(buffer); + } + }catch (Exception e) { + log.error(e.getMessage(), e.getStackTrace()); + }finally { + if(bis != null) { + try { + bis.close(); + } catch (IOException e) { + } + } + if(fis != null) { + try { + fis.close(); + } catch (IOException e) { + } + } + } + }else { + + } + } + + + + /** + * 涓婁紶鏂囦欢宸ュ叿绫� + * @param file + * @param path + * @param fileNewName 鏂扮殑鏂囦欢鍚� + * @return + */ + public static Long uploadFileInput(File file, String path, String fileNewName) { + File targetFile = new File(path, fileNewName); + if(!targetFile.getParentFile().exists()){ + targetFile.getParentFile().mkdirs(); + } + try { + FileInputStream fis = new FileInputStream(file); + FileOutputStream fos = new FileOutputStream(targetFile); + //FileInputStream fis = new FileInputStream(file); + byte[] bytes = new byte[1024 * 4]; + int index = 0; + long total = 0; + while ((index = fis.read(bytes)) != -1) { + fos.write(bytes, 0, index); + total =+ index; + } + fis.close(); + fos.flush(); + fos.close(); + return total; + } catch (Exception e) { + log.error(e.getMessage(), e.getStackTrace()); + return 0L; + } + } + /** + * 涓婁紶鏂囦欢宸ュ叿绫� + * @param multipartFile + * @param path + * @param fileNewName 鏂扮殑鏂囦欢鍚� + * @return + */ + public static boolean uploadFile(MultipartFile multipartFile, String path, String fileNewName) { + File targetFile = new File(path, fileNewName); + if(!targetFile.getParentFile().exists()){ + targetFile.getParentFile().mkdirs(); + } + try { + multipartFile.transferTo(targetFile); + return true; + } catch (Exception e) { + log.error(e.getMessage(), e.getStackTrace()); + return false; + } + } + + /** + * 鑾峰彇鏂囦欢鍚庣紑 鏃犲悗缂�鏂囦欢杩斿洖NULL + * @param fileName + * @return + */ + public static String getFileSuffix(String fileName) { + if (fileName.contains(".")) { + String suffix = fileName.substring(fileName.lastIndexOf('.') + 1); + return suffix; + }else { + return null; + } + + } + + /** + * 鑾峰彇鏂囦欢鍚� 涓嶅甫鍚庣紑鍜岀偣鍙� + * @param fileName + * @return + */ + public static String getFilenameNonSuffix(String fileName) { + if (fileName.contains(".")) { + String filename = fileName.substring(0, fileName.lastIndexOf('.')); + return filename; + }else { + return fileName; + } + + } + + + + public static String checkFileEncode(File file) { + String charset = "GBK"; + byte[] first3Bytes = new byte[3]; + BufferedInputStream bis = null; + try { + boolean checked = false; + bis = new BufferedInputStream(new FileInputStream(file)); + bis.mark(0); + int read = bis.read(first3Bytes, 0, 3); + if (read == -1) + return charset; + if(first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE ) { + charset = "UTF-16LE"; + checked = true; + }else if ( first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF ) { + charset = "UTF-16BE"; + checked = true; + }else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF ) { + charset = "UTF-8"; + checked = true; + } + bis.reset(); + if (!checked) { + while ((read = bis.read()) != -1 ) { + if ( read >= 0xF0 ) + break; + if ( 0x80 <= read && read <= 0xBF ) // 鍗曠嫭鍑虹幇BF浠ヤ笅鐨勶紝涔熺畻鏄疓BK + break; + if ( 0xC0 <= read && read <= 0xDF ) { + read = bis.read(); + if ( 0x80 <= read && read <= 0xBF ) // 鍙屽瓧鑺� (0xC0 - 0xDF) (0x80 - 0xBF),涔熷彲鑳藉湪GBK缂栫爜鍐吢犅� + continue; + else + break; + } else if ( 0xE0 <= read && read <= 0xEF ) { + // 涔熸湁鍙兘鍑洪敊锛屼絾鏄嚑鐜囪緝灏� + read = bis.read(); + if ( 0x80 <= read && read <= 0xBF ) { + read = bis.read(); + if ( 0x80 <= read && read <= 0xBF ) { + charset = "UTF-8"; + break; + } else + break; + } else { + break; + } + } + } + } + return charset; + }catch (Exception e) { + return null; + }finally { + if(bis != null) { + try { + bis.close(); + } catch (IOException e) { + } + } + } + + } + + +} -- Gitblit v1.9.3