码迷,mamicode.com
首页 > 其他好文 > 详细

downLoad

时间:2017-04-11 16:54:47      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:servlet   res   utf-8   coder   bsp   out   pos   down   encoder   

String root= ServletActionContext.getServletContext().getRealPath(File.separator).replace("\\", File.separator);
String fileName = "download.text";
File file = new File(root + fileName);
in = new FileInputStream(file);
// 设置响应正文的MIME类型
response.setContentType("Content-Disposition;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;" + " filename=\""+ URLEncoder.encode(fileName, "UTF-8") + "\"");
// 把本地文件发送给客户端
out = response.getOutputStream();
int byteRead = 0;
byte[] buffer = new byte[512];
while ((byteRead = in.read(buffer)) != -1)
{
  out.write(buffer, 0, byteRead);
}
in.close();
out.close();

 

downLoad

标签:servlet   res   utf-8   coder   bsp   out   pos   down   encoder   

原文地址:http://www.cnblogs.com/kuiyeit/p/6693957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!