当前位置: 首页 > 知识库问答 >
问题:

如何在rest API中用Java向浏览器返回图像?

翟默
2023-03-14

当我点击像localhost:8080:/getimage/app/path={imagePath}这样的API时,我想要一个图像

当我点击这个API时,它将返回给我一个图像。

@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();
    System.out.println(bufferedImage.getWidth());
    return bufferedImage;
}
java.io.IOException: The image-based media type image/webp is not supported for writing

共有1个答案

孔城
2023-03-14

你可以用IOUTILS。下面是代码示例。

@RequestMapping(path = "/getImage/app/path/{filePath}", method = RequestMethod.GET)
public void getImage(HttpServletResponse response, @PathVariable String filePath) throws IOException {
    File file = new File(filePath);
    if(file.exists()) {
        String contentType = "application/octet-stream";
        response.setContentType(contentType);
        OutputStream out = response.getOutputStream();
        FileInputStream in = new FileInputStream(file);
        // copy from in to out
        IOUtils.copy(in, out);
        out.close();
        in.close();
    }else {
        throw new FileNotFoundException();
    }
}
 类似资料:
  • 问题内容: 我有一个运行着Jersey REST资源的Web服务器,我想知道如何获取浏览器img标签的image / png参考;提交表单或获得Ajax响应后。用于添加图形的图像处理代码正在运行,只需以某种方式返回即可。 码: 干杯 问题答案: 我不认为在REST服务中返回图像数据是一个好主意。它占用了应用程序服务器的内存和IO带宽。最好将任务委派给针对这种传输进行了优化的适当的Web服务器。您可

  • 问题内容: TL; DR; 上传之前,有没有一种方法可以直接在浏览器端压缩图像(主要是jpeg,png和gif)?我很确定JavaScript可以做到这一点,但是我找不到实现它的方法。 这是我要实现的完整方案: 用户访问我的网站,然后通过元素选择图片, 该图片是通过JavaScript检索的,我们进行了一些验证,例如正确的文件格式,最大文件大小等, 如果一切正常,则会在页面上显示图像的预览, 用户

  • 终端输出 我正在尝试获取存储在mongodb数据库中的图像,以便检索并渲染到浏览器。我已经尝试过一些以前的解决方案,如btoa、Windows。btoa,但它说两者都没有定义。(https://stackoverflow.com/questions/6182315/how-to-do-base64-encoding-in-node-js)(https://stackoverflow.com/que

  • 我的需求: 先从列表=>详情页 如果在详情页点击的返回按钮,则读取之前缓存的历史数据 (在详情页刷新后再点击返回,也符合条件) 此时的返回包括: 浏览器的返回按钮 页面中的按钮(调用router.back()/go(-1)等路由方法) 要如何全局判断用户进行了返回操作呢?

  • 我试图在浏览器中打开xls工作表,而不是在MS Excel中。我尝试使用但是不起作用。这是execute方法的完整代码: 无论如何,这可以从Microsoft Office excel应用程序打开excel,但只能通过从NetBeans运行项目。如果我尝试在没有Netbeans的情况下从Tomcat打开它,它是不起作用的。

  • 问题内容: 以下是我的tls后端: 密钥是使用以下两行生成的: 当我启动tls服务器并使用浏览器(https://example.com:8443)访问站点时, 在 忽略浏览器警告 后 ,我得到了预期的结果: 到目前为止,一切都很酷。 现在,当我将浏览器指向http://example.com:8443(注意使用的是http, 而不是 https)时,我得到了Firfox的以下结果(Chrome浏