首页 > Web开发 > 详细

通过URL下载文件

时间:2017-09-03 22:45:07      阅读:275      评论:0      收藏:0      [点我收藏+]

URL url = new URL(fileRoute);//fileRoute:文件URL路径
//通过URL的openStrean方法获取URL对象所表示的自愿字节输入流
InputStream is = url.openStream();
// 设置response参数,可以打开下载页面
response.reset();
String mimeType = MimeUtil.getMIMEType(fileName);//获取Mime 类型列表 地址:http://www.w3school.com.cn/media/media_mimeref.asp

response.setContentType(""+mimeType+";charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("utf-8"), "ISO8859-1")); 
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] e = new byte[2048];

int bytesRead;
while (-1 != (bytesRead = bis.read(e, 0, e.length))) {
bos.write(e, 0, bytesRead);
}
} catch (IOException arg24) {
throw arg24;
} finally {
if (bis != null) {
bis.close();
}

if (bos != null) {
bos.close();
}

}

return null;

通过URL下载文件

原文:http://www.cnblogs.com/hillyuen/p/7471259.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!