現在、JAX-RSでZipファイルを送受信しようとしています。現在、応答でzipファイルを送信するために、私は以下のコードを使用してそれを達成することができます(ブラウザにURLを入力してzipのダウンロードを確認しました)が、Zipファイルを読むためのコードロジックを書くのは不明です応答から。JavaのレスポンスからZipファイルを読み取るには?
この機能を達成するために手伝ってください。
@GET
@Produces({"application/zip"})
@Path("getProduct")
public Response getProduct() {
String METHODNAME = "getProduct";
if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
LOGGER.entering(CLASSNAME, METHODNAME);
}
File file;
try {
Properties prop = getProp();
file = new File(prop.getProperty("ZipLocation")+prop.getProperty("ProductZip"));
byte[] buffer = new byte[5120];
FileOutputStream fos = new FileOutputStream(file);
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry(prop.getProperty("ProductOutfile"));
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream("C:\\Documents\\ProductExtract.xml");
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
zos.close();
} catch (FileNotFoundException ex) {
LOGGER.logp(Level.SEVERE, CLASSNAME, METHODNAME, ex.getMessage(), ex);
return Response.status(204).entity(ex.getMessage()).build();
} catch (Exception ex) {
LOGGER.logp(Level.SEVERE, CLASSNAME, METHODNAME, ex.getMessage(), ex);
return Response.status(204).entity(ex.getMessage()).build();
}
return Response.ok(file, "application/zip").header("Content-Disposition", "attachment; filename=\""+file.getName()+"\"")
.header("Content-Type", "application/zip").header("Set-Cookie", "fileDownload=true; path=/").build();
}
上記のコードの応答からどのようにzipファイルを読み取ることができますか教えてください。
これと同じである必要がありますか? http://stackoverflow.com/questions/23869228/how-to-read-file-from-zip-using-inputstream – Ming
クライアントはどのように見えますか? Javaクライアントですか? JAX-RSクライアントAPIを使用していますか? –
@CássioMazzochiMolinそのJavaクライアントです。 JAX-RS APIのみを使用します。私は、JavaからのこのサービスのHTTP呼び出しを行うつもりです。 –