zoukankan      html  css  js  c++  java
  • Servlet下载文件代码


    public class FileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String realPath = "F:\ideaSpace\New\servletDemo\src\main\resources\1.png";
    System.out.println("下载文件的路径:"+realPath);
    String fileName = realPath.substring(realPath.lastIndexOf("\")+1);
    //F:ideaSpaceNewservletDemosrcmain esources1.png
    resp.setHeader("Content-Disposition","attachement;filename="+fileName);
    FileInputStream in = new FileInputStream(realPath);
    int len = 0;
    byte[] buffer = new byte[1024];
    ServletOutputStream out = resp.getOutputStream();
    while((len=in.read(buffer))>0){
    out.write(buffer,0,len);
    }
    in.close();
    out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doPost(req, resp);
    }
    }
  • 相关阅读:
    Javascript 进阶
    transform顺序浅谈
    js对象克隆
    js动画最佳实现——requestAnimationFrame
    svg标签
    typeof和instanceof
    js变量浅谈
    X-UA-compatible浅谈
    封装$
    面向对象
  • 原文地址:https://www.cnblogs.com/bellwether/p/14642317.html
Copyright © 2011-2022 走看看