<%
try {

String url = "D:/aaaa";//文件路径 可以用request.getParameter();取参数
String name = "aaaaaaaaaaaaaaaaaa";//显示给用户看的文件名,即ie弹出下载框中提示保存的文件名
response.reset();
response.setContentType("bin");
response.setHeader("Content-Disposition", "attachment; filename="+new String(name.getBytes(),"iso8859-1"));

ServletOutputStream os = response.getOutputStream();
FileInputStream in = new FileInputStream(url);
byte[] data = new byte[1024];
int temp = -1;
while((temp=in.read(data))!= -1){
os.write(data,0,temp);
os.flush();
}//while//

os.close();
} catch(Exception e) {
out.print(e.toString());
}
%>

解决方案 »

  1.   

    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);
    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("文件下载过程中出现错误! ");
          }
       }
    }
    --------------------
    <%@ page import="java.io.*,SqlData,FileDownLoad,Utility,java.util.*"%><%@ page  language="java"  contentType="text/html;charset=gb2312"%><jsp:useBean id="fload" scope="page" class="FileDownLoad" /><%String username="";if (session.getValue("username")==null){%><script>window.open("/mail/webmail/login.htm","_top");</script><%}else{username=session.getValue("username").toString();}String cd=request.getRequestURI();cd=cd.substring(cd.lastIndexOf('/')+1);String path=application.getRealPath(cd);int lastSlash=path.lastIndexOf('\\');path=path.substring(0,lastSlash);String filename=path+"/mail/affix/"+username+"/"+request.getParameter("affixname");if(filename!=null)  fload.fileDown(filename,response);%>
    ------------------
    <a href="+java.net.URLEncoder.encode(RS.getString("filename"))+">RS.getString("filename")
    download.jsp文件,请不要用硬回车,要一行写下来,修改相应的路经传递给FILEDOWNLOAD类就可以了
      

  2.   

    写连接,不是所有得文件都会提示下载,jpg回提示下载?