fix():格式规范修改
parent
c35dbc58eb
commit
3009c07ed6
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue