写道
public void DownloadFileManager(String filename, String url)
throws IOException {
HttpServletResponse response = Struts2Utils.getResponse();
InputStream is = null;
OutputStream os = null;
try {
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("gbk"), "iso-8859-1"));
response.setContentType("application/x-msdownload");
is = new FileInputStream(url);
os = response.getOutputStream();
byte[] buffer = new byte[21940];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
}
}
// 上面代码下载,如果文件名称出现空格,火狐无法下载
throws IOException {
HttpServletResponse response = Struts2Utils.getResponse();
InputStream is = null;
OutputStream os = null;
try {
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("gbk"), "iso-8859-1"));
response.setContentType("application/x-msdownload");
is = new FileInputStream(url);
os = response.getOutputStream();
byte[] buffer = new byte[21940];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
}
}
// 上面代码下载,如果文件名称出现空格,火狐无法下载
解决方法
public void DownloadFileManager(String filename, String url)
throws IOException {
HttpServletResponse response = Struts2Utils.getResponse();
HttpServletRequest request = Struts2Utils.getRequest();
InputStream is = null;
OutputStream os = null;
try {
String agent = (String)request.getHeader("USER-AGENT");
if(agent != null && agent.indexOf("MSIE") == -1) {
String enableFileName = "=?UTF-8?B?" + (new String (Base64.encodeBase64(filename.getBytes("UTF-8")))) + "?=";
response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
}else {
// IE
String enableFileName = new String(filename.getBytes("GBK"), "ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
}
/* response.setHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("gbk"), "iso-8859-1")); */
response.setContentType("application/x-msdownload");
is = new FileInputStream(url);
os = response.getOutputStream();
byte[] buffer = new byte[21940];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
}
}