Im trying to create and download a zip file in java using restful service. But its not working for me. Please find the code below:
@GET
@Path("/exportZip")
@Produces("application/zip")
public Response download(@QueryParam("dim") final String dimId,
@QueryParam("client") final String clientId,
@QueryParam("type") final String type,
@Context UriInfo ui) {
System.out.println("Start");
ResponseBuilder response = null ;
String filetype = "";
if(type.equalsIgnoreCase("u")){
filetype = "UnMapped";
}else if(type.equalsIgnoreCase("m")){
filetype = "Mapped";
}
try {
byte[] workbook = null;
workbook = engineService.export(dim, client, type);
InputStream is = new ByteArrayInputStream(workbook);
FileOutputStream out = new FileOutputStream(filetype + "tmp.zip");
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
int n = is.read(buf);
while (n >= 0) {
out.write(buf, 0, n);
n = is.read(buf);
}
response = Response.ok((Object) out);
response.header("Content-Disposition",
"attachment; filename=\"" + filetype + " - " + new Date().toString() + ".zip\"");
out.flush();
out.close();
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("End");
return response.build();
}
This is giving me the following error: javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.io.FileOutputStream, and Java type class java.io.FileOutputStream, and MIME media type application/zip was not found
You haven't added MIME type of your response . Your browser will be confused while processing this response. It needs response content type. To set response content type for your response, add following code ,
after
thanks.