Javaweb file Pack bulk download code _java

Source: Internet
Author: User
Tags pack

This example for you to share the Javaweb file packaging bulk download for your reference, the specific content as follows

Bulk download @RequestMapping (value = "/downloadallhomework", method = Requestmethod.get) is not corrected for the job and public void Downloadallhomewor K (HttpSession HttpSession, httpservletrequest request, httpservletresponse response, String assignmentid, int classcode
  ) throws Exception {Site site = (site) httpsession.getattribute ("site");

  String SiteID = Site.getid ();
  Get job details based on job ID assignmentdetail assignmentdetail = assignmentservicews.getassignmentdetail (AssignmentID);

  Generateparameters (Assignmentdetail);
  The information is incomplete and needs to be populated later. list<assignmentsubmit> assignmentsubmitlist = assignmentservicews.getsubmitedassignmentstudent (AssignmentID

  );
  Get all the Submitid list<string> submitids = new arraylist<string> ();
   for (int i = 0; i < assignmentsubmitlist.size (); i++) {String Submitid = Assignmentsubmitlist.get (i). GetId ();
   if (Submitid = = NULL | | submitid = = "") continue;
  Submitids.add (Submitid); //Get submit details list<assignmentsubmit> assignmentsubmits = new Arraylist<assignmentsubmit> ();
   for (String a:submitids) {Assignmentsubmit as = Assignmentservicews.getsubmitassignment (a);
  Assignmentsubmits.add (AS); //To each student who has submitted a job with a map,username-->assignmentsubmit map<string, assignmentsubmit> studentsubmitmap = new HashM
  Ap<string, assignmentsubmit> ();
   for (Assignmentsubmit assignmentsubmit:assignmentsubmits) {String StudentID = Assignmentsubmit.getusername ();
  Studentsubmitmap.put (StudentID, assignmentsubmit); ///According to the class number, get the student number of all the students in the class, and then get the details list list<assignmentsubmit> assignmentstudentlist = new Arraylist<assignmentsub

  Mit> ();
  list<membervo> studentlist = memberservicews.getstudents (SiteID, Classcode);
   for (Membervo student:studentlist) {String userName = Student.getid ();
   String realname = Student.getname ();
   Assignmentsubmit assignmentsubmit = new Assignmentsubmit (); if (Studentsubmitmap.get (userName)!= null) {Assignmentsubmit = Studentsubmitmap.get (USErname);
   } assignmentsubmit.setrealname (Realname);
   Assignmentsubmit.setusername (UserName);
   Generatea (Assignmentsubmit);
  Assignmentstudentlist.add (Assignmentsubmit);
  } list<assignmentsubmit> submitedlist = new arraylist<assignmentsubmit> (); for (Assignmentsubmit as:assignmentstudentlist) {if (as.getgradepoint () = = null && as.getassignmentid ()!= n
  ull) Submitedlist.add (AS);

  } list<file> files = new arraylist<file> ();
  File File = new file ("D:/css.rar");
  if (!file.exists ()) {file.createnewfile ();
  } response.reset ();

  Response.getwriter ()//create file output stream FileOutputStream fous = new FileOutputStream (file);
  Packaging method We will use zipoutputstream such an output stream, so here we convert the output stream zipoutputstream zipout = new Zipoutputstream (fous); for (Assignmentsubmit a:submitedlist) {for (Attachids aa:a.getattachids ()) {try {String Fileid = aa.ge
     TId (); String Cloudfileurl = "Http://xxx.xxx.xxx.xxx:8066/ImageService/downloadfile/";
     String fileUrl = Announceservice.getattachmentbyfileid (Fileid). Geturlupload ();
     FILEURL = fileurl.substring (Fileurl.lastindexof ("/") + 1);
     FILEURL = Cloudfileurl + fileUrl; String fileName = Announceservice.getattachmentbyfileid (Fileid). GetName ();
     Gets the file name of the remote file. Response.AddHeader ("Content-disposition", "attachment;filename=" +//New String (Filename.getbytes ("GBK"), "iso-88
     59-1 "));
     Iso-8859-1 ZipEntry entry = new ZipEntry (New String (Filename.getbytes ("GBK"), "iso-8859-1"));
     Zipout.putnextentry (entry);
     URL urlfile = null;
     HttpURLConnection httpurl = null;
     Urlfile = new URL (FILEURL);
     Httpurl = (httpurlconnection) urlfile.openconnection ();
     Httpurl.connect ();
     InputStream downloadFile = Httpurl.getinputstream ();
     int len = 0;
     byte[] buf = new byte[1024];
     while (len = Downloadfile.read (buf, 0, 1024))!=-1) {zipout.write (buf, 0, Len); catch (Jsonexception e){E.printstacktrace ();
    catch (Unsupportedencodingexception e) {e.printstacktrace ();
  }} zipout.close ();
  Fous.close ();
 Downloadzip (file, response); ///To compress all the accepted files into a compressed package public static HttpServletResponse downloadzip (file file, httpservletresponse response) {try {/
   /Download the file as a streaming form.
   InputStream fis = new Bufferedinputstream (New FileInputStream (File.getpath ()));
   byte[] buffer = new byte[fis.available ()];
   Fis.read (buffer);
   Fis.close ();
   Empty response Response.reset ();
   OutputStream toclient = new Bufferedoutputstream (Response.getoutputstream ());

   Response.setcontenttype ("Application/octet-stream"); If the output is the Chinese name of the file, here you will use the Urlencoder.encode method for processing response.setheader ("Content-disposition", "attachment;filename=" +
   Urlencoder.encode (File.getname (), "UTF-8"));
   Toclient.write (buffer);
   Toclient.flush ();
  Toclient.close ();
  catch (IOException ex) {ex.printstacktrace (); Finally {try {file F = new file (file.getpAth ());
   F.delete ();
   catch (Exception e) {e.printstacktrace ();

 } return response;

 }

Blog address! http://oldriver.top/Old Driver Technical Handbook

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.