需要用"window.location.href"这种get请求方式请求下载方法

解决方案 »

  1.   

    打断点跟踪一下代码,看代码走到哪一步了
    走完 bos.flush(); 这一步,客户端应该出现弹出框
    代码走完,服务器断开与客户端的tcp连接时,客户端开始解析下载文件如果断点没走到后台,那就检查下请求下载的方式,看是否请求方式有问题
      

  2.   

    一楼说的对,不能用ajax等方式去请求  ,这样是不会弹出框的~~~
      

  3.   

    其实你都设置下载流了 ,直接用 response.getOutputStream 得到outputStream然后在这里面写入就可以了 
    1.你到tomcat下面看看文件生成没有
    2.如果没有生成文件就是你的写入有问题
    3.如果你生成了下载的文件 那就是你没有弹出框 建议 使用
    OutputStream out = response.getOutputStream();
    response.reset();
    response.setHeader("content-disposition","attachment;filename=demo.xls");
    response.setContentType("application/msexcel");
    WritableWorkbook wwb = WritableWorkSheet.createWorkBook(out);
    WritableSheet sheet = wwb.createSheet("123",0);
    //写入数据
    out.flush();
    out.close();
    我以前也遇到过,我是用的是 window.location.href  ajax的方法结果不可以 ajax没试过 不知道可以不
      

  4.   

    // 将文件存到指定位置(服务器)
    String path=ConstanData.LOAD_EXCEL;
    try
    {
    File file = new File(path);
    if(!file.getParentFile().exists()){
    file.mkdirs();
    }
    path = path+ "维保订单.xls";
    File file2 = new File(path);
    FileOutputStream fout = new FileOutputStream(file2);
    wb.write(fout);
    fout.close();
    DownloadTool downloadtool = new DownloadTool();
    response = ServletActionContext.getResponse();
    request = ServletActionContext.getRequest();
    // downloadtool.resetResponse(request, response, path);
    downloadtool.download(request, response, path);
    // downloadtool.download2(file2, response);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }//从服务器下载文件到本地机器
    public void download(HttpServletRequest request,HttpServletResponse response,String location) {
            try {
                String fileName = location.substring(location.lastIndexOf("\\")+1);//得到文件名
                File file = new File(location);
                if(file.exists()){
                 ServletOutputStream out = response.getOutputStream();
                response.reset();
                response.setContentType("application/x-download");//设置为下载application/x-download
                fileName=new String(fileName.getBytes("GBK"),"ISO-8859-1");
    //             response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
                response.setHeader("Content-Disposition","attachment;filename=" + fileName.substring(fileName.lastIndexOf("/")+1, fileName.length()));
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                    bis = new BufferedInputStream(new FileInputStream(location));
                    bos = new BufferedOutputStream(out);
                    byte[] buff = new byte[10240];
                    int bytesRead;
                    while (-1 != (bytesRead = bis.read(buff))) {
                      bos.write(buff);
                    }
                    bos.flush();
                  } catch (IOException e) {
                    throw e;
                  } finally {
                    if (bis != null)
                      bis.close();
                    if (bos != null)
                      bos.close();
                  }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
      

  5.   

    http://download.csdn.net/detail/u013112641/7375585  看这个吧