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

下载小例子

时间:2017-07-30 14:55:03      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:gbk   isp   text   inpu   ring   ram   etc   tps   end   

1 <body>
2     <a href="/day10/down?name=day10笔记.txt">day10笔记.txt</a>
3   </body>
public class DownServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("name");
		String n=new String(name.getBytes("iso8859-1"),"utf-8");
		ServletContext context = this.getServletContext();
		String mimeType = context.getMimeType(n);
		resp.setContentType(mimeType);
		resp.setHeader("content-disposition", "attachment;filename="+new String(n.getBytes("gbk"),"iso8859-1"));
		InputStream in = context.getResourceAsStream("/"+n);
		ServletOutputStream os = resp.getOutputStream();
		IOUtils.copy(in, os);
		in.close();
		os.close();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}

  

下载小例子

标签:gbk   isp   text   inpu   ring   ram   etc   tps   end   

原文地址:http://www.cnblogs.com/may12138/p/7259028.html

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