jsp文件下载中的文件名等属性设置
下面是一个文件下载的例子:
<%@ page contentType="application/rar; charset=UTF-8" %>
<%
//传到页面对象
ReqAttachPo attach = (ReqAttachPo)request.getAttribute("attachPo");
//文件名
String fileName = attach.getAttachName() ;
//输入文件
byte[] content=new byte[512];
//防止进行下载时抛out在用的异常
out.clear();
response.reset();
//获取输出流
OutputStream outputStream = response.getOutputStream();
//设置文件mime类型
response.setContentType(attach.getMimeType());
//设置文件名,但要注意中文的要转化为iso-8859-1,否则出错,无法下载
response.setHeader("Content-disposition","attachment;filename="+new String(fileName.getBytes("GBK"),"iso8859-1"));
//获取流
InputStream ins = attach.getAttachIns() ;
int n=0;
while((n=ins.read(content))!=-1)
{
//下载
outputStream.write(content,0,n);
}
ins.close() ;
%>