试一试这个import java.io.*;
public class FileDownLoad { public FileDownLoad() {
}
public void fileDown(String aFileName,javax.servlet.http.HttpServletResponse response) throws Exception {
java.io.BufferedInputStream iin;
BufferedOutputStream dout;
try {
File ff=new File(aFileName);
if(!ff.exists()){
throw new Exception("对不起! 您下载的文件不存在");
}else{
byte[] buffer;
int length=(new Long(ff.length())).intValue();
buffer=new byte[length];
try{
iin=new BufferedInputStream(new java.io.FileInputStream(ff));
//设置类型和头信息
response.setContentType( "application/octet-stream" ); // MIME type for pdf doc
int pos=aFileName.lastIndexOf("/");
aFileName=aFileName.substring(pos+1);
System.out.println(aFileName);
response.setHeader("Content-disposition", "attachment; filename="+aFileName);
//传送数据
dout = new BufferedOutputStream( response.getOutputStream());
int once = 0;
int total = 0;
while ((total<length) && (once>=0)) {
once = iin.read(buffer,total,length);
total += once;
dout.write(buffer,0,length);
}
if(iin!=null){ iin.close();}
if(dout!=null){ dout.close();}
}catch(Exception ex){
throw new Exception("文件下载过程中出现错误! ");
}
}
}catch(Exception ex) {
throw new Exception("文件下载过程中出现错误! ");
}
}
}
public class FileDownLoad { public FileDownLoad() {
}
public void fileDown(String aFileName,javax.servlet.http.HttpServletResponse response) throws Exception {
java.io.BufferedInputStream iin;
BufferedOutputStream dout;
try {
File ff=new File(aFileName);
if(!ff.exists()){
throw new Exception("对不起! 您下载的文件不存在");
}else{
byte[] buffer;
int length=(new Long(ff.length())).intValue();
buffer=new byte[length];
try{
iin=new BufferedInputStream(new java.io.FileInputStream(ff));
//设置类型和头信息
response.setContentType( "application/octet-stream" ); // MIME type for pdf doc
int pos=aFileName.lastIndexOf("/");
aFileName=aFileName.substring(pos+1);
System.out.println(aFileName);
response.setHeader("Content-disposition", "attachment; filename="+aFileName);
//传送数据
dout = new BufferedOutputStream( response.getOutputStream());
int once = 0;
int total = 0;
while ((total<length) && (once>=0)) {
once = iin.read(buffer,total,length);
total += once;
dout.write(buffer,0,length);
}
if(iin!=null){ iin.close();}
if(dout!=null){ dout.close();}
}catch(Exception ex){
throw new Exception("文件下载过程中出现错误! ");
}
}
}catch(Exception ex) {
throw new Exception("文件下载过程中出现错误! ");
}
}
}
解决方案 »
- servlet返回list值的双重<c:forEach>循环
- jdbc事务无法由spring控制
- servlet传参数
- 用jsp实现博客群发
- 求远程将ms sql 2000导出excel,以及将excel远程导入数据库的sql代码.
- JSF问题-深圳变成“深圳”
- 求助,关于spring与hibernate整合问题,使用getcurrentsession报错
- 新手,关于synchronized的问题
- apache2.0.45与tomcat4.1.18的整合问题。请注意版本。各位高手
- mybatis一直报语法错误
- 大家来谈谈软件测试的相关问题如何??我感觉这是我们的弱项.例如有什么好方法,好工具??
- HELP!!taglib在weblogic中在线等待!!!!
为什么不是download(URL htmlPage,File saveToFile)呢?