我在 HtmlUnit 无头浏览器中打开了一个网页。现在该网页包含一个图像 html 标记,如下所示:
<img src="..." />
所以我只想要那个图像。但问题是图像的相同 src URL 显示差异。每次的图像。意思是,如果我们刷新 img src URL,那么它会显示 diff。每次的图像。
那么如何获取显示在html页面上的图像。
我在 HtmlUnit 无头浏览器中打开了一个网页。现在该网页包含一个图像 html 标记,如下所示:
<img src="..." />
所以我只想要那个图像。但问题是图像的相同 src URL 显示差异。每次的图像。意思是,如果我们刷新 img src URL,
这是使用完全合格的 I 存储图像的功能
protected String saveImage(String imageUrl) throws Exception {
InputStream inputStream;
OutputStream os;
ByteArrayOutputStream byteArrayOutputStream;
String destinationFile = "File path where you want ot store the image";
URL url = new URL(imageUrl);
inputStream = url.openStream();
byteArrayOutputStream = new ByteArrayOutputStream();
os = new FileOutputStream(destinationFile);
int read;
String barcode = null;
while ((read = inputStream.read()) != -1) {
os.write(read);
byteArrayOutputStream.write(read);
barcode = byteArrayOutputStream.toString();
}
inputStream.close();
os.close();
byteArrayOutputStream.close();
return barcode;
}