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 static final char BACKSLASH = '\\';
/**
*
*/
public final static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+"; 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 * byte
* *
@ -105,7 +156,7 @@ public class FileUtils {
*/ */
public static boolean checkAllowDownload (String resource) { public static boolean checkAllowDownload (String resource) {
// 禁止目录上跳级别 // 禁止目录上跳级别
if (StringUtils.contains(resource, "..")) { if (StringUtils.contains(resource, LAST_PATH)) {
return false; return false;
} }
// 判断是否在允许下载的文件规则内 // 判断是否在允许下载的文件规则内
@ -121,16 +172,16 @@ public class FileUtils {
* @return * @return
*/ */
public static String setFileDownloadHeader (HttpServletRequest request, String fileName) { 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; String filename = fileName;
if (agent.contains("MSIE")) { if (agent.contains(MSIE)) {
// IE浏览器 // IE浏览器
filename = URLEncoder.encode(filename, StandardCharsets.UTF_8); filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
filename = filename.replace(ADDITION_STR, EMPTY_STR); 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); 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, StandardCharsets.UTF_8); filename = URLEncoder.encode(filename, StandardCharsets.UTF_8);
} else { } else {
@ -194,16 +245,9 @@ public class FileUtils {
*/ */
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);
String contentDispositionValue = StringUtils.format(CONTENT_DISPOSITION_VALUE_TEMPLATE, percentEncodedFileName, percentEncodedFileName);
String contentDispositionValue = "attachment; filename=" + response.setHeader(CONTENT_DISPOSITION, contentDispositionValue);
percentEncodedFileName + response.setHeader(CONTENT_ENCODING, percentEncodedFileName);
";" +
"filename*=" +
"utf-8''" +
percentEncodedFileName;
response.setHeader("Content-disposition", contentDispositionValue);
response.setHeader("download-filename", percentEncodedFileName);
} }
/** /**
@ -215,6 +259,6 @@ public class FileUtils {
*/ */
public static String percentEncode (String s) { public static String percentEncode (String s) {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8); 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}; TEXT[i] = new char[]{(char) i};
} }
// special HTML characters
// 单引号 // 单引号
TEXT['\''] = "'".toCharArray(); TEXT['\''] = "'".toCharArray();
// 双引号 // 双引号
@ -129,19 +128,11 @@ public class EscapeUtil {
tmp.append(content.substring(lastPos)); tmp.append(content.substring(lastPos));
lastPos = content.length(); lastPos = content.length();
} else { } else {
tmp.append(content.substring(lastPos, pos)); tmp.append(content, lastPos, pos);
lastPos = pos; lastPos = pos;
} }
} }
} }
return tmp.toString(); 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));
}
} }