从字符串变量创建包含text / csv的zip文件的下载

时间:2017-11-22 05:32:37

标签: java spring-mvc httpresponse zipfile downloading

我目前需要创建一个zip文件供下载。这应该包含两个(2)csv文件,这些文件将从字符串变量创建。我对如何做到这一点感到茫然。我的草案如下。

public @ResponseBody Object getFileV1(HttpServletRequest request, HttpServletResponse response) {
  try {
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=Reassigned Tickets Report " + new Date().toString()  + ".zip");
        String stringValue1 = "This is a test value for csv1";
        String stringValue2 = "This is a test value for csv2";
        InputStream is1 = new ByteArrayInputStream(stringValue1.getBytes("UTF-8"));
        InputStream is2 = new ByteArrayInputStream(stringValue2.getBytes("UTF-8"));
        ZipInputStream zin;
        ZipEntry entry;
        ZipOutputStream zout= new ZipOutputStream(response.getOutputStream());

        zin = new ZipInputStream(is1);
        entry = zin.getNextEntry();
        zout.putNextEntry(entry); 

        zin = new ZipInputStream(is2);
        entry = zin.getNextEntry();
        zout.putNextEntry(entry); 

        zout.closeEntry();
        zin.close();
        zout.close();

        response.flushBuffer();
        return null;

    } catch (Exception e) {
        e.printStackTrace();
        return e;
    }
}

显然这不起作用。可能是因为我还是新手。请多多包涵。

我得到了一个" java.lang.NullPointerException"在" zout.putNextEntry"叫做。非常感谢您的建议。提前谢谢。

1 个答案:

答案 0 :(得分:0)

经过一天的环顾,我解决了我的问题。这适合我。但我不确定这是否是最有效的方式。

    public @ResponseBody Object getFileV1(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=Test Report " + new Date().toString()  + ".zip");
            String stringValue1 = "This is a test value for csv1";
            String stringValue2 = "This is a test value for csv2";

            PrintWriter writer1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("stringValue1.csv"), "UTF-8"));
            writer1.print(stringValue1);
            writer1.close();

            PrintWriter writer2 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("stringValue2.csv"), "UTF-8"));
            writer2.print(stringValue2);
            writer2.close();


            File file1 = new File("stringValue1.csv");
            File file2 = new File("stringValue2.csv");
            filesToZip(response, file1, file2);     

            file1.delete();
            file2.delete();

            response.flushBuffer();
            return null;

        } catch (Exception e) {
            e.printStackTrace();
            return e;
        }
    }

这是我通过一些编辑从另一个线程获得的方法。

    public static void filesToZip(HttpServletResponse response, File... files) throws IOException {
        // Create a buffer for reading the files
        byte[] buf = new byte[1024];
        // create the ZIP file
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        // compress the files
        for(int i=0; i<files.length; i++) {
            FileInputStream in = new FileInputStream(files[i].getName());
            // add ZIP entry to output stream
            out.putNextEntry(new ZipEntry(files[i].getName()));
            // transfer bytes from the file to the ZIP file
            int len;
            while((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            // complete the entry
            out.closeEntry();
            in.close();
        }
        // complete the ZIP file
        out.close();
    }

我唯一不喜欢的是我必须创建临时文件并在处理后删除它们。