代码之家  ›  专栏  ›  技术社区  ›  Juni

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

  •  0
  • Juni  · 技术社区  · 8 年前

    我目前需要创建一个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;
        }
    }
    

    显然这不起作用。可能是因为我还是个新手。请容忍我。

    我在调用“zout.putNextEntry”的行中得到一个“java.lang.NullPointerException”。非常感谢您的建议。提前谢谢你。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Juni    8 年前

    我环顾四周一天后解决了问题。这对我来说很有用。但我不确定这是否是最有效的方法。

        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();
        }
    

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

    推荐文章