java消息头_【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文件下载的代码:

/*** 生成审核报告

*@throwsException

*@throwsException*/@RequestMapping("/geneReport")

@ResponseBodypublic void download(HttpServletRequest request,HttpServletResponse response ,Writer writer) throwsException{

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= newBufferedOutputStream(response.getOutputStream());

os.write(buffer,0, buffer.length);

os.flush();

os.close();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值