Location>code7788 >text

Compressor Flow Usage

Popularity:835 ℃/2024-08-27 16:53:55

compressed flow

Scenario: need to download some data, as well as the attachments associated with this data compressed, data exported to an Excel, attachments exported to a folder

Here use easyxcel to export excel from Huawei cloud obs Download attachment

and give it to the browser

image

public void downloadAllEliminate(HttpServletResponse response) throws IOException {

    // Response header settings
    ();
    ("utf-8");
    ("multipart/form-data");
    // Set the name of the zip file
    // Solve the problem of messy code when the zip name contains Chinese in different browsers.
    String downloadName = ()+".zip";
    ("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");


    // digital
    List<CurrentTransactionsCountDetailsResult> allEliminate = ();
    InputStream inputStream = ().getClassLoader().getResourceAsStream("static/pending account-get rid of.xlsx");

    // excel output stream
    ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
    ExcelWriter excelWriter = (outputStream1).withTemplate(inputStream).build();
    WriteSheet writeSheet = ("pending account明细").build();
    FillConfig fillConfig = ().direction().forceNewRow().build();
    (new FillWrapper("list", allEliminate), fillConfig, writeSheet);
    ();//Stop the flow.


    // Setting up the compression stream:Write directly toresponse,Download while compressing
    ZipOutputStream zipos = null;
    try {
        zipos = new ZipOutputStream(new BufferedOutputStream(()));
        (); // Setting the compression method
    } catch (Exception e) {
        ();
    }

    // commander-in-chief (military)excelwrite
    (new ZipEntry("pending account-get rid of.xlsx"));
    byte[] buffer = ();
    (buffer, 0, );
    ();

    // Get all the files., 并write压缩流
    List<Map<String, String>> allEliminateFileName = ();
    String filePath = "attachment (email)/";
    for (Map<String, String> stringStringMap : allEliminateFileName) {
        InputStream is = (("path"), );
        (new ZipEntry(filePath + ("fileName")));

        byte[] bufferTo = new byte[2048]; // Larger buffers usually improve efficiency
        int length;
        while ((length = (bufferTo)) != -1) {
            (bufferTo, 0, length); // Write directly tozipos
        }

        ();
        ();
    }
    // 确保所有条目都已write,and flushes the buffer
    ();

    // Stop the flow.
    try {
        ();
    } catch (IOException e) {
        ();
    }


}