String docname = new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312");
File filePath=null; filePath = new File("/document");
                  if (!filePath.exists()) {
     //输出syso   
                  }     BufferedInputStream br = new BufferedInputStream(new FileInputStream(filePath));
    
     int byteLength = 10; 
     byte[] buf = new byte[byteLength];         
     int fLength = br.available();   
     int times = fLength/byteLength+1;
     int mod = fLength%byteLength; 
         for(int i=1; i<=times; i++){
    while(br.read(buf)!= -1){  
         if(i+1==times){ 
         buf = new byte[mod]; 
         } 
    } 
     }
    
         int len = 0;
     response.reset();
     response.setContentType("application/x-msdownload");
     response.setHeader("Content-Disposition", "attachment; filename="+docname);
    
     OutputStream out = response.getOutputStream();
     while ((len = br.read(buf)) > 0)
     out.write(buf, 0, len);
     br.close();
     out.close();
请大家帮忙看下 这个是可用的  只是 有些地方不知道哪写错了:
1.本地下载弹出来的时候 写的确实是文件名  但迅雷弹出来的是我下载所在的action名 这是为啥?
2.当我读取附件LIST在JS往页面动态写的时候 遇到附件名里有空格的时候 它就停止解析了 导致报错命令未正常结束 这个问 题咋整?希望大家帮帮忙 很急 小弟先在此谢过 祝大家 圣诞愉快!!!!

解决方案 »

  1.   

    response.reset();
    response.setContentType("application/x-download");
    String filedownload = application
    .getRealPath("/download/tklfreesetup.exe");
    String filedisplay = URLEncoder.encode("tklfreesetup.exe", "UTF-8");
    response.addHeader("Content-Disposition", "attachment;filename="
    + filedisplay); java.io.OutputStream outp = null;
    java.io.FileInputStream in = null;
    try
    {
    outp = response.getOutputStream();
    in = new FileInputStream(filedownload); byte[] b = new byte[1024];
    int i = 0; while ((i = in.read(b)) > 0)
    {
    outp.write(b, 0, i);
    }
    //     
    outp.flush();
    out.clear();
    out = pageContext.pushBody();
    }
    catch (Exception e)
    {
    System.out.println("Error!");
    e.printStackTrace();
    }
    finally
    {
    if (in != null)
    {
    in.close();
    in = null;
    }
    }
      

  2.   

    1、因为你请求的就是action....  用ls的方法就好