Utilizing REST jersey on the server side combined with AngularJS on the client side.
My task involves downloading a zip file requested by the client for a specific date range.
Server-side code: //Currently, I have hardcoded a single zip file for testing purposes
@POST
@Path("/LogRange")
@Produces({MediaType.APPLICATION_OCTET_STREAM} )
@Consumes({ MediaType.APPLICATION_JSON} )
public Response getLogsBetween(@HeaderParam(HttpHeaders.AUTHORIZATION) String authorization,
@Context HttpServletRequest request, @Context HttpServletResponse response, LogFolders folders){
StreamingOutput stream = new StreamingOutput(){
@Override
public void write(OutputStream arg0) {
// TODO Auto-generated method stub
BufferedOutputStream bus = new BufferedOutputStream(arg0);
try {
File file = new File("C:\\ProgramData\\ABC\\Logfiles\\UI.zip");
FileInputStream fizip = new FileInputStream(file);
byte[] buffer2 = IOUtils.toByteArray(fizip);
bus.write(buffer2);
bus.flush();
bus.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
return Response.status(Response.Status.OK).entity(stream).header("Content-Disposition","attachment; filename=\"log.zip\"").build();
}
Client-side code:
$http({
url : urlBase + endPoint,
method: "POST",
data: formData, //this is your json data string
headers : {
'Content-Type' : "application/json",
'Authorization' : authCode,
},
responseType: 'arraybuffer'
}).success(function(response, status, headers, config) {
var blob = new Blob([response], { type: "application/zip" });
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}).error(function (data, status, headers, config) {
//upload failed
});
The downloaded file is appearing corrupted and displaying incorrectly when opened locally. If you have any advice on how to properly download the file, please provide your assistance. https://i.sstatic.net/FSpsO.png