IE6下文件下载不能直接打开,提示‘找不到....'
这是什么原因呢?
请看我的原始代码:
if(!StringUtils.isEmpty(name)){try{String disposition = "attachment;filename="+ new String(name.getBytes("gb2312"),"iso-8859-1")+ "."+suffix;response.addHeader("Content-disposition", disposition);response.addHeader("Content-Length", String.valueOf(attachFile.length()));in = FileUtils.openInputStream(attachFile); ou = response.getOutputStream();IOUtils.copy(in,ou);}catch(Exception e){e.printStackTrace();}finally{IOUtils.closeQuietly(in);IOUtils.closeQuietly(ou);}}只要加上
response.setHeader("Connection", "close");就OK了 1 楼 hemin108 2012-06-29 还是不行 2 楼 从百草园到三味书屋 2012-07-08 hemin108 写道还是不行
那么兄弟,那你找到其他的解决方法了吗?我又遇到了这个问题,但是解决不了哦。 3 楼 hemin108 前天 我这样写ok了
public void writeFileToResponse(Object fileObj,
HttpServletResponse response, String fileName, String contentType) {
if (fileObj == null || response == null
|| !StringUtils.hasText(fileName)
|| !StringUtils.hasText(contentType)) {
logger.error("writeFileToResponse the params error");
return;
}
try {
InputStream is = null;
if (fileObj instanceof InputStream) {
is = (InputStream) fileObj;
} else if (fileObj instanceof File) {
is = new FileInputStream((File) fileObj);
} else if (fileObj instanceof byte[]) {
is = new ByteArrayInputStream((byte[]) fileObj);
} else {
throw new ControllerException(
"writeFileToResponse not support:"
+ fileObj.getClass().getName());
}
//处理在IE下无法下载t从百草园到三味书屋 写道hemin108 写道还是不行
那么兄弟,那你找到其他的解决方法了吗?我又遇到了这个问题,但是解决不了哦。
xt 文件问题
if(contentType.equals("text/plain")){
contentType ="application/octet-stream";
}
response.reset();
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(
response.getOutputStream());
long fileLength = 0;
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
fileLength = fileLength + bytesRead;
bos.write(buff, 0, bytesRead);
}
response.setHeader("Content-Length", String.valueOf(fileLength));
response.setHeader("Connection", "close");
response.flushBuffer();
bis.close();
bos.close();
} catch (Exception e) {
if (e.getClass().getSimpleName().equals("ClientAbortException")) {
logger.warn("Client Abort download File:{}", fileName);
} else {
throw new ControllerException(e);
}
}
}
4 楼 hemin108 前天 public void writeFileToResponse(Object fileObj,
HttpServletResponse response, String fileName, String contentType) {
if (fileObj == null || response == null
|| !StringUtils.hasText(fileName)
|| !StringUtils.hasText(contentType)) {
logger.error("writeFileToResponse the params error");
return;
}
try {
InputStream is = null;
if (fileObj instanceof InputStream) {
is = (InputStream) fileObj;
} else if (fileObj instanceof File) {
is = new FileInputStream((File) fileObj);
} else if (fileObj instanceof byte[]) {
is = new ByteArrayInputStream((byte[]) fileObj);
} else {
throw new ControllerException(
"writeFileToResponse not support:"
+ fileObj.getClass().getName());
}
//处理在IE下无法下载txt 文件问题
if(contentType.equals("text/plain")){
contentType ="application/octet-stream";
}
response.reset();
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(
response.getOutputStream());
long fileLength = 0;
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
fileLength = fileLength + bytesRead;
bos.write(buff, 0, bytesRead);
}
response.setHeader("Content-Length", String.valueOf(fileLength));
response.setHeader("Connection", "close");
response.flushBuffer();
bis.close();
bos.close();
} catch (Exception e) {
if (e.getClass().getSimpleName().equals("ClientAbortException")) {
logger.warn("Client Abort download File:{}", fileName);
} else {
throw new ControllerException(e);
}
}
}