【java】 java 文件下载头消息

原始头信息
Content-Disposition    attachment;filename=苏州乡村高尔夫针织纤维有限责任公司.pdf
Content-Length    49292
Content-Type    application/txt;charset=UTF-8
Date    Fri, 13 Feb 2015 03:03:09 GMT
Server    Apache-Coyote/1.1
原始头信息
Accept    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding    gzip, deflate
Accept-Language    zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Connection    keep-alive
Cookie    JSESSIONID=B29B30E6E6A0B3A72297D01912242B78
Host    localhost:8282
Referer    http://localhost:8282/InfomationServiceSystem/credit/acrossApply/acrossApply.action
User-Agent    Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0 FirePHP/0.7.4
x-insight    activate

以上头消息可以正常弹出下载框,一下的消息头出现异常:

使用jquery的$.post来下载文件,代码如下:

$.post("${pageContext.request.contextPath}/credit/report/geneReport.action",
                  {companys:info},
                  function(data){
                    //  alert(data.result);
               },"json");

 


原始头信息
Content-Disposition    attachment;filename=20150213.zip
Content-Length    176718
Content-Type    application/txt
Date    Fri, 13 Feb 2015 03:06:22 GMT
Server    Apache-Coyote/1.1
原始头信息
Accept    application/json, text/javascript, */*; q=0.01
Accept-Encoding    gzip, deflate
Accept-Language    zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control    no-cache
Connection    keep-alive
Content-Length    258
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie    JSESSIONID=B29B30E6E6A0B3A72297D01912242B78
Host    localhost:8282
Pragma    no-cache
Referer    http://localhost:8282/InfomationServiceSystem/credit/report/toSingleQueryReport.action
User-Agent    Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0 FirePHP/0.7.4
X-Requested-With    XMLHttpRequest
x-insight    activate

 

jquery 代码为:

$.post("${pageContext.request.contextPath}/credit/report/geneReport.action",
                  {companys:info},
                  function(data){
                    //  alert(data.result);
               });

 

原始头信息
Content-Disposition    attachment;filename=20150213.zip
Content-Length    260704
Content-Type    application/octet-stream
Date    Fri, 13 Feb 2015 03:21:27 GMT
Server    Apache-Coyote/1.1
原始头信息
Accept    */*
Accept-Encoding    gzip, deflate
Accept-Language    zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control    no-cache
Connection    keep-alive
Content-Length    276
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie    JSESSIONID=D23C5D6E287862E89554C9A4894F8717
Host    localhost:8282
Pragma    no-cache
Referer    http://localhost:8282/InfomationServiceSystem/credit/report/toSingleQueryReport.action
User-Agent    Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0 FirePHP/0.7.4
X-Requested-With    XMLHttpRequest
x-insight    activate

 

一般下载文件用js代码直接实现即可。

document.location.href="download.html";

 

java文件下载的代码:

/**
     * 生成审核报告
     * @throws Exception 
     * @throws Exception 
     */
     @RequestMapping("/geneReport")
    @ResponseBody
    public void download(HttpServletRequest request,HttpServletResponse response ,Writer writer) throws Exception{
        
         geneRepor(writer, response, request);
         
         
         String path = this.getClass().getClassLoader().getResource("/").getPath()+"template";
         File filePath = new File(path+File.separator+"xyhcbg");
            /*
             * 压缩文件夹
             */ 
            
            String zipPath = request.getSession().getServletContext().getRealPath("zip");
             String fileName = new SimpleDateFormat("yyyyMMdd").format(new Date())+".zip";
             
              fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");
                //String realPath = request.getSession().getServletContext().getRealPath("/upload");
            
                System.out.println("文件路径为:"+filePath+"编码前的:"+fileName+"编码后的文件名字:"+fileName);
                InputStream fis = null;
                byte[] buffer = null;
                try {
                    fis = new BufferedInputStream(new FileInputStream(zipPath+File.separator+fileName));
                    buffer = new byte[fis.available()];
                    fis.read(buffer);
                }finally{
                    try {
                        if(fis !=null){
                            fis.close();
                            response.reset();
                        }
                    } catch (Exception e) {
                    }
                }
                response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment;filename=" +new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
            response.addHeader("Content-Length", ""+buffer.length);
            OutputStream os = new BufferedOutputStream(response.getOutputStream());
                os.write(buffer, 0, buffer.length);
                os.flush();
                os.close();
    }

 

posted @ 2015-02-13 11:34  snow__wolf  阅读(746)  评论(0)    收藏  举报