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;
// 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;
}

View File

@ -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;

View File

@ -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");
}
}