Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to download multiple files in batch and package them into zip format

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use java to achieve bulk download of multiple files packaged into zip format to download, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

The details are as follows

What is needed now:

Download the pic package under the product family and product type according to the product family and product type

The pic package is a zip compressed file

T _ product table:

These packages are stored in another table in the form of blob:

T _ imagefile table:

What we need to do now is to package the two packages downloadPIC:MA5800 series-pic.zip and MA5900-pic.rar under the access network and OLT together into zip compressed files and download them.

Code:

ProductController.java:

/ * download photo package based on product family and product type * / @ RequestMapping ("/ downloadwBatch") public void downloadwBatch (HttpServletRequest request, HttpServletResponse response, String productFamily, String productType) {/ / http://localhost:8080/MySSM/downloadwBatch?productFamily= access network & productType=OLT try {productFamily = new String ("iso-8859-1"), "utf-8"); productType= new String (productType.getBytes ("iso-8859-1"), "utf-8") } catch (UnsupportedEncodingException e) {e.printStackTrace ();} / / get the photo package name to download Map params = new HashMap (); params.put ("productFamily", productFamily); params.put ("productType", productType); List packageNames = productService.getPackageNamesByFamilyAndType (params); / / get the file name to be downloaded according to the package name-the form of a byte array Map files = new HashMap (); for (String packageName: packageNames) {byte [] f = productService.getPackage (packageName) If (favored null) {files.put (packageName, f);}} / / set the name of the downloaded package String zipName = productFamily + "_" + productType + ".zip"; / / compress it according to the file, and download if (files.size () > 0) {productService.downloadBatchByFile (response, files, zipName);}}

ProductService.java:

/ * get the file * / public byte [] getPackage (String packageName) {byte [] bag = null; try {ImageFile m = productMapper.getPackage (packageName); if (masked null) {bag = m.getPicture ();}} catch (Exception e) {e.printStackTrace ();} return bag } / * obtain the package name to be downloaded according to product family and product type * @ param params * @ return * / public List getPackageNamesByFamilyAndType (Map params) {List packageNames = productMapper.getPackageNamesByFamilyAndType (params); return packageNames;} / * compress according to the file, batch download * @ param response * @ param files * @ throws Exception * / public void downloadBatchByFile (HttpServletResponse response, Map files, String zipName) {try {response.setContentType ("application/x-msdownload") Response.setHeader ("content-disposition", "attachment;filename=" + URLEncoder.encode (zipName, "utf-8")); ZipOutputStream zos = new ZipOutputStream (response.getOutputStream ()); BufferedOutputStream bos = new BufferedOutputStream (zos); for (Entry entry: files.entrySet ()) {String fileName = entry.getKey (); / / each zip file name byte [] file = entry.getValue (); / / the zip file byte BufferedInputStream bis = new BufferedInputStream (new ByteArrayInputStream (file)); zos.putNextEntry (new ZipEntry (fileName)) Int len= 0; byte [] buf = new byte [10 * 1024]; while ((len=bis.read (buf, 0, buf.length))! =-1) {bos.write (buf, 0, len);} bis.close (); bos.flush ();} bos.close ();} catch (Exception e) {e.printStackTrace ();}

ProductMapper.java:

/ * * get the file * / public ImageFile getPackage (String packageName) throws Exception; / * get the package name to be downloaded according to product family and product type * / public List getPackageNamesByFamilyAndType (Map params)

ProductMapper.xml:

Select * from t_imagefile where packageName = # {packageName} select packageName from t_imagefile m join t_product p on m.packageName = p.downloadPic where p.productFamily = # {productFamily} and p.productType = # {productType}

Test:

Enter: http://localhost:8080/MySSM/downloadwBatch?productFamily= access Network & productType=OLT in the browser

Simple demo

Package com.msznyl; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class Download {public static void main (String [] args) {/ / Files to be compressed-including file address and file name String [] path = {"E:\\ 360DocProtect\ 1.txt", "E:\\ 360DocProtect\ 2.docx"} / / the address and file name of the compressed file to be generated String desPath = "D:\\ DOWNLOADS\\ new.zip"; File zipFile = new File (desPath); ZipOutputStream zipStream = null; FileInputStream zipSource = null; BufferedInputStream bufferStream = null; try {/ / construct the output stream of the final compressed package zipStream = new ZipOutputStream (new FileOutputStream (zipFile)); for (int I = 0trai

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report