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
    已采纳

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

    打赏 评论
  • 毕小宝 2015-10-27 05:31

    批量文件下载可以利用ZipOutputStream将文件打包,然后输出给客户端。
    简单批量下载的工程实例,参考地址:http://download.csdn.net/download/totzc/5772899
    批量下载servlet参考代码:

     package servlet;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipOutputStream;
    /**
     * 2013-07-17 22:23:12
     * Servlet 实现多文件下载
     * WeiBo http://weibo.com/totzcc
     * 技术参考文档:
     *      Servlet 实现多文件下载                       http://blog.csdn.net/sunshinestation/article/details/4383953
     *      java.util.zip.ZipOutputStream中文乱码       http://blog.csdn.net/xueyepiaoling/article/details/6119924
     * @author Leo
     *
     */
    public class ZipDown extends HttpServlet {
    
        /**
         * serialVersionUID
         */
        private static final long serialVersionUID = -6442346058533380418L;
    
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //要下载的文件目录
            String path = "D:/sss";
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            //设置文件编码。 防止中文乱码。 这里用 org.apache.tools.zip.ZipOutputStream; 而不是java.util.zip 下的类
            zos.setEncoding("GBK");
            File file = new File(path);
            makeZip(file, "", zos);
            zos.flush();
            zos.close();
        }
    
    
        /**
         *  制作 zip 文件
         * @param file 文件
         * @param baseName 文件根包名
         * @param zos zip 输出流
         * @throws IOException 
         */
        private void makeZip(File file, String baseName, ZipOutputStream zos)
                throws IOException {
            if(file.exists()) {
                if (file.isFile()) {
                    //如果是文件,写入到 zip 流中
                    zos.putNextEntry(new ZipEntry(baseName + file.getName()));
                    FileInputStream fis = new FileInputStream(file);
                    byte[] buffer = new byte[1024];
                    int r = 0;
                    while ((r = fis.read(buffer)) != -1) {
                        zos.write(buffer, 0, r);
                    }
                    zos.flush();
                    //文件读取完后记得关闭!
                    fis.close();
                } else {
                    //如果是目录。递归查找里面的文件
                    String dirName = baseName + file.getName() + "/";
                    zos.putNextEntry(new ZipEntry(dirName));
                    File[] subs = file.listFiles();
                    for (File f : subs) {
                        makeZip(f, dirName, zos);
                    }
                }
            }
        }
    
    }
    
    
    打赏 评论
  • bogebujianfei 2015-10-30 04:45

    在慕课网看了视频,搞定

    打赏 评论

相关推荐 更多相似问题