Skip to content

Instantly share code, notes, and snippets.

@xenogew
Created March 23, 2016 04:41
Show Gist options
  • Save xenogew/00a3426f5dd8cac0b114 to your computer and use it in GitHub Desktop.
Save xenogew/00a3426f5dd8cac0b114 to your computer and use it in GitHub Desktop.
method snippet for read and send image media file as byte array through REST api
public void getDashboardProfileImage(HttpServletRequest servletRequest,
@RequestParam(value = "username") final String username, HttpServletResponse response) {
try {
String theUsername = URLDecoder.decode(username, "UTF-8");
ProfileImageDetailsDto profileImageDetailsResponse = new ProfileImageDetailsDto();
profileImageDetailsResponse = profileImageDetailsService.getProfileImage(theUsername);
// retrieve mime type of the image
String mimeType = FormatUtils.retrieveMimeTypeBytes(profileImageDetailsResponse.getProfileImage());
IOUtils.write(profileImageDetailsResponse.getProfileImage(), response.getOutputStream());
response.flushBuffer();
response.setContentType(mimeType);
response.setContentLength(profileImageDetailsResponse.getProfileImage().length);
String extension = StringUtils.substringAfterLast(mimeType, "/");
response.setHeader("Content-Disposition", "inline; filename=\"profile_img." + extension + "\"");
response.setStatus(HttpServletResponse.SC_OK);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment