美文网首页
javaweb文件下载

javaweb文件下载

作者: Albert_d37d | 来源:发表于2020-11-24 15:16 被阅读0次

    //jsp

    <a href="download?filename=test.jpg">download</a>

    //servlet

    public void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    response.setContentType("text/html");

    response.addHeader("content-type", "application/octet-stream");

    String filename = request.getParameter("filename");

    response.addHeader("content-Disposition", "attachment;filename=" + filename);

    System.out.println("filename=" + filename);

        InputStream in = getServletContext().getResourceAsStream("download/" + filename);

        ServletOutputStream out = response.getOutputStream();

        byte[] bs = new byte[10];

        int len = -1;

        while((len = in.read(bs)) != -1)

        {

        out.write(bs,0,len);

        }

        out.close();

        in.close();

    }

    文件名中文乱码解决

    URLEncoder.encode(filename, "utf-8")

    相关文章

      网友评论

          本文标题:javaweb文件下载

          本文链接:https://www.haomeiwen.com/subject/zlwiiktx.html