public static void downLoadFileByByte(HttpServletRequest request, HttpServletResponse response, byte[] b, String fileName) throws IOException {
OutputStream outp = response.getOutputStream();
if (b.length > 0) {
response.setContentType("APPLICATION/OCTET-STREAM");
String userAgent = request.getHeader("USER-AGENT");
String finalFileName=null;
try {
if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent, "Trident") || StringUtils.contains(userAgent,"Edge")){//IE 浏览器
finalFileName = URLEncoder.encode(fileName,"UTF8");
}else{//火狐,google等其他浏览器
finalFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + finalFileName + "\"");//\" 解决Firefox下载英文+中文组合的文件名的问题
} catch (UnsupportedEncodingException e) {
}
outp.write(b);
} else {
outp.write("文件不存在!".getBytes("utf-8"));
}
if (outp != null) {
outp.close();
outp = null;
response.flushBuffer();
}
}
public static void downLoadFile(HttpServletRequest request, HttpServletResponse response, String fullPath, String fileName) throws IOException {
OutputStream outp = response.getOutputStream();
File file = new File(fullPath);
if (file.exists()) {
response.setContentType("APPLICATION/OCTET-STREAM");
String userAgent = request.getHeader("USER-AGENT");
String enableFileName = null;
try {
if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent, "Trident") || StringUtils.contains(userAgent,"Edge")){//IE 浏览器
enableFileName = URLEncoder.encode(fileName,"UTF8");
}else{//火狐,google等其他浏览器
enableFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + enableFileName + "\"");//\" 解决Firefox下载英文+中文组合的文件名的问题
} catch (UnsupportedEncodingException e) {
}
FileInputStream in = null;
try {
outp = response.getOutputStream();
in = new FileInputStream(fullPath);
byte[] b = new byte[1024];
boolean var10 = false;
int i;
while((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
} catch (Exception var14) {
var14.printStackTrace();
} finally {
if (in != null) {
in.close();
enableFileName = null;
}
if (outp != null) {
outp.close();
outp = null;
response.flushBuffer();
}
}
} else {
outp.write("File does not exist!".getBytes("utf-8"));
}
}