如何从Servlet上的bytearray返回zip

时间:2015-06-02 12:35:43

标签: java servlets stream

再次使用Java EE。

在示例中

  1. 在servlet上我有byte [] fileContent ---->(file.txt)
  2. 我尝试将其添加到zip文件
  3. 将servlet设置为

    response.setContentType("Content-type: text/zip");
    response.setHeader("Content-Disposition",
            "attachment; filename=mytest.zip");
    
    // List of files to be downloaded
    List files = new ArrayList();
    files.add(new File("C:/first.txt"));
    
    ServletOutputStream out = response.getOutputStream();
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(out));
    
    for (File file : files) {
    
        System.out.println("Adding " + file.getName());
        zos.putNextEntry(new ZipEntry(file.getName()));
    
        // Get the file
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
    
        } catch (FileNotFoundException fnfe) {
            // If the file does not exists, write an error entry instead of
            // file
            // contents
            zos.write(("ERRORld not find file " + file.getName())
                    .getBytes());
            zos.closeEntry();
            System.out.println("Couldfind file "
                    + file.getAbsolutePath());
            continue;
        }
    
        BufferedInputStream fif = new BufferedInputStream(fis);
    
        // Write the contents of the file
        int data = 0;
        while ((data = fif.read()) != -1) {
            zos.write(data);
        }
        fif.close();
    
        zos.closeEntry();
        System.out.println("Finishedng file " + file.getName());
    }
    
    zos.close();
    

    } }

  4. 我对第二点有疑问。我尝试了几种解决方案,但他们都很难使用我的servlet或者没有工作。

    有人可以通过示例向我展示如何将文件作为字节数组添加到zip并在servlet上作为atachment返回?感谢您的任何提示和建议

    - 发布编辑

    我尝试过类似的东西,但是如何添加byte []而不是file。

1 个答案:

答案 0 :(得分:0)

以下是解决方案的示例:

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) { /**/ }
    }
}

 }