Servlet - how to download multiplexers from the database

I look around and it seems that combining all the files together is the way to go. If this is the case, this is the design I'm thinking of. Please let me know if there is a more efficient way to do this.

  • The client selects a multiple download file, then click "Download"
  • the servlet receives the queries, then executes several SELECT queries (files saved as blob objects) to the database.

I can create a BufferedOutputStream and write blobs to different files, and I think that after I do this, I can archive the files. (is this a good way to zip all the files or is there a better and faster way to achieve this?) After you have done the zipping, send it to the client (don’t know how to do this, please anyone know how to do this, please help) Please indicate if there is any flaw in my design. I post some questions above, and would really appreciate that someone could help me answer. The sample code will be awesome. Thank you so much and I have a wonderful new year.

+3
source share
3 answers

ZipOutputStream response.getOutputStream(), InputStream ZipEntry. , response.getOutputStream() BufferedOutputStream InputStream BufferedInputStream byte[].

:

package com.example;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ZipFileServlet extends HttpServlet {

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
    private YourFileDAO yourFileDAO = YourDAOFactory.getYourFileDAO();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        String[] fileIds = request.getParameterValues("fileId");
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"allfiles.zip\"");
        ZipOutputStream output = null;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        try {
            output = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE));

            for (String fileId : fileIds) {
                YourFileItem item = yourFileDAO.find(fileId);
                if (item == null) continue; // Handle yourself. The fileId may be wrong/spoofed.
                InputStream input = null;

                try {
                    input = new BufferedInputStream(item.getInputStream(), DEFAULT_BUFFER_SIZE);
                    output.putNextEntry(new ZipEntry(item.getName()));
                    for (int length = 0; (length = input.read(buffer)) > 0;) {
                        output.write(buffer, 0, length);
                    }
                    output.closeEntry();
                } finally {
                    if (input != null) try { input.close(); } catch (IOException logOrIgnore) { /**/ }
                }
            }
        } finally {
            if (output != null) try { output.close(); } catch (IOException logOrIgnore) { /**/ }
        }
    }

}

:

<form action="zipFile" method="post"> 
    <input type="checkbox" name="fileId" value="1"> foo.exe<br>
    <input type="checkbox" name="fileId" value="2"> bar.pdf<br>
    <input type="checkbox" name="fileId" value="3"> waa.doc<br>
    <input type="checkbox" name="fileId" value="4"> baz.html<br>
    <input type="submit" value="download zip">
</form>

, ByteArrayInputStream/ByteArrayOutputStream, . byte[]. , , , ( !) bytearrays , . . /. ( ), .

+11

. zip .

FileOutputStream fos = new FileOutputStream(zipFileName);
zipOutStream = new ZipOutputStream(fos);

,

ZipEntry zipEntry = new ZipEntry("NameOfFileToBeAdded");
zipOutStream.putNextEntry(zipEntry);
zipOutStream.write(byteArrayOfFileEntryData);
zipOutStream.closeEntry();

, , byteArrayOfFileEntryData .

zip , zip

zipOutStream.close();

zip .

: - zip , . , zip . .

zipOutStream = new ZipOutputStream(response.getOutputStream())

zip , Tomcat, zip . , , , zip , . , zip . zip .

+5

blob , zip . zip , .

+1

Source: https://habr.com/ru/post/1726813/


All Articles