1

有什么问题,如果不保存在硬盘驱动器上,我就无法下载图像

try {
         sentenceLabel= new JLabel(new ImageIcon(ImageIO.read(new URL("http://www.google.ru/intl/en_com/images/logo_plain.png"))));
    } catch (MalformedURLException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } catch (IOException ex) {
        // "http://img.yandex.net/i/wiz"+imgType.trim()+".png"
        ex.printStackTrace();
    }

怎么了?对不起菜鸟问题

4

2 回答 2

1

你的代码完全没问题。这向我显示了内容窗格中的图像。

    JFrame frame = new JFrame(); 
    frame.setTitle("Polygons"); 
    frame.setSize(550, 550); 
    Container contentPane = frame.getContentPane(); 
    try {
     JLabel sentenceLabel= new JLabel(new ImageIcon(
                  ImageIO.read(new URL(
                     "http://www.google.ru/intl/en_com/images/logo_plain.png"))));
     contentPane.add(sentenceLabel);
    } catch (MalformedURLException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } catch (IOException ex) {
        // "http://img.yandex.net/i/wiz"+imgType.trim()+".png"
        ex.printStackTrace();
    }
    frame.show(); 
于 2012-12-18T18:37:37.433 回答
1

您可以使用此代码下载图像:

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

public class ImageDownloader
{      
    public static void main(String[] args )
    {
        BufferedImage image =null;
        try{

            URL url =new URL("http://developerfeed.com/sites/default/files
                             /have_a_question.png");
            // read the url
           image = ImageIO.read(url);

            for png
            ImageIO.write(image, "png",new File("/tmp/have_a_question.png"));

            // for jpg
            ImageIO.write(image, "jpg",new File("/tmp/have_a_question.jpg"));

        }catch(IOException e){
            e.printStackTrace();
        }
    }}
于 2012-12-18T18:26:14.607 回答