验证中...
gistfile1.txt
Raw Copy
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
fileName = fileNeedName;
InputStream is = new FileInputStream(file);
OutputStream osf = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(osf);
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("Edge")) {
logger.info("Edge");
fileName = URLEncoder.encode(fileName, "UTF-8");
fileName = fileName.replace("+", "%20");
}else if (userAgent.contains("Chrome")) {
logger.info("Chrome");
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}else if (userAgent.contains("Firefox")) {
logger.info("Firefox");
fileName = fileName.split("\\.")[0];//解决火狐特有的后缀
fileName = "=?utf-8?b?"+new BASE64Encoder().encode(fileName.getBytes("utf-8"))+"?=";
}else if (userAgent.contains("Trident")) {
logger.info("IE");
fileName = URLEncoder.encode(fileName, "UTF-8");
fileName = fileName.replace("+", "%20");
}else {
logger.info("Others");
fileName = new String(fileName.getBytes("gbk"),"ISO8859-1");
}
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/ vnd.ms-excel");// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.flush();
bis.close();
bos.close();
is.close();
osf.close();

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025