try (OutputStream os = res.getOutputStream()) {
// 判断是否是IE11
Boolean flag = httpRequest.getHeader("User-Agent").indexOf("like Gecko") > 0;
if (httpRequest.getHeader("User-Agent").toLowerCase().indexOf("msie") > 0 || flag) {
fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
} else {
// firefox浏览器
// firefox浏览器User-Agent字符串:
// Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101
// Firefox/36.0
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,
// 这个文件名称用于浏览器的下载框中自动显示的文件名
fileName = new String(fileName.replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1");
}
res.setContentType("application/x-download;charset=utf-8");
res.setHeader("Content-disposition", "attachment; filename=" + fileName);
DownloadUtils.downloadByFileId(fileId, os);
}catch(IOException e){
.........
}
public static void downloadByFileId(String fileId, OutputStream os) throws Exception {
try {
FileInfo downloadInfo = downloadServerClient.download(fileId);
byte[] content = downloadInfo.getContent();
os.write(content, 0, content.length);
os.flush();
os.close();
} catch (Exception e) {
throw e;
}
}