2011-01-13 1 views
5

私の目標は、複数のjava.io.Fileオブジェクトをzipファイルに入れて、ユーザーがダウンロードできるようにHttpServletResponseに出力することです。"仮想ファイル"のリストのためにZIPファイルを作成してhttpservletresponseに出力する方法

ファイルは、JAXBマーシャーによって作成されました。これはjava.io.Fileオブジェクトですが、ファイルシステム上には実際には存在しません(メモリ内にのみあります)ので、FileInputStreamを作成することはできません。

私が見たすべてのリソースは、Zipファイルの内容を出力するためにOutputStreamを使用しています。しかし、これらのリソースはすべてFileInputStreamを使用しています(使用できません)。

これをどのように達成できるかご存知ですか?

+3

あなたはそれが 'java.io.File'何を意味するのですか? 'File'オブジェクトは単なる空想的なアドレスオブジェクトです。それはどんなファイルコンテンツも運びません。あなたのデータはどこですか? – erickson

答えて

3

は私がばか判明:)だった「作成」されていたファイルを無効なパスに保存し、例外を飲み込むので、私はそれが "作成された" okだと思った。しかし、新しいFileInputStreamをインスタンス化しようとすると、ファイルが存在しなかったと訴えました。 brainfartを持っていて、java.io.Fileオブジェクトに実際にどこかにファイル情報が含まれていると仮定しました。しかし、エリクソンが指摘したように、それは間違っていた。

ありがとうございましたコードのRalph、無効なパスの問題を解決した後に使用しました。

マイコード:

ZipOutputStream out = new ZipOutputStream(response.getOutputStream()); 
byte[] buf = new byte[1024]; 

File file; 
InputStream in; 
// Loop through entities 
for (TitleProductAccountApproval tpAccountApproval : tpAccountApprovals) { 
    // Generate the file  
    file = xmlManager.getXML(
     tpAccountApproval.getTitleProduct().getTitleProductId(), 
     tpAccountApproval.getAccount().getAccountId(), 
     username); 

    // Write to zip file 
    in = new FileInputStream(file); 
    out.putNextEntry(new ZipEntry(file.getName())); 

    int len; 
    while ((len = in.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 

    out.closeEntry(); 
    in.close(); 
} 

out.close(); 
+0

不正なパスの問題? - 私の答えのコードにseriosのバグはありますか? – Ralph

+0

あなたのコードは完璧ではありませんでした。私のせいでした。 – Corey

6

Apache Commons Compress libaryをご覧ください。必要な機能があります。

もちろん、「エリクソン」はあなたの質問に忠実です。ファイルconentetが必要で、java.io.Fileオブジェクトは必要ありません。私の例では、fileNummer番目のファイルのファイル内容(メモリ内)を返すメソッド byte[] getTheContentFormSomewhere(int fileNummer)があると仮定します。 - もちろん、この機能は貧弱なデザインですが、ilustrationのためだけです。

それはこのようにビットを動作するはずです:あなたのhttpコントローラの

void compress(final OutputStream out) { 
    ZipOutputStream zipOutputStream = new ZipOutputStream(out); 
    zipOutputStream.setLevel(ZipOutputStream.STORED); 

    for(int i = 0; i < 10; i++) { 
    //of course you need the file content of the i-th file 
    byte[] oneFileContent = getTheContentFormSomewhere(i); 
    addOneFileToZipArchive(zipOutputStream, "file"+i+"."txt", oneFileContent); 
    } 

    zipOutputStream.close(); 
} 

void addOneFileToZipArchive(final ZipOutputStream zipStream, 
      String fileName, 
      byte[] content) { 
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName); 
    zipStream.putNextEntry(zipEntry); 
    zipStream.write(pdfBytes); 
    zipStream.closeEntry(); 
} 

Snipets:

HttpServletResponse response 
... 
    response.setContentType("application/zip"); 
    response.addHeader("Content-Disposition", "attachment; filename=\"compress.zip\""); 
    response.addHeader("Content-Transfer-Encoding", "binary"); 
    ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream(); 
    compress(outputBuffer); 
    response.getOutputStream().write(outputBuffer.toByteArray()); 
    response.getOutputStream().flush(); 
    outputBuffer.close();