2017-11-22 2 views
0

현재 다운로드 용 zip 파일을 만들어야합니다. 여기에는 문자열 변수에서 생성되는 두 개의 CSV 파일이 들어 있어야합니다. 나는 어떻게해야합니까? 내 초안은 아래와 같습니다.문자열 변수에서 text/csv가 포함 된 zip 파일 다운로드 생성

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"이 표시됩니다. 당신의 충고에 감사드립니다. 미리 감사드립니다.

답변

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

내가 좋아하지 않는 유일한 점은 임시 파일을 만들고 처리 후 삭제해야한다는 것입니다.