fix():格式规范修改

boot3.0
dongzeliang 2025-02-27 11:35:14 +08:00
parent 3f32325c9b
commit a3538fde09
3 changed files with 44 additions and 28 deletions

View File

@ -41,4 +41,25 @@ public class FileConstants {
public static final int PHOTO_BYTE_PNG_TWO = 78; public static final int PHOTO_BYTE_PNG_TWO = 78;
// PNG // PNG
public static final int PHOTO_BYTE_PNG_THREE = 71; 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;
} }

View File

@ -72,14 +72,14 @@ public class FileTypeUtils {
*/ */
public static String getFileExtendName (byte[] photoByte) { public static String getFileExtendName (byte[] photoByte) {
String strFileExtendName = "JPG"; 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) 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[4] == PHOTO_BYTE_GIF_FOUR) || (photoByte[4] == PHOTO_BYTE_GIF_FOUR_OR)) && (photoByte[5] == PHOTO_BYTE_GIF_FIVE)) { && ((photoByte[FOUR_INDEX] == PHOTO_BYTE_GIF_FOUR) || (photoByte[FOUR_INDEX] == PHOTO_BYTE_GIF_FOUR_OR)) && (photoByte[FIVE_INDEX] == PHOTO_BYTE_GIF_FIVE)) {
strFileExtendName = "GIF"; 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"; 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"; 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"; strFileExtendName = "PNG";
} }
return strFileExtendName; return strFileExtendName;

View File

@ -1,6 +1,7 @@
package com.muyu.common.core.utils.file; package com.muyu.common.core.utils.file;
import com.muyu.common.core.utils.StringUtils; import com.muyu.common.core.utils.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
@ -14,6 +15,7 @@ import java.nio.charset.StandardCharsets;
* *
* @author muyu * @author muyu
*/ */
@Log4j2
public class FileUtils { public class FileUtils {
/** /**
* {@code '/'} * {@code '/'}
@ -32,8 +34,6 @@ public class FileUtils {
* *
* @param filePath * @param filePath
* @param os * @param os
*
* @return
*/ */
public static void writeBytes (String filePath, OutputStream os) throws IOException { public static void writeBytes (String filePath, OutputStream os) throws IOException {
FileInputStream fis = null; FileInputStream fis = null;
@ -55,14 +55,14 @@ public class FileUtils {
try { try {
os.close(); os.close();
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); log.error(e1);
} }
} }
if (fis != null) { if (fis != null) {
try { try {
fis.close(); fis.close();
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); log.error(e1);
} }
} }
} }
@ -72,8 +72,6 @@ public class FileUtils {
* *
* *
* @param filePath * @param filePath
*
* @return
*/ */
public static boolean deleteFile (String filePath) { public static boolean deleteFile (String filePath) {
boolean flag = false; boolean flag = false;
@ -120,22 +118,22 @@ public class FileUtils {
* *
* @return * @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"); final String agent = request.getHeader("USER-AGENT");
String filename = fileName; String filename = fileName;
if (agent.contains("MSIE")) { if (agent.contains("MSIE")) {
// IE浏览器 // IE浏览器
filename = URLEncoder.encode(filename, "utf-8"); filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
filename = filename.replace("+", " "); filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) { } 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")) { } else if (agent.contains("Chrome")) {
// google浏览器 // google浏览器
filename = URLEncoder.encode(filename, "utf-8"); filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
} else { } else {
// 其它浏览器 // 其它浏览器
filename = URLEncoder.encode(filename, "utf-8"); filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
} }
return filename; return filename;
} }
@ -191,21 +189,18 @@ public class FileUtils {
* *
* @param response * @param response
* @param realFileName * @param realFileName
*
* @return
*/ */
public static void setAttachmentResponseHeader (HttpServletResponse response, String realFileName) throws UnsupportedEncodingException { public static void setAttachmentResponseHeader (HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
String percentEncodedFileName = percentEncode(realFileName); String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder(); String contentDispositionValue = "attachment; filename=" +
contentDispositionValue.append("attachment; filename=") percentEncodedFileName +
.append(percentEncodedFileName) ";" +
.append(";") "filename*=" +
.append("filename*=") "utf-8''" +
.append("utf-8''") percentEncodedFileName;
.append(percentEncodedFileName);
response.setHeader("Content-disposition", contentDispositionValue.toString()); response.setHeader("Content-disposition", contentDispositionValue);
response.setHeader("download-filename", percentEncodedFileName); response.setHeader("download-filename", percentEncodedFileName);
} }
@ -216,8 +211,8 @@ public class FileUtils {
* *
* @return * @return
*/ */
public static String percentEncode (String s) throws UnsupportedEncodingException { public static String percentEncode (String s) {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); String encode = URLEncoder.encode(s, StandardCharsets.UTF_8);
return encode.replaceAll("\\+", "%20"); return encode.replaceAll("\\+", "%20");
} }
} }