bogebujianfei
2015-10-27 02:08
采纳率: 25%
浏览 1.9k
已采纳

java servlet这是单个下载,怎么打包批量下载

package cn.szusst.aqms.action.common;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

  • Servlet implementation class ServletDownload
    */

    @WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })

    public class ServletDownload extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**

    • @see HttpServlet#HttpServlet() */
      public ServletDownload() {
      super();
      // TODO Auto-generated constructor stub
      }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      */

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      response.setContentType("application/x-msdownload");
      // TODO Auto-generated method stub

      //获得请求文件名

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

      //设置文件MIME类型

      response.setContentType(getServletContext().getMimeType(filename));

      //设置Content-Disposition

      response.setHeader("Content-Disposition", "attachment;filename="+filename);

      //读取目标文件,通过response将目标文件写到客户端

      //获取目标文件的绝对路径

      // //读取文件

      OutputStream out = response.getOutputStream();

      String fullFileName = filename;
      InputStream in = new FileInputStream(fullFileName);

      //写文件

      int b;

      while((b=in.read())!= -1)

      {

      out.write(b);

      }

      in.close();

      // out.close();

      }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      }

}

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • bogebujianfei 2015-10-27 07:41
    已采纳

    这是百度上搜的呀图片说明

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题