簡體   English   中英

如何在 JAVA 的 rest API 中將圖像返回給瀏覽器?

[英]How to return an Image to browser in rest API in JAVA?

當我點擊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

有什么方法可以在點擊 java 中的任何 URL 時返回圖像?

我沒有測試它,因為我在這台機器上沒有環境,但邏輯上它應該像下面這樣工作,將它作為輸入流讀取並讓你的方法返回 @ResponseBody byte[]

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

    BufferedImage image = resizeImage(300, 300, objectKey);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", os);
    InputStream is = new ByteArrayInputStream(os.toByteArray());
    return IOUtils.toByteArray(is);
}

根據@Habooltak Ana 的建議進行更新,無需創建輸入流,代碼應如下所示

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

    BufferedImage image = resizeImage(300, 300, objectKey);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", os);
    return os.toByteArray();
}

您可以使用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();
    }
}

在大多數情況/環境下,只需返回具有正確 HTTP 標頭( Content-TypeContent-Disposition )的文件對象即可。

偽代碼

File result = createSomeJPEG(); 
/*
 e.g.
 RenderedImage rendImage = bufferedImage;
 File file = new File("filename.jpg");
 ImageIO.write(rendImage, "jpg", file);
*/
response().setHeader("Content-Disposition", "attachment;filename=filename.jpg;");
response().setHeader("Content-Type", "image/jpeg");
return ok(result);

也可以看看:

當我按下localhost:8080:/getImage/app/path={imagePath}這樣的API時,我想拍攝一張圖片localhost:8080:/getImage/app/path={imagePath}

當我點擊此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

有什么方法可以在擊中Java中的任何URL時返回圖像?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM