简体   繁体   English

如何使用 JAVA 从 REST API 调用打印/读取图像

[英]How to print/read image from REST API call using JAVA

I have one REST URL which if I use in postman it gives me image in response.我有一个 REST URL 如果我在 postman 中使用它,它会给我图像作为响应。 Now, I'm trying to see the same thing with JAVA code in Eclipse.现在,我试图在 Eclipse 中看到与 JAVA 代码相同的东西。 (I'm looking for either saving image file or showing with JFrame or Applet etc.) (我正在寻找保存图像文件或使用 JFrame 或 Applet 等显示)

    String url = "https://test-api.com/v1/abc.jpg";

    // Creating URL Connection
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    
    // Defining the request method. It can be GET, POST, PUT, DELETE
    con.setRequestMethod("GET");
    
    // Getting the HTTP Response code like 200, 404 etc.
    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url + "\n");
    System.out.println("Response Code : " + responseCode + "\n");
    
    // Saving response in the Stringbuffer
    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    
    
    byte bytes[] = String.valueOf(response).getBytes();
    
    InputStream inS = new ByteArrayInputStream(bytes);
    int width = 963;    //width of the image
    int height = 640;   //height of the image
    BufferedImage image = null;
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    BufferedImage bImageFromConvert = ImageIO.read(inS);    
    

When I run the code, I get the exception like below.当我运行代码时,我得到如下异常。

javax.imageio.IIOException: Invalid JPEG file structure: two SOI markers
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:628)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:347)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:495)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:621)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1078)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1058)
    at javax.imageio.ImageIO.read(ImageIO.java:1448)
    at javax.imageio.ImageIO.read(ImageIO.java:1352)

Response code comes 200. So, not sure what I'm doing wrong here.响应代码为 200。所以,不确定我在这里做错了什么。 Why ImageIO cannot read the inputstream and picking up two SOI marker.为什么 ImageIO 无法读取输入流并拾取两个 SOI 标记。

To show an image on the browser using jax-rs you can use this method with the hypothese that you have images folder under resources in your classpath:要使用 jax-rs 在浏览器上显示图像,您可以使用此方法,假设您的类路径中的资源下有 images 文件夹:

@GET
@Path("image")
@Produces({"image/jpeg"})
public Response getImage() {
    BufferedImage image = null;
    byte[] imageData = null;
    try {
        //System.out.println("chemin courant : " + ClassLoader.getSystemClassLoader().
        //      getResource(".").getPath());
        image = ImageIO.read(ClassLoaderTestResource.class
                        .getResourceAsStream("/images/yourimage.jpg"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        imageData = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("image : "+ image);
    System.out.println("Image data : "+ imageData);
    return Response.ok(new ByteArrayInputStream(imageData)).build();
}

so if you call http://localhost:8080/image for example you will have your image in the browser.因此,例如,如果您调用 http://localhost:8080/image,您将在浏览器中看到您的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM