fix():格式规范修改

boot3.0
dongzeliang 2025-02-27 16:56:57 +08:00
parent c35dbc58eb
commit 3009c07ed6
2 changed files with 61 additions and 26 deletions

View File

@ -29,8 +29,59 @@ public class FileUtils {
*/
public static final char BACKSLASH = '\\';
/**
*
*/
public final static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
/**
*
*/
private final static String LAST_PATH = "..";
/**
*
*/
private final static String AGENT_KEY = "USER-AGENT";
/**
* IE
*/
private final static String MSIE = "MSIE";
/**
*
*/
private final static String FIREFOX = "Firefox";
/**
*
*/
private final static String CHROME = "Chrome";
/**
*
*/
private final static String CONTENT_DISPOSITION_VALUE_TEMPLATE = "attachment; filename={};filename*=utf-8''{}";
/**
*
*/
private final static String CONTENT_DISPOSITION = "Content-disposition";
/**
*
*/
private final static String CONTENT_ENCODING = "Content-encoding";
/**
*
*/
private final static String PERCENT_SIGN_ENCODE = "\\+";
/**
*
*/
private final static String PERCENT_SIGN_DECODE = "%20";
/**
* byte
*
@ -105,7 +156,7 @@ public class FileUtils {
*/
public static boolean checkAllowDownload (String resource) {
// 禁止目录上跳级别
if (StringUtils.contains(resource, "..")) {
if (StringUtils.contains(resource, LAST_PATH)) {
return false;
}
// 判断是否在允许下载的文件规则内
@ -121,16 +172,16 @@ public class FileUtils {
* @return
*/
public static String setFileDownloadHeader (HttpServletRequest request, String fileName) {
final String agent = request.getHeader("USER-AGENT");
final String agent = request.getHeader(AGENT_KEY);
String filename = fileName;
if (agent.contains("MSIE")) {
if (agent.contains(MSIE)) {
// IE浏览器
filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
filename = filename.replace(ADDITION_STR, EMPTY_STR);
} else if (agent.contains("Firefox")) {
} else if (agent.contains(FIREFOX)) {
// 火狐浏览器
filename = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
} else if (agent.contains("Chrome")) {
} else if (agent.contains(CHROME)) {
// google浏览器
filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
} else {
@ -194,16 +245,9 @@ public class FileUtils {
*/
public static void setAttachmentResponseHeader (HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
String percentEncodedFileName = percentEncode(realFileName);
String contentDispositionValue = "attachment; filename=" +
percentEncodedFileName +
";" +
"filename*=" +
"utf-8''" +
percentEncodedFileName;
response.setHeader("Content-disposition", contentDispositionValue);
response.setHeader("download-filename", percentEncodedFileName);
String contentDispositionValue = StringUtils.format(CONTENT_DISPOSITION_VALUE_TEMPLATE, percentEncodedFileName, percentEncodedFileName);
response.setHeader(CONTENT_DISPOSITION, contentDispositionValue);
response.setHeader(CONTENT_ENCODING, percentEncodedFileName);
}
/**
@ -215,6 +259,6 @@ public class FileUtils {
*/
public static String percentEncode (String s) {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8);
return encode.replaceAll("\\+", "%20");
return encode.replaceAll(PERCENT_SIGN_ENCODE, PERCENT_SIGN_DECODE);
}
}

View File

@ -17,7 +17,6 @@ public class EscapeUtil {
TEXT[i] = new char[]{(char) i};
}
// special HTML characters
// 单引号
TEXT['\''] = "'".toCharArray();
// 双引号
@ -129,19 +128,11 @@ public class EscapeUtil {
tmp.append(content.substring(lastPos));
lastPos = content.length();
} else {
tmp.append(content.substring(lastPos, pos));
tmp.append(content, lastPos, pos);
lastPos = pos;
}
}
}
return tmp.toString();
}
public static void main (String[] args) {
String html = "<script>alert(1);</script>";
String escape = EscapeUtil.escape(html);
System.out.println("clean: " + EscapeUtil.clean(html));
System.out.println("escape: " + escape);
System.out.println("unescape: " + EscapeUtil.unescape(escape));
}
}