diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/constant/FileConstants.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/constant/FileConstants.java index b60fcdf..a2b8d33 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/constant/FileConstants.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/constant/FileConstants.java @@ -41,4 +41,25 @@ public class FileConstants { public static final int PHOTO_BYTE_PNG_TWO = 78; // PNG public static final int PHOTO_BYTE_PNG_THREE = 71; + + // 下标 0 + public static final int ZERO_INDEX = 0; + // 下标 1 + public static final int ONE_INDEX = 1; + // 下标 2 + public static final int TWO_INDEX = 2; + // 下标 3 + public static final int THREE_INDEX = 3; + // 下标 4 + public static final int FOUR_INDEX = 4; + // 下标 5 + public static final int FIVE_INDEX = 5; + // 下标 6 + public static final int SIX_INDEX = 6; + // 下标 7 + public static final int SEVEN_INDEX = 7; + // 下标 8 + public static final int EIGHT_INDEX = 8; + // 下标 9 + public static final int NINE_INDEX = 9; } diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileTypeUtils.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileTypeUtils.java index c5bad82..4104b86 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileTypeUtils.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileTypeUtils.java @@ -72,14 +72,14 @@ public class FileTypeUtils { */ public static String getFileExtendName (byte[] photoByte) { String strFileExtendName = "JPG"; - if ((photoByte[0] == PHOTO_BYTE_GIF_ZERO) && (photoByte[1] == PHOTO_BYTE_GIF_ONE) && (photoByte[2] == PHOTO_BYTE_GIF_TWO) && (photoByte[3] == PHOTO_BYTE_GIF_THREE) - && ((photoByte[4] == PHOTO_BYTE_GIF_FOUR) || (photoByte[4] == PHOTO_BYTE_GIF_FOUR_OR)) && (photoByte[5] == PHOTO_BYTE_GIF_FIVE)) { + if ((photoByte[ZERO_INDEX] == PHOTO_BYTE_GIF_ZERO) && (photoByte[ONE_INDEX] == PHOTO_BYTE_GIF_ONE) && (photoByte[TWO_INDEX] == PHOTO_BYTE_GIF_TWO) && (photoByte[THREE_INDEX] == PHOTO_BYTE_GIF_THREE) + && ((photoByte[FOUR_INDEX] == PHOTO_BYTE_GIF_FOUR) || (photoByte[FOUR_INDEX] == PHOTO_BYTE_GIF_FOUR_OR)) && (photoByte[FIVE_INDEX] == PHOTO_BYTE_GIF_FIVE)) { strFileExtendName = "GIF"; - } else if ((photoByte[6] == PHOTO_BYTE_JPG_SIX) && (photoByte[7] == PHOTO_BYTE_JPG_SEVEN) && (photoByte[8] == PHOTO_BYTE_JPG_EIGHT) && (photoByte[9] == PHOTO_BYTE_JPG_NINE)) { + } else if ((photoByte[SIX_INDEX] == PHOTO_BYTE_JPG_SIX) && (photoByte[SEVEN_INDEX] == PHOTO_BYTE_JPG_SEVEN) && (photoByte[EIGHT_INDEX] == PHOTO_BYTE_JPG_EIGHT) && (photoByte[NINE_INDEX] == PHOTO_BYTE_JPG_NINE)) { strFileExtendName = "JPG"; - } else if ((photoByte[0] == PHOTO_BYTE_BMP_ZERO) && (photoByte[1] == PHOTO_BYTE_BMP_ONE)) { + } else if ((photoByte[ZERO_INDEX] == PHOTO_BYTE_BMP_ZERO) && (photoByte[ONE_INDEX] == PHOTO_BYTE_BMP_ONE)) { strFileExtendName = "BMP"; - } else if ((photoByte[1] == PHOTO_BYTE_PNG_ONE) && (photoByte[2] == PHOTO_BYTE_PNG_TWO) && (photoByte[3] == PHOTO_BYTE_PNG_THREE)) { + } else if ((photoByte[ONE_INDEX] == PHOTO_BYTE_PNG_ONE) && (photoByte[TWO_INDEX] == PHOTO_BYTE_PNG_TWO) && (photoByte[THREE_INDEX] == PHOTO_BYTE_PNG_THREE)) { strFileExtendName = "PNG"; } return strFileExtendName; diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java index 0d58592..319c3e7 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java @@ -1,6 +1,7 @@ package com.muyu.common.core.utils.file; import com.muyu.common.core.utils.StringUtils; +import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.ArrayUtils; import jakarta.servlet.http.HttpServletRequest; @@ -14,6 +15,7 @@ import java.nio.charset.StandardCharsets; * * @author muyu */ +@Log4j2 public class FileUtils { /** * 字符常量:斜杠 {@code '/'} @@ -32,8 +34,6 @@ public class FileUtils { * * @param filePath 文件路径 * @param os 输出流 - * - * @return */ public static void writeBytes (String filePath, OutputStream os) throws IOException { FileInputStream fis = null; @@ -55,14 +55,14 @@ public class FileUtils { try { os.close(); } catch (IOException e1) { - e1.printStackTrace(); + log.error(e1); } } if (fis != null) { try { fis.close(); } catch (IOException e1) { - e1.printStackTrace(); + log.error(e1); } } } @@ -72,8 +72,6 @@ public class FileUtils { * 删除文件 * * @param filePath 文件 - * - * @return */ public static boolean deleteFile (String filePath) { boolean flag = false; @@ -120,22 +118,22 @@ public class FileUtils { * * @return 编码后的文件名 */ - public static String setFileDownloadHeader (HttpServletRequest request, String fileName) throws UnsupportedEncodingException { + public static String setFileDownloadHeader (HttpServletRequest request, String fileName) { final String agent = request.getHeader("USER-AGENT"); String filename = fileName; if (agent.contains("MSIE")) { // IE浏览器 - filename = URLEncoder.encode(filename, "utf-8"); + filename = URLEncoder.encode(filename, StandardCharsets.UTF_8); filename = filename.replace("+", " "); } else if (agent.contains("Firefox")) { // 火狐浏览器 - filename = new String(fileName.getBytes(), "ISO8859-1"); + filename = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1); } else if (agent.contains("Chrome")) { // google浏览器 - filename = URLEncoder.encode(filename, "utf-8"); + filename = URLEncoder.encode(filename, StandardCharsets.UTF_8); } else { // 其它浏览器 - filename = URLEncoder.encode(filename, "utf-8"); + filename = URLEncoder.encode(filename, StandardCharsets.UTF_8); } return filename; } @@ -191,21 +189,18 @@ public class FileUtils { * * @param response 响应对象 * @param realFileName 真实文件名 - * - * @return */ public static void setAttachmentResponseHeader (HttpServletResponse response, String realFileName) throws UnsupportedEncodingException { String percentEncodedFileName = percentEncode(realFileName); - StringBuilder contentDispositionValue = new StringBuilder(); - contentDispositionValue.append("attachment; filename=") - .append(percentEncodedFileName) - .append(";") - .append("filename*=") - .append("utf-8''") - .append(percentEncodedFileName); + String contentDispositionValue = "attachment; filename=" + + percentEncodedFileName + + ";" + + "filename*=" + + "utf-8''" + + percentEncodedFileName; - response.setHeader("Content-disposition", contentDispositionValue.toString()); + response.setHeader("Content-disposition", contentDispositionValue); response.setHeader("download-filename", percentEncodedFileName); } @@ -216,8 +211,8 @@ public class FileUtils { * * @return 百分号编码后的字符串 */ - public static String percentEncode (String s) throws UnsupportedEncodingException { - String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); + public static String percentEncode (String s) { + String encode = URLEncoder.encode(s, StandardCharsets.UTF_8); return encode.replaceAll("\\+", "%20"); } }