2012-04-24 20 views
14

Sto cercando di leggere l'immagine seguenteOttenere Immagine da URL (Java)

enter image description here

Ma sta mostrando IIOException.

Ecco il codice:

Image image = null; 
URL url = new URL("http://bks6.books.google.ca/books?id=5VTBuvfZDyoC&printsec=frontcover&img=1& zoom=5&edge=curl&source=gbs_api"); 
image = ImageIO.read(url); 
jXImageView1.setImage(image); 
+4

... e l'eccezione è ...? – Marcelo

+0

javax.imageio.IIOException ... "Impossibile ottenere il flusso di input dall'URL!" –

+0

Hai bloccato la tua applicazione nel tuo firewall? – Tobi

risposta

7

Hai trovato un errore HTTP 400 (Bad Request) perché c'è una space nell'URL. Se lo aggiustate (prima del parametro zoom), otterrete un errore HTTP 400 (Non autorizzato). Forse hai bisogno di un'intestazione HTTP per identificare il tuo download come browser riconosciuto (usa l'intestazione "User-Agent") o un ulteriore parametro di autenticazione.

Per l'esempio User-Agent, quindi utilizzare il ImageIO.read(InputStream) utilizzando l'InputStream collegamento:

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

Usare qualsiasi necessario per xxxxxx

+0

Spazio c'è solo per errore .. In ogni caso come aggiungere le informazioni di intestazione? –

+2

Ho aggiunto un esempio. – JScoobyCed

+0

Ho lo stesso problema ma quando aggiungo Image image = ImageIO.read (url) ottengo un errore - Impossibile risolvere il simbolo IO –

2

direttamente chiamando un URL per ottenere un'immagine può riguardare con i maggiori problemi di sicurezza. È necessario assicurarsi di disporre di diritti sufficienti per accedere a tale risorsa. Tuttavia è possibile utilizzare ByteOutputStream per leggere il file di immagine. Questo è un esempio (Il suo solo un esempio, è necessario fare le modifiche necessarie secondo il vostro requisito.)

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

Try This:

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 lenna = 
     new URL("http://bks6.books.google.ca/books?id=5VTBuvfZDyoC&printsec=frontcover&img=1& zoom=5&edge=curl&source=gbs_api"); 

    final ImageComponent image = new ImageComponent(lenna); 

    JFrame frame = new JFrame("Test"); 
    frame.add(new JScrollPane(image)); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 300); 
    frame.setVisible(true); 
    } 

}

5

Prova questo:

//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

questo non funzionerà correttamente se url ha parametri di query – geek

13

Questo codice ha funzionato bene per 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

Ciao. C'è una API che restituirebbe l'immagine se fornisco un collegamento al sito web? http://stackoverflow.com/questions/38612896/android-load-thumbnail-from-website-url-using-glide-picasso – Zen

+1

prova libreria JSoup. Ecco il link di riferimento http://stackoverflow.com/questions/12465586/how-can-i-download-an-image-using-jsoup –

+0

Grazie! mi ha aiutato! –