首页 > 代码库 > Servlet文件下载
Servlet文件下载
<a href=http://www.mamicode.com/"DownLoadServlet">下载</a> <br/>
public class DownLoadServlet extends HttpServlet { public DownLoadServlet() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //处理请求 //读取要下载的文件 File f = new File("E:/好久不见.mp3"); if(f.exists()){ FileInputStream fis = new FileInputStream(f); String filename=URLEncoder.encode(f.getName(),"utf-8"); //解决中文文件名下载后乱码的问题 byte[] b = new byte[fis.available()]; fis.read(b); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition","attachment; filename="+filename+""); //获取响应报文输出流对象 ServletOutputStream out =response.getOutputStream(); //输出 out.write(b); out.flush(); out.close(); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
Servlet文件下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。