Last active
April 10, 2016 17:00
-
-
Save vanduc1102/f4c7adab0862cb604799 to your computer and use it in GitHub Desktop.
Java RESTeasy return PDF IMAGE file.
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
@Path("media") | |
@Stateless | |
public class MediaRestService { | |
private final static org.slf4j.Logger log = LoggerFactory.getLogger(MediaRestService.class); | |
@EJB | |
private ThumbnailServices thumbnailService; | |
@EJB | |
private DocumentServices docServices; | |
@Path("document/{id}/thumbnail") | |
@GET | |
@Produces("image/jpg") | |
public Response getThumbnailOfDocument(@PathParam("id") String id) { | |
try { | |
String base64Content = thumbnailService.getThumbnail(id); | |
byte[] imageData = IOUtils.convertToBytesFromBase64(base64Content,"jpg"); | |
return Response.ok(new ByteArrayInputStream(imageData)).build(); | |
} catch (ConversionException | EntityNotFoundException | IOException ex) { | |
log.debug("Error: " + ex.getMessage()); | |
return Response.ok().build(); | |
} | |
} | |
@GET | |
@Path("document/{id}/file") | |
@Produces("application/pdf") | |
public Response findContentBase64(@PathParam("id") String documentId) { | |
byte[] bytes = null; | |
try { | |
try { | |
bytes = org.apache.commons.io.IOUtils.toByteArray(docServices.fileContent(documentId)); | |
} catch (UnknownHostException | EntityNotFoundException ex) { | |
log.error("Error: " + ex.getMessage()); | |
} | |
} catch (ConversionException | IOException ex) { | |
log.error("Error: " + ex.getMessage()); | |
} | |
return Response.ok(new ByteArrayInputStream(bytes)).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The fundamental is return the file as binary and set header for as file type. Browser will automatically open the file.