小编Sha*_*vil的帖子

如何在JAVA中的rest API中将图像返回到浏览器?

我想要一个像我这样的API的图像 localhost:8080:/getImage/app/path={imagePath}

当我点击这个API时,它会返回一个Image.

这可能吗?

实际上,我试过这个,但它给了我一个错误.这是我的代码,

@GET
@Path("/app")
public BufferedImage getFullImage(@Context UriInfo info) throws MalformedURLException, IOException {
    String objectKey = info.getQueryParameters().getFirst("path");

    return resizeImage(300, 300, objectKey);
}


public static BufferedImage resizeImage(int width, int height, String imagePath)
        throws MalformedURLException, IOException {
    BufferedImage bufferedImage = ImageIO.read(new URL(imagePath));
    final Graphics2D graphics2D = bufferedImage.createGraphics();
    graphics2D.setComposite(AlphaComposite.Src);
    // below three lines are for RenderingHints for better image quality at cost of
    // higher processing time
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.drawImage(bufferedImage, 0, 0, width, height, null);
    graphics2D.dispose(); …
Run Code Online (Sandbox Code Playgroud)

java spring java-api restful-url

6
推荐指数
1
解决办法
4857
查看次数

标签 统计

java ×1

java-api ×1

restful-url ×1

spring ×1