本帖最后由 liutianxiong888 于 2010-10-07 20:53:42 编辑

解决方案 »

  1.   

    能debug嘛,跟下,应该是数据问题
      

  2.   


    如果是数据问题,就不会是getOutputStream() has already been called for this response
    这个异常了!
      

  3.   


    返回null,是没有错了,但我的问题是怎样弹出那个下载的对话框
      

  4.   

     To: liutianxiong888昨天,我也遇到了与你相同的问题,请问你是怎样解决的?
    地址:http://blog.csdn.net/zhuli_java 或QQ359459774留言,谢谢!!!
      

  5.   

    public class DownloadFile {
    public  static HttpServletResponse download(String path, HttpServletResponse response) {
         try {
             // path是指欲下载的文件的路径。
             File file = new File(path);
             // 取得文件名。
             String filename = file.getName();
             // 以流的形式下载文件。
             InputStream fis = new BufferedInputStream(new FileInputStream(path));
             byte[] buffer = new byte[fis.available()];
             fis.read(buffer);
             fis.close();
             // 清空response
             response.reset();
             // 设置response的Header
             //response.setHeader("Content-disposition","attachment;filename="+"book.zip");  
             response.addHeader("Content-Disposition", "inline;filename=" + new String(filename.getBytes()));
             response.addHeader("Content-Length", "" + file.length());
             OutputStream toClient = new BufferedOutputStream(response.getOutputStream());          
             response.setContentType("application/OCTET-STREAM;charset=gb2312");
             toClient.write(buffer);
             toClient.flush();
             toClient.close();
         } catch (IOException ex) {
             ex.printStackTrace();
         }
         return response;
     }
    }
    调用方法:
    public class EmployeeAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
    ActionContext context=ActionContext.getContext();
    HttpServletRequest request ;
    HttpServletResponse response ;
    public HttpServletResponse getDownloadFile(){
    String str="/Excel/empInfo.xls";
    String path = request.getSession().getServletContext().getRealPath(str);
    return DownloadFiles.download(path, response);
    }
    @Override
    public void setServletRequest(HttpServletRequest request) {
    this.request=request;
    }
    @Override
    public void setServletResponse(HttpServletResponse response) {
    this.response=response;
    }
    }