使用JSoup将此URL的内容:http://www.aw20.co.uk/images/logo.png保存到文件中

我尝试使用JSoup来获取此URL的内容http://sofzh.miximages.com/java/logo.png ,这是image logo.png,并将其保存到文件中。 到目前为止,我已经使用JSoup连接到http://www.aw20.co.uk并获取文档。 然后我找到了我正在寻找的图像的绝对url,但现在我不知道如何获得实际图像。 所以我希望有人能指出我正确的方向吗? 无论如何我也可以使用Jsoup.connect(“http://sofzh.miximages.com/java/logo.png”)。get(); 得到图像?

import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JGet2 { public static void main(String[] args) { try { Document doc = Jsoup.connect("http://www.aw20.co.uk").get(); Elements img = doc.getElementsByTag("img"); for (Element element : img) { String src = element.absUrl("src"); System.out.println("Image Found!"); System.out.println("src attribute is: " + src); if (src.contains("logo.png") == true) { System.out.println("Success"); } getImages(src); } } catch (IOException e) { e.printStackTrace(); } } private static void getImages(String src) throws IOException { int indexName = src.lastIndexOf("/"); if (indexName == src.length()) { src = src.substring(1, indexName); } indexName = src.lastIndexOf("/"); String name = src.substring(indexName, src.length()); System.out.println(name); } } 

如果您不想将其解析为HTML,则可以使用Jsoup来获取任何URL并将数据作为字节获取。 例如:

 byte[] bytes = Jsoup.connect(imgUrl).ignoreContentType(true).execute().bodyAsBytes(); 

ignoreContentType(true)被设置,因为否则Jsoup会抛出一个exception,内容不是HTML可解析的 – 在这种情况下没问题,因为我们使用bodyAsBytes()来获取响应体,而不是解析。

查看Jsoup Connection API以获取更多详细信息。

Jsoup不是为下载url内容而设计的。

由于您可以使用第三方库,因此您可以尝试使用apache common IO将给定URL的内容下载到文件:

 FileUtils.copyURLToFile(URL source, File destination); 

它只有一条线。

这种方法效果不好。 使用时请小心。

 byte[] bytes = Jsoup.connect(imgUrl).ignoreContentType(true).execute().bodyAsBytes(); 

您可以使用这些方法或部分方法来解决您的问题。 注意:IMAGE_HOME是绝对路径。 例如/ home / yourname / foldername

 public static String storeImageIntoFS(String imageUrl, String fileName, String relativePath) { String imagePath = null; try { byte[] bytes = Jsoup.connect(imageUrl).ignoreContentType(true).execute().bodyAsBytes(); ByteBuffer buffer = ByteBuffer.wrap(bytes); String rootTargetDirectory = IMAGE_HOME + "/"+relativePath; imagePath = rootTargetDirectory + "/"+fileName; saveByteBufferImage(buffer, rootTargetDirectory, fileName); } catch (IOException e) { e.printStackTrace(); } return imagePath; } public static void saveByteBufferImage(ByteBuffer imageDataBytes, String rootTargetDirectory, String savedFileName) { String uploadInputFile = rootTargetDirectory + "/"+savedFileName; File rootTargetDir = new File(rootTargetDirectory); if (!rootTargetDir.exists()) { boolean created = rootTargetDir.mkdirs(); if (!created) { System.out.println("Error while creating directory for location- "+rootTargetDirectory); } } String[] fileNameParts = savedFileName.split("\\."); String format = fileNameParts[fileNameParts.length-1]; File file = new File(uploadInputFile); BufferedImage bufferedImage; InputStream in = new ByteArrayInputStream(imageDataBytes.array()); try { bufferedImage = ImageIO.read(in); ImageIO.write(bufferedImage, format, file); } catch (IOException e) { e.printStackTrace(); } 

}

无论如何我也可以使用Jsoup.connect(“http://sofzh.miximages.com/java/logo.png”)。get(); 得到图像?

不,JSoup只会获得文本等,但不能用于下载文件或二进制数据。 话虽这么说,只需使用您通过JSoup获得的文件名和路径,然后使用标准Java I / O下载该文件。

我用NIO做了下载。 即

  String imgPath = // ... url path to image String imgFilePath = // ... file path String URL imgUrl; ReadableByteChannel rbc = null; FileOutputStream fos = null; try { imgUrl = new URL(imgPath); rbc = Channels.newChannel(imgUrl.openStream()); fos = new FileOutputStream(imgFilePath); // setState(EXTRACTING + imgFilePath); fos.getChannel().transferFrom(rbc, 0, 1 << 24); } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (rbc != null) { try { rbc.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } }