com.feingto.cloud.kit.ServletKit Maven / Gradle / Ivy
package com.feingto.cloud.kit;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* Servlet 工具类.
*
* @author longfei
*/
@Slf4j
public class ServletKit {
/**
* 设置让浏览器弹出下载对话框的Header,不同浏览器使用不同的编码方式.
*
* @param fileName 下载后的文件名.
*/
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response, String fileName) {
final String CONTENT_DISPOSITION = "Content-Disposition";
try {
String agent = request.getHeader("User-Agent");
String encodedfileName = null;
if (null != agent) {
agent = agent.toLowerCase();
if (agent.contains("firefox") || agent.contains("chrome") || agent.contains("safari")) {
encodedfileName = "filename=\"" + new String(fileName.getBytes(), "ISO8859-1") + "\"";
} else if (agent.contains("msie")) {
encodedfileName = "filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"";
} else if (agent.contains("opera")) {
encodedfileName = "filename*=UTF-8\"" + fileName + "\"";
} else {
encodedfileName = "filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"";
}
}
response.setHeader(CONTENT_DISPOSITION, "attachment; " + encodedfileName);
} catch (UnsupportedEncodingException e) {
log.error("不支持的编码异常: {}", e.getMessage());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy