Reputation: 336
I am currently working on JAX-RS, where I'm trying to send and receive Zip files. Currently for sending the zip file in response, I'm able to achieve it using the below code (verified the zip downloading by entering the URL in the browser), but I'm not clear about writing the code logic to read the Zip file from response.
Please help me to achieve this functionality.
@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();
}
Kindly let me know how can I achieve reading the zip file from response of the above code.
Upvotes: 0
Views: 8305
Reputation: 130887
Once you are using the JAX-RS Client API, your client code can be like:
Client client = ClientBuilder.newClient();
InputStream is = client.target("http://localhost:8080")
.path("api").path("getProduct")
.request().accept("application/zip")
.get(InputStream.class);
ZipInputStream zis = new ZipInputStream(is);
Then read the content of the ZipInputStream
.
Upvotes: 4