java实现文件下载
2018-07-20 来源:open-open
文件下载
输出内容包含
1.文件内容:content
2. 输出类型 contentType : application/msword
3 文件长度: contentLength: content.length
4.文件名称:
/**
* 下载文件
* @param request
* @param response
* @throws IOException
* @throws InterruptedException
*/
public void downloadDoc(HttpServletRequest request,HttpServletResponse response) throws IOException, InterruptedException
{
String id=request.getParameter("id")==null?"0":request.getParameter("id");
DocumentAtt documentAtt=documentAttDao.findById(id); //业务对象根据实际情况修改
byte [] content=documentAtt.getFiles();
OutputStream os=response.getOutputStream();
InputStream is=new ByteArrayInputStream(content);
response.setContentType(documentAtt.getFiletype());//<span style="font-family: Arial, Helvetica, sans-serif;">可不设置</span>
response.setContentLength(content.length);//可不设置
response.setHeader("Content-Disposition","attachment;filename="+new String(documentAtt.getName().getBytes("GBK"),"ISO-8859-1"));
byte[] buffer = new byte[4000];
int length;
while((length = is.read(buffer)) != -1){
os.write(buffer,0,length);
}
is.close();
os.close();
}
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:java日期遍历
最新资讯
热门推荐