试一试这个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("文件下载过程中出现错误! ");
}
}
}
解决方案 »
- rs.next()为空,求救!谢谢
- prepareCall 和 prepareStatement 有什么区别?
- 在线跪等解决方法:struts的
- 用history.back()两级连动combox数据丢失,在线等候,立刻给分
- 新手的问题,请大家帮忙呀????????
- 如何判断访问的用户是哪里的 .......
- jsp 如何访问扩展名为 ‘.properties’ 属性文件?请介绍大体思路··
- 请教一个关于tomcat编码,websphere,sql server 2000编码的问题!内详细叙述
- 做了一个表单。只有两个输入框。点提交。出现一个错误:405 POST not supported,怎么办
- 在jsp中使用javabean的问题!
- 大家来谈谈软件测试的相关问题如何??我感觉这是我们的弱项.例如有什么好方法,好工具??
- HELP!!taglib在weblogic中在线等待!!!!
为什么不是download(URL htmlPage,File saveToFile)呢?