Tomcatにサーブレットの内容をbzip2ファイルとして書き出させようとしています(おそらく愚かな要件ですが、統合作業には明らかに必要です)。私はSpringフレームワークを使用しているので、これはAbstractControllerにあります。
http://www.kohsuke.org/bzip2/のbzip2ライブラリを使用してい ます
内容をbzipで圧縮できますが、ファイルを書き出すと、大量のメタデータが含まれているようで、bzip2ファイルとして認識できません。
これが私がしていることです
// get the contents of my file as a byte array
byte[] fileData = file.getStoredFile();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//create a bzip2 output stream to the byte output and write the file data to it
CBZip2OutputStream bzip = null;
try {
bzip = new CBZip2OutputStream(baos);
bzip.write(fileData, 0, fileData.length);
bzip.close();
} catch (IOException ex) {
ex.printStackTrace();
}
byte[] bzippedOutput = baos.toByteArray();
System.out.println("bzipcompress_output:\t" + bzippedOutput.length);
//now write the byte output to the servlet output
//setting content disposition means the file is downloaded rather than displayed
int outputLength = bzippedOutput.length;
String fileName = file.getFileIdentifier();
response.setBufferSize(outputLength);
response.setContentLength(outputLength);
response.setContentType("application/x-bzip2");
response.setHeader("Content-Disposition",
"attachment; filename="+fileName+";)");
これは、Springabstractcontrollerの次のメソッドから呼び出されています
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
ServletOutputに直接書き込むなど、さまざまなアプローチでいくつかの試みを行いましたが、かなり困惑しており、オンラインで例を見つけることができません。
これまでにこれに出くわしたことがある人からのアドバイスをいただければ幸いです。代替のライブラリ/アプローチは問題ありませんが、残念ながらbzip2化する必要があります。