Java web实现文件打包下载功能

来源:互联网 发布:java选择结构的种类 编辑:程序博客网 时间:2024/06/04 17:40

首先需要实现一个文件打包下载的辅助类

public class CustomFileUtil {    /**      *       * @param inputFileName      *            输入一个文件夹      * @param zipFileName      *            输出一个压缩文件夹,打包后文件名字      * @throws Exception      */      public static void zip(String inputFileName, String zipFileName) throws Exception {          zip(zipFileName, new File(inputFileName));      }      private static void zip(String zipFileName, File inputFile) throws Exception {          ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));          zip(out, inputFile, "");          out.close();      }      private static void zip(ZipOutputStream out, File f, String base) throws Exception {          if (f.isDirectory()) { // 判断是否为目录              File[] fl = f.listFiles();              out.putNextEntry(new ZipEntry(base + "/"));              base = base.length() == 0 ? "" : base + "/";              for (int i = 0; i < fl.length; i++) {                  zip(out, fl[i], base + fl[i].getName());              }          } else { // 压缩目录中的所有文件              out.putNextEntry(new ZipEntry(base));              FileInputStream in = new FileInputStream(f);              int b;              while ((b = in.read()) != -1) {                  out.write(b);            }              in.close();          }      }      /**      * 下载文件      *       * @param file 文件路径     * @param response      * @param isDelete 是否删除生成的压缩包     */      public static void downloadFile(File file,HttpServletResponse response,boolean isDelete) {        try {            // 以流的形式下载文件。            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));            byte[] buffer = new byte[fis.available()];            fis.read(buffer);            fis.close();            // 清空response            response.reset();            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());            response.setContentType("application/octet-stream");            response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));            toClient.write(buffer);            toClient.flush();            toClient.close();            if(isDelete)            {                file.delete();        //是否将生成的服务器端文件删除            }         }          catch (IOException ex) {            ex.printStackTrace();        }    }    public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException, ServletException {        try {            int size = files.size();            // 压缩列表中的文件            for (int i = 0; i < size; i++) {                File file = (File) files.get(i);                zipFile(file, outputStream);            }        } catch (IOException e) {            throw e;        }    }    public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException {        try {            if (inputFile.exists()) {                if (inputFile.isFile()) {                    FileInputStream inStream = new FileInputStream(inputFile);                    BufferedInputStream bInStream = new BufferedInputStream(inStream);                    ZipEntry entry = new ZipEntry(inputFile.getName());                    outputstream.putNextEntry(entry);                    final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流为10M                    long streamTotal = 0; // 接受流的容量                    int streamNum = 0; // 流需要分开的数量                    int leaveByte = 0; // 文件剩下的字符数                    byte[] inOutbyte; // byte数组接受文件的数据                    streamTotal = bInStream.available(); // 通过available方法取得流的最大字符数                    streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分开的数量                    leaveByte = (int) streamTotal % MAX_BYTE; // 分开文件之后,剩余的数量                    if (streamNum > 0) {                        for (int j = 0; j < streamNum; ++j) {                            inOutbyte = new byte[MAX_BYTE];                            // 读入流,保存在byte数组                            bInStream.read(inOutbyte, 0, MAX_BYTE);                            outputstream.write(inOutbyte, 0, MAX_BYTE); // 写出流                        }                    }                    // 写出剩下的流数据                    inOutbyte = new byte[leaveByte];                    bInStream.read(inOutbyte, 0, leaveByte);                    outputstream.write(inOutbyte);                    outputstream.closeEntry(); // Closes the current ZIP entry                    // and positions the stream for                    // writing the next entry                    bInStream.close(); // 关闭                    inStream.close();                }            } else {                throw new ServletException("文件不存在!");            }        } catch (IOException e) {            throw e;        }    }}

控制器实现下载功能,其中File.separatorChar是File类的静态字段,由于windows系统和linux系统下的路径分割符是不一样的,所以在此使用File.separatorChar,来默认在windows中为\分隔符,Linux下为/分割符

@RequestMapping(value ="/BatchDownload", method = RequestMethod.GET)@ResponseBodypublic void BatchDownload(    @RequestParam(value = "ids", required = true) String ids,    HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{    //文件路径最后带分隔符"/"    String inputFileName = request.getSession().getServletContext().getRealPath("upload")+File.separatorChar+"qrcode"+File.separatorChar+ids+File.separatorChar;        List<File> files = new ArrayList<File>();       File Allfile = new File(inputFileName);       if (Allfile.exists()) {           File[] fileArr = Allfile.listFiles();           for (File file2 : fileArr) {               files.add(file2);           }       }        //压缩包名字       String fileName =this_ResListService.selectById(ids).getName()+".zip";       // 在服务器端创建打包下载的临时文件       String outFilePath = inputFileName;       File fileZip = new File(outFilePath + fileName);       FileOutputStream outStream = new FileOutputStream(fileZip); // 文件输出流       ZipOutputStream toClient = new ZipOutputStream(outStream);// 压缩流       CustomFileUtil.zipFile(files, toClient);       outStream.close();       CustomFileUtil.downloadFile(fileZip, response, true);   }

最后需要在前台请求打包下载,在这也把代码贴上,

/*批量下载二维码*/function BatchDownload(){    var url='/ResSeat/BatchDownload.do?ids='+resrantid;    if(url){       location.href = url;    }}

前台请求时一定注意,一定要写上

location.href = url;  //如果不写这一行代码,打包的文件只会缓存在Response里,不会下载下来

贴代码的原因,也是如此,作为一个新手,我第一次实现打包下载功能时,前台是用ajax请求的,并且没有跳转,来来去去修改了好几次打包下载的后台代码,最后打开浏览器查看才发现,文件是缓存到浏览器了,但是没有跳转下载。

至此,打包下载的功能已完全实现,适用于所有文件。