Getting Image from URL (Java)

前端 未结 6 2067
长发绾君心
长发绾君心 2020-12-10 01:09

I am trying to read the following image

\"enter

But it is showing IIOException

相关标签:
6条回答
  • 2020-12-10 01:34

    This code worked fine for me.

     import java.io.FileOutputStream;
     import java.io.IOException;
     import java.io.InputStream;
     import java.io.OutputStream;
     import java.net.URL;
    
    public class SaveImageFromUrl {
    
    public static void main(String[] args) throws Exception {
        String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
        String destinationFile = "image.jpg";
    
        saveImage(imageUrl, destinationFile);
    }
    
    public static void saveImage(String imageUrl, String destinationFile) throws IOException {
        URL url = new URL(imageUrl);
        InputStream is = url.openStream();
        OutputStream os = new FileOutputStream(destinationFile);
    
        byte[] b = new byte[2048];
        int length;
    
        while ((length = is.read(b)) != -1) {
            os.write(b, 0, length);
        }
    
        is.close();
        os.close();
    }
    
    }
    
    0 讨论(0)
  • 2020-12-10 01:37

    Try This:

    //urlPath = address of your picture on internet
    URL url = new URL("urlPath");
    BufferedImage c = ImageIO.read(url);
    ImageIcon image = new ImageIcon(c);
    jXImageView1.setImage(image);
    
    0 讨论(0)
  • 2020-12-10 01:50

    You can try the this class to displays an image read from a URL within a JFrame.

    public class ShowImageFromURL {
    
        public static void show(String urlLocation) {
            Image image = null;
            try {
                URL url = new URL(urlLocation);
                URLConnection conn = url.openConnection();
                conn.setRequestProperty("User-Agent", "Mozilla/5.0");
    
                conn.connect();
                InputStream urlStream = conn.getInputStream();
                image = ImageIO.read(urlStream);
    
                JFrame frame = new JFrame();
                JLabel lblimage = new JLabel(new ImageIcon(image));
                frame.getContentPane().add(lblimage, BorderLayout.CENTER);
                frame.setSize(image.getWidth(null) + 50, image.getHeight(null) + 50);
                frame.setVisible(true);
    
            } catch (IOException e) {
                System.out.println("Something went wrong, sorry:" + e.toString());
                e.printStackTrace();
            }
        }
    }
    

    Ref : https://gist.github.com/aslamanver/92af3ac67406cfd116b7e4e177156926

    0 讨论(0)
  • 2020-12-10 01:51

    You are getting an HTTP 400 (Bad Request) error because there is a space in your URL. If you fix it (before the zoom parameter), you will get an HTTP 400 error (Unauthorized). Maybe you need some HTTP header to identify your download as a recognised browser (use the "User-Agent" header) or additional authentication parameter.

    For the User-Agent example, then use the ImageIO.read(InputStream) using the connection inputstream:

    URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-Agent", "xxxxxx");
    

    Use whatever needed for xxxxxx

    0 讨论(0)
  • 2020-12-10 01:53

    Directly calling a URL to get an image may concern with major security issues. You need to ensure that you have sufficient rights to access that resource. However You can use ByteOutputStream to read image file. This is an example (Its just an example, you need to do necessary changes as per your requirement.)

    ByteArrayOutputStream bis = new ByteArrayOutputStream();
    InputStream is = null;
    try {
      is = url.openStream ();
      byte[] bytebuff = new byte[4096]; 
      int n;
    
      while ( (n = is.read(bytebuff)) > 0 ) {
        bis.write(bytebuff, 0, n);
      }
    }
    
    0 讨论(0)
  • 2020-12-10 01:54

    Try:

    public class ImageComponent extends JComponent {
      private final BufferedImage img;
    
      public ImageComponent(URL url) throws IOException {
        img = ImageIO.read(url);
        setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
    
      }
    
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
      }
    
      public static void main(String[] args) throws Exception {
        final URL kitten = new URL("https://placekitten.com/g/200/300");
    
        final ImageComponent image = new ImageComponent(kitten);
    
        JFrame frame = new JFrame("Test");
        frame.add(new JScrollPane(image));
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setVisible(true);
      }
    }
    
    0 讨论(0)
提交回复
热议问题