经过URL下载文件

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.aspjava

response.setContentType(""+mimeType+";charset=utf-8");//new String(fileName.getBytes("utf-8"), "ISO8859-1")以前是这个,经过放到服务器上,发现乱码,若是不乱,多是你的tomcat,server.xml配置了URIEncoding("UTF-8"),在这里不建议这样作。作好换成URLDecoder.decode(model.getFileName(), "UTF-8")
response.setHeader("Content-Disposition","attachment;filename="+URLDecoder.decode(model.getFileName(), "UTF-8")); 
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;tomcat

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();
}url

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

}code

return null;server

相关文章
相关标签/搜索