Created
August 27, 2015 10:00
-
-
Save yusuke2255/f6d039faeb2008e65d5e to your computer and use it in GitHub Desktop.
画像を返すDropwizardリソース
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
package my.sample.resources; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Response; | |
@Path("/") | |
public class SampleResource { | |
@GET | |
@Path("/image") | |
public Response getTopPage() { | |
try { | |
BufferedImage image = ImageIO.read(new File("/Users/yusuke/Pictures/yaruo.jpeg")); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ImageIO.write(image, "jpeg", baos); | |
byte[] imageData = baos.toByteArray(); | |
return Response.ok(imageData, "image/jpeg").build(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return Response.serverError().build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment