下载小例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了下载小例子相关的知识,希望对你有一定的参考价值。
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); } }
以上是关于下载小例子的主要内容,如果未能解决你的问题,请参考以下文章