【问题标题】:Using JSoup to save the contents of this url: http://www.aw20.co.uk/images/logo.png to a file使用 JSoup 将这个 url: http://www.aw20.co.uk/images/logo.png 的内容保存到文件中
【发布时间】:2016-08-08 19:45:26
【问题描述】:

我尝试使用JSoup来获取这个urlhttp://www.aw20.co.uk/images/logo.png的内容,也就是图片logo.png,并将其保存到一个文件中。到目前为止,我已经使用 JSoup 连接到http://www.aw20.co.uk 并获取了一个文档。然后我去找到了我正在寻找的图像的绝对网址,但现在不知道如何获得实际图像。所以我希望有人能指出我这样做的正确方向吗?无论如何我也可以使用 Jsoup.connect("http://www.aw20.co.uk/images/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);
}
}

【问题讨论】:

标签: java image jsoup


【解决方案1】:

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

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

ignoreContentType(true) 已设置,否则 Jsoup 将抛出内容不可 HTML 解析的异常——在这种情况下没关系,因为我们使用 bodyAsBytes() 来获取响应正文,而不是解析。

查看Jsoup Connection API了解更多详情。

【讨论】:

    【解决方案2】:

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

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

    FileUtils.copyURLToFile(URL source, File destination);
    

    只有一行。

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 如上所述,通过此方法下载的一些二进制文件可能已损坏。
    【解决方案4】:

    您可以使用这些方法或这些方法的一部分来解决您的问题。 注意:IMAGE_HOME 是绝对路径。例如/home/你的名字/文件夹名

    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();
       }
    

    }

    【讨论】:

      【解决方案5】:

      还有我可以使用 Jsoup.connect("http://www.aw20.co.uk/images/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();
                 }
              }
           }
      

      【讨论】:

      • 我不允许使用 URL 类来执行此操作,这就是我一开始尝试使用 JSoup 的原因。
      • @user1644544:关于,"I am not allowed to use the URL class to do this"——这种疯狂限制的理由是什么?为什么不能使用最基本的类来轻松地允许访问互联网资源?
      猜你喜欢
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多