我可以从任何类型的网站导入图像吗?

Posted

技术标签:

【中文标题】我可以从任何类型的网站导入图像吗?【英文标题】:Can I import an image from any type of website? 【发布时间】:2022-01-16 20:54:27 【问题描述】:

我正在尝试使用 Java (IDE IntelliJ) 从 Internet 导入图像,但我不知道如何从谷歌图像中选择图像(在本例中为第一行)。 例如,我试图搜索罗马和那不勒斯的首都,但代码无法从图像 google 的部分中找到任何图像。

可能你对我说的不太了解,所以下面你会发现我写的代码有相对错误

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main
  public static void main(String[] args) 
    String[] listaCapitali = 
            "Roma",
            "Napoli",

    ;

    for (String capitale : listaCapitali) 
        ricercaGoogle("https://www.google.com/search?q=" + capitale + "+cartina&source=lnms&tbm=isch&sa=X&ved=2ahUKEwj-moK1y-D0AhXIzaQKHeXUBLUQ_AUoAXoECAEQAw&cshid=1639392166213289&biw=2240&bih=1082&dpr=2");

    
  

private static void ricercaGoogle(String urlPath) 

    try 
        URL url = new URL(urlPath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        int response = connection.getResponseCode();
        System.out.println(response);

        BufferedImage image = ImageIO.read(url);

        FileOutputStream fos = new FileOutputStream(String.valueOf(image));
        fos.write(response);
        fos.close();

     catch (IOException e) 
        e.printStackTrace();
    
  

错误提示:

403
javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
at Main.ricercaGoogle(Main.java:33)
at Main.main(Main.java:19)

403
javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
at Main.ricercaGoogle(Main.java:33)
at Main.main(Main.java:19)

你能帮我把这些图片下载到我的电脑上,用大写的名字命名吗?非常感谢

【问题讨论】:

首先,403(禁止)响应代码表示您尝试执行 Goggle 不允许您执行的操作。不确定他们的条款是否允许抓取。其次,当您在 Internet 上搜索时,通常会返回 html。您需要解析该 HTML 以获取图像的 URL。最后,当您拥有图像 URL 时,只需将图像文件直接下载并写入磁盘,无需对其进行解码(就像 ImageIO.read(..) 所做的那样)。 【参考方案1】:

要做的第一件事:您的网址不是图片的网址!尝试更改网址

您可以从该代码中获得灵感: 从 HTTPGET 获取图像并将其放在文件中

 new Thread(new Runnable() 
            @Override
            public void run() 
                HttpClient httpclient = HttpClients.createDefault();
                
                /*
                 * put your urlPath here instead of http://localhost...
                 */
                HttpGet httpget = new HttpGet("http://localhost:9090/imageFilm/1");

             
                // Execute and get the response.
                HttpResponse response = null;
                try 
                    response = httpclient.execute(httpget);
                 catch (IOException e1) 
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                
                HttpEntity entity = response.getEntity();

                if (entity != null) 
                    try (InputStream instream = entity.getContent()) 

                    // receiving an image and write it within a file 

                        try (FileOutputStream outputStream = new FileOutputStream(
                                /*
                                 * here i create a file with the image received from the httpGet , you can do other things 
                                 */
                                new File("C:\\Users\\OUSSAMA\\Desktop\\xc.png"), false)) 
                            int read;
                            byte[] bytes = new byte[1024];
                            while ((read = instream.read(bytes)) != -1) 
                                outputStream.write(bytes, 0, read);
                            
                        
                     catch (UnsupportedOperationException | IOException e) 
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    
                
            
        ).start();

需要 Apache HttpClient 依赖项: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.13


一个类中的所有代码:


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
public class Main
  public static void main(String[] args) 
    String[] listaCapitali = 
            "Roma",
            "Napoli",

    ;

    for (String capitale : listaCapitali) 
        //ricercaGoogle("https://www.google.com/search?q=" + capitale + "+cartina&source=lnms&tbm=isch&sa=X&ved=2ahUKEwj-moK1y-D0AhXIzaQKHeXUBLUQ_AUoAXoECAEQAw&cshid=1639392166213289&biw=2240&bih=1082&dpr=2");//Your URL is not an URL of an Image ; you must change it !
             ricercaGoogle("https://i.pinimg.com/originals/1b/75/84/1b758419a811ae05ad4da61acdb7ce22.jpg");
    
  

private static void ricercaGoogle(String urlPath) 
    HttpClient httpclient = HttpClients.createDefault();
    
    /*
     * put your urlPath here instead of http://localhost...
     */
    HttpGet httpget = new HttpGet(urlPath);

     // Execute and get the response.
    HttpResponse response = null;
    try 
        response = httpclient.execute(httpget);
     catch (IOException e1) 
        // TODO Auto-generated catch block
        e1.printStackTrace();
    
    HttpEntity entity = response.getEntity();

    if (entity != null) 
        try (InputStream instream = entity.getContent()) 

        // receiving an image and write it within a file 
             try (FileOutputStream outputStream = new FileOutputStream(
                    /*
                     * here i create a file with the image received from the httpGet , you can do other things 
                     */
                    new File("C:\\Users\\Mourad\\Desktop\\xc.png"), false)) 
                int read;
                byte[] bytes = new byte[1024];
                while ((read = instream.read(bytes)) != -1) 
                    outputStream.write(bytes, 0, read);
                
            
         catch (UnsupportedOperationException | IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    

 

【讨论】:

在哪里可以导入这些类? IntelliJ 它不会让我这样做 @FrancescoZanoncelli 答案已更新为 Apache HttpClient 的 Maven 依赖项,此外,您的 URL 不是图像的 µURL,所有代码都在一个类中,我在这里对其进行了测试,它工作正常 出现此错误:httpclient 定义的类与 android 现在提供的类冲突。解决方案包括查找没有相同问题的较新版本或替代库(例如,对于httpclient,请改用HttpUrlConnectionokhttp),或使用jarjar 之类的东西重新打包库。 @FrancescoZanoncelli 你可以使用 HttpUrlConnection ,尝试阅读文档并使用相同的方法,这个答案可能对你有帮助 ***.com/questions/26898667/… // 这个答案也是:***.com/questions/54355038/…

以上是关于我可以从任何类型的网站导入图像吗?的主要内容,如果未能解决你的问题,请参考以下文章

从网站获取图像列表并显示它

动态水印图像会影响网站性能吗?

它们是在网站中使用的任何内容可编辑驱动的组件吗?

试图将数据从网站导入熊猫

带宏的excel无法在sharepoint上显示吗

我可以在我的网站上使用 atto 编辑器吗?