Java bulk Download, packaging multiple files into a zip format for download

Source: Internet
Author: User

Now the needs of:

According to product family, product type, download the product family, the product type under the PIC package;

Pic Package is a zip compressed file;

T_product table:

These packages exist in another table in the form of blobs:

T_imagefile table:

Now to do is: the access network, the Olt below the two package downloadpic:ma5800 series-pic.zip and Ma5900-pic.rar packaged together into a zip compressed file download down;

Code:

Productcontroller.java:

    /*** Download Photo packs According to product family, product type*/@RequestMapping ("/downloadwbatch")     Public voidDownloadwbatch (httpservletrequest request, httpservletresponse Response, String productfamily, String ProductType ){        //http://localhost: 8080/myssm/downloadwbatch?productfamily= access Network &producttype=olt        Try{productfamily=NewString (Productfamily.getbytes ("iso-8859-1"), "Utf-8"); ProductType=NewString (Producttype.getbytes ("iso-8859-1"), "Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }                //get the photo package name to downloadmap<string, string> params =NewHashmap<string, string>(); Params.put ("Productfamily", productfamily); Params.put ("ProductType", ProductType); List<String> Packagenames =Productservice.getpackagenamesbyfamilyandtype (params); //gets the file name to download, in the form of a byte array, based on the package nameMap<string,byte[]> files =NewHashmap<string,byte[]>();  for(String packagename:packagenames) {byte[] f =productservice.getpackage (PackageName); if(f!=NULL) {files.put (PackageName, F); }        }                //set the download's compressed package nameString zipname = productfamily + "_" + ProductType + ". zip"; //according to the file, compression, bulk download        if(Files.size () > 0) {productservice.downloadbatchbyfile (response, files, zipname); }            }

Productservice.java:

    /*** Get files based on package name*/     Public byte[] getpackage (String packagename) {byte[] bag =NULL; Try{imagefile m=productmapper.getpackage (PackageName); if(m!=NULL) {Bag=m.getpicture (); }        }Catch(Exception e) {e.printstacktrace (); }        returnbag; }            /*** Get the package name to download according to product family and product Type *@paramparams *@return     */     PublicList<string> Getpackagenamesbyfamilyandtype (map<string, string>params) {List<String> Packagenames =Productmapper.getpackagenamesbyfamilyandtype (params); returnPackagenames; }        /*** According to the file, compression, bulk download *@paramResponse *@paramFiles *@throwsException*/     Public voidDownloadbatchbyfile (httpservletresponse response, map<string,byte[]>files, String zipname) {        Try{Response.setcontenttype ("Application/x-msdownload"); Response.setheader ("Content-disposition", "Attachment;filename=" +urlencoder.encode (Zipname, "Utf-8")); Zipoutputstream Zos=NewZipoutputstream (Response.getoutputstream ()); Bufferedoutputstream Bos=NewBufferedoutputstream (ZOS);  for(Entry<string,byte[]>Entry:files.entrySet ()) {String FileName= Entry.getkey ();//each zip file name                byte[] File = Entry.getvalue ();//The bytes of this zip fileBufferedinputstream bis=NewBufferedinputstream (Newbytearrayinputstream (file)); Zos.putnextentry (NewZipEntry (fileName)); intLen = 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 file according to package name */public     throws  Exception;     /**      * According to product family, product type get the package name     to download */public     list<string> Getpackagenamesbyfamilyandtype (map<string, string> params);
View Code

Productmapper.xml:

    <!--get files based on package name -    <SelectID= "Getpackage"ParameterType= "Java.lang.String"Resulttype= "Com.cy.model.ImageFile">SELECT * from t_imagefile where packagename = #{packagename}</Select>        <!--according to product family, product type get the package name to download -    <SelectID= "Getpackagenamesbyfamilyandtype"ParameterType= "Java.util.Map"Resulttype= "Java.lang.String">Select PackageName from t_imagefile m joins t_product p on m.packagename = p.downloadpic where P.produc tfamily = #{productfamily} and P.producttype = #{producttype}</Select>
View Code

Test:

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

The download results are as follows:

---------

Java bulk Download, packaging multiple files into a zip format for download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.