Generate multiple files and package them in a zipped package and download them on the Web page.

Source: Internet
Author: User
Tags openid

Web page:

<href= "${ctx}/tactivity/redpacketdetailszip?activityid=${activityid}"  Target= "_blank"> export yesterday OpenID</a>

Java code:

    /*** Export zip file. A TXT file is in the zip. TXT file is a 500 OpenID.     OpenID a line. * @authorXuyong * @date 2014-09-26*/@RequestMapping (Value= "/redpacketdetailszip")     PublicString Redpacketdetailszip (modelmap model, ttakepartquery query, httpservletrequest request, Ht Tpservletresponse response)throwsException {logger.info ("Redpacketdetailszip Red envelopes export Zip file start ..."); //query criteria Activity ID is Activityid,and 10,000 not sent record for participation time less than dateutil.getcurrentdate (DATEUTIL.DATE_FMT)String activityidstr = Request.getparameter ("ActivityID");//Activity IDTactivity tactivity =Tactivitymanager.getbyid (long.valueof (ACTIVITYIDSTR)); Long ActivityID=NewLong (ACTIVITYIDSTR); Query.setsendtimezip (Dateutil.getcurrentdate (DATEUTIL.DATE_FMT)); //month and day, time is less than the participation of this timeQuery.setactivityid (ActivityID);//EventsQuery.setsortcolumns ("T.createtime desc");//SortQuery.setpagesize (10000);//article numberPage page =ttakepartmanager.findpage (query); Logger.info ("Generate File File ...."); String Zipfilename= This. SaveFile (Request, Tactivity.getshopid (), Page.getresult ()); Logger.info ("Generate file ...."); OutputStream o=Response.getoutputstream (); byteB[] =New byte[1024]; File Fileload=NewFile (zipfilename); Response.setheader ("Content-disposition", "attachment;filename=" + Urlencoder.encode (Fileload.getname (), "Utf-8")); Response.setcontenttype ("Application/zip");//set the MIME type. Response.setheader ("Content_length", String.valueof (Fileload.length ()));//get the file length.FileInputStream in =NewFileInputStream (Fileload);//Download the file.         intn = 0;  while((n = in.read (b))! =-1) {O.write (b,0, N); } logger.info ("Redpacketdetailszip Red envelopes export Zip file end."); return NULL; }
/**Save the file and claim to compress the package, return the package address * * @date Nov, 20142:06:25 PM *@paramResultlist *@returnZip Package Address *@throwsIOException*/    PrivateString SaveFile (httpservletrequest request, Long Shopid, List resultlist)throwsioexception{Logger.info ("Generate File Start ..."); String Zippath= Dateutil.getcurrentdatestr (DATEUTIL.DATE_FMT) +resultlist.size () + "OpenID compression pack. zip";//Upload file path        intPageSize = 3;//each file storage sizeString txtfullpath = "file name"; StringBuffer Openidbuffer=NewStringBuffer (100);//OpenID ContentList<file> allfileslist =NewArraylist<file> ();//File Collection//Save the export file. Maximum of 20 txtLogger.info ("Generate file TXT file start ...");  for(intI=0;i<resultlist.size (); i++) {Ttakepart Ttakepart=(Ttakepart) resultlist.get (i); if(I%pagesize==0 && i>0) {Txtfullpath= Fileutil.getfilepath (Request, Constant.carddown_path, Shopid, i+ ". txt"); Logger.info ("Generate file TXT file txtfullpath=" +Txtfullpath); //Create a Save filefileutil.saveonefile (Txtfullpath, openidbuffer.tostring ()); Allfileslist.add (NewFile (Txtfullpath)); //Reset ContentOpenidbuffer =NewStringBuffer (100); } openidbuffer.append (Ttakepart.getwxno ()). Append ("\ n");//OpenID Content        }        if(Openidbuffer.length () > 0) {//the remaining contents of the file exist, save the fileTxtfullpath = Fileutil.getfilepath (Request, Constant.carddown_path, Shopid, "Last.txt"); Logger.info ("Generate file TXT file txtfullpath=" +Txtfullpath); //Create a Save filefileutil.saveonefile (Txtfullpath, openidbuffer.tostring ()); Allfileslist.add (NewFile (Txtfullpath)); //Reset ContentOpenidbuffer =NewStringBuffer (100); } logger.info ("Generate file TXT file end ..."); Logger.info ("Compress txt file start ..."); //Delete an existing fileFile ZipFile =NewFile (Zippath); if(Zipfile.exists ()) {zipfile.delete (); } Zippath=Fileutil.getfilepath (Request, Constant.carddown_path, Shopid, Zippath); //Compress file Packaging        byte[] buffer =New byte[1024]; Zipoutputstream out=NewZipoutputstream (NewFileOutputStream (Zippath)); //encapsulate your list with the appropriate business. FileInputStream FIS =NULL;  for(File eachfile:allfileslist) {FIS=NewFileInputStream (Eachfile); Out.putnextentry (NewZipEntry (Eachfile.getname ())); intLen;  while(len = fis.read (buffer)) > 0) {out.write (buffer,0, Len);            } out.closeentry ();            Fis.close (); Logger.info ("Delete file txt file txtfullpath=" +Eachfile.getcanonicalpath ());        Eachfile.delete ();        } out.close (); Logger.info ("Compress txt file end."); Logger.info ("Generate file End....zippath=" +Zippath); returnZippath; }

/**Save File ** @date, 20144:27:49 PM *@paramTxtfullpath File Full path *@paramfilecontent File Contents *@throwsIOException*/     Public Static voidSaveonefile (String FullPath, String filecontent)throwsIOException {File file=NewFile (FullPath); //Create a file        if(!file.exists ()) {            if(!File.getparentfile (). exists ()) {File.getparentfile (). Mkdirs ();//Create a directory} file.createnewfile (); }        //Save FileBufferedoutputstream Bufferedoutputstream =NewBufferedoutputstream (Newfileoutputstream (file));         Bufferedoutputstream.write (Filecontent.tostring (). GetBytes ()); Bufferedoutputstream.close ();//Write Content}

Generate multiple files and package them in a zipped package and download them on the Web page.

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.