Created
March 23, 2016 04:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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