¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.common.util; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.jeecg.common.api.vo.FileUploadResult; |
| | | import org.jeecg.common.exception.ExceptionCast; |
| | | 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.nio.file.Files; |
| | | import java.nio.file.Paths; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class FileUtil { |
| | | |
| | | private static String fileUploadFolder; |
| | | |
| | | @SuppressWarnings("unused") |
| | | private static String uploadType; |
| | | |
| | | @Value(value = "${jeecg.path.upload}") |
| | | public void setFileUploadFolder(String fileUploadFolder) { |
| | | FileUtil.fileUploadFolder = fileUploadFolder; |
| | | } |
| | | |
| | | @Value(value = "${jeecg.uploadType}") |
| | | public void setUploadType(String uploadType) { |
| | | FileUtil.uploadType = uploadType; |
| | | } |
| | | |
| | | public static FileUploadResult uploadFile(MultipartFile file) { |
| | | if (file == null || file.isEmpty()) |
| | | return null; |
| | | String fileName = file.getOriginalFilename(); |
| | | String suffix = getFileSuffix(fileName); |
| | | Date currentDate = DateUtils.getDate(); |
| | | String monthStr = DateUtils.date2Str(currentDate, DateUtils.monthFormat.get()); |
| | | // ç¸å¯¹è·¯å¾ |
| | | String relativePath = "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/"; |
| | | // ç»å¯¹è·¯å¾ |
| | | String absolutePath = fileUploadFolder + "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/"; |
| | | String fileNameNonSuffix = getFilenameNonSuffix(fileName); |
| | | if (fileNameNonSuffix == null) |
| | | return null; |
| | | // è·åæä»¶å å¯åç§° 1 ä¿è¯æä»¶å¯ä¸ ä¸åå¨è¦çé®é¢ 2 ç®å½ä¸ä¿åæä»¶å å¯åç§° å»é¤æä»¶å
³é®ä¿¡æ¯ |
| | | String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()) + "." + suffix; |
| | | Long fileSize = file.getSize(); |
| | | boolean b = uploadFile(file, absolutePath, encodeFileName); |
| | | if (!b) |
| | | return null; |
| | | FileUploadResult dto = new FileUploadResult(); |
| | | dto.setFileName(fileNameNonSuffix); |
| | | dto.setFileEncodeName(encodeFileName); |
| | | dto.setFilePath(relativePath + encodeFileName); |
| | | dto.setFileSize(fileSize); |
| | | dto.setFileSuffix(suffix); |
| | | return dto; |
| | | } |
| | | |
| | | public static FileUploadResult uploadFile(byte[] fileData, String fileName) { |
| | | String suffix = getFileSuffix(fileName); |
| | | Date currentDate = DateUtils.getDate(); |
| | | String monthStr = DateUtils.date2Str(currentDate, DateUtils.monthFormat.get()); |
| | | // ç¸å¯¹è·¯å¾ |
| | | String relativePath = "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/"; |
| | | // ç»å¯¹è·¯å¾ |
| | | String absolutePath = fileUploadFolder + "/" + monthStr + "/" + DateUtils.getDayStr(currentDate) + "/"; |
| | | String fileNameNonSuffix = getFilenameNonSuffix(fileName); |
| | | if (StringUtils.isBlank(fileNameNonSuffix)) { |
| | | return null; |
| | | } |
| | | // è·åæä»¶å å¯åç§° 1 ä¿è¯æä»¶å¯ä¸ ä¸åå¨è¦çé®é¢ 2 ç®å½ä¸ä¿åæä»¶å å¯åç§° å»é¤æä»¶å
³é®ä¿¡æ¯ |
| | | String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()) + "." + suffix; |
| | | Long fileSize = (long) fileData.length; |
| | | try { |
| | | File targetFile = new File(absolutePath, encodeFileName); |
| | | if(!targetFile.getParentFile().exists()) { |
| | | //å建ç®å½ |
| | | targetFile.getParentFile().mkdirs(); |
| | | } |
| | | Files.write(Paths.get(absolutePath + encodeFileName), fileData); |
| | | FileUploadResult dto = new FileUploadResult(); |
| | | dto.setFileName(fileNameNonSuffix); |
| | | dto.setFileEncodeName(encodeFileName); |
| | | dto.setFilePath(relativePath + encodeFileName); |
| | | dto.setFileSize(fileSize); |
| | | dto.setFileSuffix(suffix); |
| | | return dto; |
| | | } catch (IOException e) { |
| | | log.error("æä»¶åå
¥å¤±è´¥: {}", e.getMessage(), e); |
| | | return null; |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @param filePath è·¯å¾ |
| | | * @param fileName æä»¶å |
| | | */ |
| | | public static void deleteFile(String filePath, String fileName) { |
| | | String absolutePath = fileUploadFolder + "/" + filePath + "/"; |
| | | File targetFile = new File(absolutePath, fileName); |
| | | targetFile.deleteOnExit(); |
| | | } |
| | | |
| | | 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="+ new |
| | | // String(toFileName.getBytes("UTF-8"),"ISO-8859-1")); |
| | | 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); |
| | | } |
| | | os.flush(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (bis != null) { |
| | | try { |
| | | bis.close(); |
| | | } catch (IOException e) { |
| | | } |
| | | } |
| | | if (fis != null) { |
| | | try { |
| | | fis.close(); |
| | | } catch (IOException e) { |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | ExceptionCast.cast("æä»¶ä¸åå¨"); |
| | | } |
| | | } |
| | | |
| | | public static void downLoadFile(HttpServletResponse response, String filePath, String toFileName) { |
| | | String absolutePath = fileUploadFolder + filePath; |
| | | File file = new File(absolutePath); |
| | | 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="+ new |
| | | // String(toFileName.getBytes("UTF-8"),"ISO-8859-1")); |
| | | 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); |
| | | } |
| | | os.flush(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (bis != null) { |
| | | try { |
| | | bis.close(); |
| | | } catch (IOException e) { |
| | | } |
| | | } |
| | | if (fis != null) { |
| | | try { |
| | | fis.close(); |
| | | } catch (IOException e) { |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | ExceptionCast.cast("æä»¶ä¸åå¨"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ä¸ä¼ æä»¶å·¥å
·ç±» |
| | | * |
| | | * @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) { |
| | | e.printStackTrace(); |
| | | 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 List<String> readFile(String fileEncodeName, String filePath) { |
| | | String absolutePath = fileUploadFolder + filePath; |
| | | File file = new File(absolutePath, fileEncodeName); |
| | | if (!file.exists() || file.isDirectory()) |
| | | return null; |
| | | String charset = checkFileEncode(file); |
| | | if (charset == null) |
| | | return null; |
| | | List<String> readList = new ArrayList<>(); |
| | | String tempString = null; |
| | | BufferedReader reader = null; |
| | | try { |
| | | reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); |
| | | while ((tempString = reader.readLine()) != null) { |
| | | readList.add(tempString); |
| | | } |
| | | if (readList.isEmpty()) |
| | | return null; |
| | | return readList; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } finally { |
| | | if (reader != null) { |
| | | try { |
| | | reader.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private 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以ä¸çï¼ä¹ç®æ¯GBK |
| | | 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) { |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |