用JSP做的下载,基本设置如下String realPath = request.getSession().getServletContext().getRealPath("/")+fileName;
File file = new File(realPath);
if(file.isFile()){
String filename = file.getName();
System.out.println(filename);
int typIn=filename.lastIndexOf(".");
// System.out.println(typIn);
String xx=filename.substring(typIn+1);
// System.out.println(xx+"&&&&&&&");
//设置response的编码方式
//response.setContentType("application/x-msdownload");
response.setContentType("APPLICATION/OCTET-STREAM");
//response.setContentType("text/plain");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition","attachment;filename="+new String(fileRealName.getBytes("gbk"),"iso-8859-1"));
//设置附加文件名
/*if("doc".equals(xx)){
response.setHeader("Content-Disposition","inline;filename="+new String(fileRealName.getBytes("gbk"),"iso-8859-1"));
}else{
response.setHeader("Content-Disposition","attachment;filename="+new String(fileRealName.getBytes("gbk"),"iso-8859-1"));
}*/
FileInputStream fis=null;
BufferedInputStream buff=null;
OutputStream myout=null; fis=new FileInputStream(file);
buff=new BufferedInputStream(fis);
//读出文件到i/o流
byte [] b=new byte[1024];//相当于我们的缓存
long k=0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
myout=response.getOutputStream();
//开始循环下载 while(k<file.length()){
int j=buff.read(b,0,1024);
k+=j; //将b中的数据写到客户端的内存
myout.write(b,0,j);
}
myout.flush();
}else{
response.getWriter().print("<script>alert('文件不存在!');history.go(-1);</script>");
}
问题:点击后,弹出下载框 ,有3个按钮 打开,保存,取消 其中,保存及取消,都正常,但直接点打开时,除WORD能直接打开外,其它格式都打不开,主要解决TXT的,提示 XX路径下的文件不存在,问是否要创建,但不管怎么点,文件是打开了,但没有内容。
此问题在360下可以解决,IE8下可以解决,我的是IE6,肯定不是浏览器的问题,因为我用邮箱,可以直接打开。
File file = new File(realPath);
if(file.isFile()){
String filename = file.getName();
System.out.println(filename);
int typIn=filename.lastIndexOf(".");
// System.out.println(typIn);
String xx=filename.substring(typIn+1);
// System.out.println(xx+"&&&&&&&");
//设置response的编码方式
//response.setContentType("application/x-msdownload");
response.setContentType("APPLICATION/OCTET-STREAM");
//response.setContentType("text/plain");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition","attachment;filename="+new String(fileRealName.getBytes("gbk"),"iso-8859-1"));
//设置附加文件名
/*if("doc".equals(xx)){
response.setHeader("Content-Disposition","inline;filename="+new String(fileRealName.getBytes("gbk"),"iso-8859-1"));
}else{
response.setHeader("Content-Disposition","attachment;filename="+new String(fileRealName.getBytes("gbk"),"iso-8859-1"));
}*/
FileInputStream fis=null;
BufferedInputStream buff=null;
OutputStream myout=null; fis=new FileInputStream(file);
buff=new BufferedInputStream(fis);
//读出文件到i/o流
byte [] b=new byte[1024];//相当于我们的缓存
long k=0;//该值用于计算当前实际下载了多少字节
//从response对象中得到输出流,准备下载
myout=response.getOutputStream();
//开始循环下载 while(k<file.length()){
int j=buff.read(b,0,1024);
k+=j; //将b中的数据写到客户端的内存
myout.write(b,0,j);
}
myout.flush();
}else{
response.getWriter().print("<script>alert('文件不存在!');history.go(-1);</script>");
}
问题:点击后,弹出下载框 ,有3个按钮 打开,保存,取消 其中,保存及取消,都正常,但直接点打开时,除WORD能直接打开外,其它格式都打不开,主要解决TXT的,提示 XX路径下的文件不存在,问是否要创建,但不管怎么点,文件是打开了,但没有内容。
此问题在360下可以解决,IE8下可以解决,我的是IE6,肯定不是浏览器的问题,因为我用邮箱,可以直接打开。
当你header啥啥什么都不设置的时候,利用response.getOutputStream()写进去的流都会直接用浏览器显示
若想达到其他效果,如弹出下载那就是
response.setContentType("application/x-msdownload");
如果你写了
有没有必要把response.setContentType("APPLICATION/OCTET-STREAM");
写在此处呢?没必要,请使用xml配置某些格式后缀名对应APPLICATION/OCTET-STREAM就行,对于tomcat,就是MIME得设置技巧