【问题标题】:HtmlUnit Save Image QualityHtmlUnit 保存图像质量
【发布时间】:2015-08-23 22:42:57
【问题描述】:

我正在创建代码以使用 HtmlUnit 从 Web 下载一些图像。 但是当我用 HtmlUnit 保存图像时,图像的质量与原始图像相比非常低。 我使用此代码将 img url 保存到文件中。

 try {
              HtmlImage imgX = (HtmlImage) 
              page.getByXPath("//img").get(0);
              Thread.sleep(3000);

              File imageFile = new File(dir +"/"+ name);
              imgX.saveAs(imageFile);
              System.out.println("Done!!!!");
 } catch (Exception e) {
              e.printStackTrace();
 }

是否有其他选项可以获得最佳质量的图像?

我试过用这个: How to convert an <img... in html to byte [] in Java 但是没有创建图像。

【问题讨论】:

  • 你能把你的完整案例贴上实际的图片吗?

标签: htmlunit


【解决方案1】:

HtmlImage 获取WebResponse

InputStream inputStream = imgX.getWebResponse(true).getContentAsStream();

然后直接在 InputStream 上工作:

File imageFile = new File(dir +"/"+ Name);
FileOutputStream outputStream = new FileOutputStream(imageFile);
IOUtils.copy(inputStream, outputStream);

【讨论】:

  • 我很乐意提供帮助。不要忘记将答案标记为正确答案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多