Getting Image from URL (Java)

前端 未结 6 2078
长发绾君心
长发绾君心 2020-12-10 01:09

I am trying to read the following image

\"enter

But it is showing IIOException

6条回答
  •  清歌不尽
    2020-12-10 01:53

    Directly calling a URL to get an image may concern with major security issues. You need to ensure that you have sufficient rights to access that resource. However You can use ByteOutputStream to read image file. This is an example (Its just an example, you need to do necessary changes as per your requirement.)

    ByteArrayOutputStream bis = new ByteArrayOutputStream();
    InputStream is = null;
    try {
      is = url.openStream ();
      byte[] bytebuff = new byte[4096]; 
      int n;
    
      while ( (n = is.read(bytebuff)) > 0 ) {
        bis.write(bytebuff, 0, n);
      }
    }
    

提交回复
热议问题