I am using Jersey (ver 1.9.1) to implement RESTful web service for png images. I'm using Apache HttpClient (ver. 4x) at client side. The code on client side calls HttpGet to download image. On successful download, it saves the InputStream from HttpEntity to the disk. Now the problem is resulting file and the file on the server is different. The output image file produced by client code is not Render-able.
@GET
@Path("/public/profile/{userId}")
@Produces({ "image/png" })
public Response getImage(@PathParam(value = "userId") String userId) {
Response res = null;
// ImageManagement.gerProfilePicture(userId) returns me profile picture
// of the provided userId in PathParam
File imageFile = ImageManagement.getProfilePicture(userId);
if (imageFile == null) {
res = Response.status(Status.NOT_FOUND).build();
} else {
res = Response
.ok(imageFile, "image/png")
.header("Content-Disposition",
"attachment; filename=Img" + userId + ".png")
.build();
}
return res;
}
My client code below invokes above resource method
private File downloadProfilePicture(String userId) throws IOException{
// URIHelper is a utility class, this give me uri for image resource
URI imageUri = URIHelper.buildURIForProfile(userId);
HttpGet httpGet = new HttpGet(imageUri);
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
File imageFile = null;
if (statusCode == HttpURLConnection.HTTP_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
Header[] headers = httpResponse.getHeaders("Content-Disposition");
imageFile = new File(OUTPUT_DIR, headers[0].getElements()[0]
.getParameterByName("filename").getValue());
FileOutputStream foutStream = new FileOutputStream(imageFile);
httpEntity.writeTo(foutStream);
foutStream.close();
}
return imageFile;
}
Now problem is the file exists on the server and file downloaded are different.
Below is the dump of the file exists on the server.
Below is the dump of the downloaded file.
You can see, some bytes are being changed. Is Jersey server api modifying the data in stream from file? What is going wrong?
Update:
If I hit the same url from browser, it downloads the file but downloaded file is not viewable. So the issue seems associated with server.