Servlet package and download multiple files

Source: Internet
Author: User

When a user downloads multiple files at a time, a download dialog box is usually displayed for each file to be downloaded, which causes great inconvenience to the user.

Ideally, after you select multiple files, the backend of the server directly packs them as zip files. The following is the implementation code.


Front-end Javascript code (create a form using Javascript and access the backend MultiDownload by submitting a form ):

Var tmpForm = document. createElement ("form"); tmpForm. id = "form1"; tmpForm. name = "form1"; document. body. appendChild (tmpForm); var tmpInput = document. createElement ("input"); // you can specify the tmpInput parameter. type = "text"; tmpInput. name = "fileUrls"; tmpInput. value = downloadUrls; // Insert the input box to tmpform. appendChild (tmpInput); tmpForm. action = "servlet/MultiDownload"; tmpForm. method = "get"; tmpForm. submit ();
// Remove the formdocument. body. removeChild (tmpForm) from html );

Back-end MultiDownload code:

Public class MultiDownload extends HttpServlet {private static final long serialVersionUID = 1L;/*** @ see HttpServlet # HttpServlet () */public MultiDownload () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse * response) */protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubString strArray = null; strArray = request. getParameter ("fileUrls"); // when passed to servlet, the encoding is converted to a ISO-8859-1 by default and transcoded to GBKstrArray = new String (strArray. getBytes ("ISO-8859-1"), "GBK"); String [] filePathArray = strArray. split (","); String zipFileName = "product.zip"; response. setContentType ("application/x-msdownload"); // notify the customer of the MIME type of the file: response. setHeader ("Content-dis Position "," attachment; filename = "+ zipFileName); // The file directory to be downloaded ZipOutputStream zos = new ZipOutputStream (response. getOutputStream (); for (String filePath: filePathArray) {File file = new File (filePath); doZip (file, zos);} zos. close ();} /*** package as a zip file * @ param File the file to be packaged * @ param zos zip output stream * @ throws IOException */private void doZip (file File, ZipOutputStream zos) throws IOException {if (file. exist S () {if (file. isFile () {// if it is a file, write it to zos in the zip stream. putNextEntry (new ZipEntry (file. getName (); FileInputStream FCM = new FileInputStream (file); byte [] buffer = new byte [1024]; int r = 0; while (r = FCM. read (buffer ))! =-1) {zos. write (buffer, 0, r);} zos. flush (); FCM. close () ;}else {// if it is a directory. Recursively search for the file String dirName = file. getName () + "/"; zos. putNextEntry (new ZipEntry (dirName); File [] subs = file. listFiles (); for (File f: subs) {makeZip (f, dirName, zos) ;}}/ *** @ see HttpServlet # doPost (HttpServletRequest request, httpServletResponse * response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub }}


Servlet package and download multiple files

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.