Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
画像を返すDropwizardリソース
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
You can’t perform that action at this time.