动态生成的zip下载结果损坏的文件

时间:2014-08-17 00:52:23

标签: java servlets io zip

我想要实现的是,创建动态zip文件并将其写入ServletOutput流。我可以设法通过我的代码下载一个zip文件。但下载的zip内容无法使用。

感谢您的回答。

package mainpackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

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

import org.apache.commons.io.IOUtils;
import org.apache.coyote.Response;


public class DownloadZipServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public DownloadZipServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition",
                "attachment;filename=download.zip");

        ServletOutputStream sos;
        ZipOutputStream zos;
        InputStream fis;
        List<File> filesToDownload = new ArrayList<File>();

        filesToDownload.add(new File(getDirectory(), "download.png"));
        filesToDownload.add(new File(getDirectory(), "download2.png"));

        sos = response.getOutputStream();
        zos = new ZipOutputStream(sos);


        for (File fileToSend : filesToDownload) {

            ZipEntry ze = new ZipEntry(fileToSend.getName());
            zos.putNextEntry(ze);
            fis = new FileInputStream(fileToSend);
            byte[] buffer = new byte[4096];
            int readBytesCount = 0;
            while ((readBytesCount = fis.read(buffer)) >= 0) {
                sos.write(buffer, 0, readBytesCount);
            }
            fis.close();
            sos.flush();
            zos.flush();
            zos.closeEntry();
        }
           zos.close();
    }


    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    public String getDirectory() {
        Properties prop;
        String home;
        String fileSeparator;
        String directoryName;

        prop = System.getProperties();
        home = prop.getProperty("user.dir").toString();
        fileSeparator = prop.getProperty("file.separator").toString();
        directoryName = "FileToDownload";

        return home + fileSeparator + directoryName;
    }

}

My Results

1 个答案:

答案 0 :(得分:0)

您正在将文件内容写入ServletOutputStream而不是ZipOutputStream

sos = response.getOutputStream();
zos = new ZipOutputStream(sos);

// ...

while ((readBytesCount = fis.read(buffer)) >= 0) {
    sos.write(buffer, 0, readBytesCount); // <-- should be zos instead of sos
}