Skip to content

Instantly share code, notes, and snippets.

@vanduc1102
Last active April 10, 2016 17:00
Show Gist options
  • Save vanduc1102/f4c7adab0862cb604799 to your computer and use it in GitHub Desktop.
Save vanduc1102/f4c7adab0862cb604799 to your computer and use it in GitHub Desktop.
Java RESTeasy return PDF IMAGE file.
@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();
}
}
@vanduc1102
Copy link
Author

The fundamental is return the file as binary and set header for as file type. Browser will automatically open the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment