java excel下载代码_[代码全屏查看]-java下载excel,保证ie和非ie的中文文件名都没有乱码...
[1].[代码] [Java]代码
public static void downloadExcelFile(HttpServletRequest request, HttpServletResponse response, String fileName,
String fileDownloadPath) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
String userAgent = request.getHeader("user-agent");
String downLoadFileName = "";
if (StringUtils.isBlank(userAgent)) {
downLoadFileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
} else {
if (userAgent.toLowerCase().contains("msie") || userAgent.toLowerCase().contains("trident")) {
downLoadFileName = URLEncoder.encode(fileName, "UTF-8");
} else {
downLoadFileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
}
}
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
long fileLength = new File(fileDownloadPath).length();
response.setContentType("application/msexcel");
response.setHeader("Content-disposition", "attachment; filename="
+ downLoadFileName);
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(fileDownloadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
}
IOUtils.closeQuietly(bos);
IOUtils.closeQuietly(bis);
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
