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

どうしたの?noobの質問でごめんなさい

4

2 に答える 2

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 に答える
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 に答える