标题:下载bing每日美图
取消只看楼主
JiaJinz21
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2021-7-15
 问题点数:0 回复次数:0 
下载bing每日美图
下载界面:

程序代码:
package main;

import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.image.BufferedImage;
import import import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionEvent;

public class Main extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private Download d = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    /**
     * Create the frame.
     */
    public Main() {
        this.d = new Download();
        setResizable(false);
        setTitle("\u6BCF\u65E5\u7F8E\u56FE");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 570, 425);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane); 
        contentPane.setLayout(null);
        
        JLabel imageLabel = new JLabel("");
        imageLabel.setBounds(10, 10, 546, 245);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(d.getUrl());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(bi!=null) {
            Image image = bi.getScaledInstance((int)(bi.getWidth()*0.5), (int)(bi.getHeight()*0.5), Image.SCALE_DEFAULT); 
            imageLabel.setIcon(new ImageIcon(image));
        }
        contentPane.add(imageLabel);
        
        JButton btnNewButton = new JButton("\u4E0B\u8F7D");
        btnNewButton.addActionListener((ActionEvent ae)->{
            FileNameExtensionFilter filter  = new FileNameExtensionFilter("*.jpg", "jpg");
            var choose = new JFileChooser();
            choose.setFileFilter(filter);
            choose.showSaveDialog(this);
            File file = choose.getSelectedFile();
            if(!file.getName().endsWith(".jpg")) {
                file = new File(file.getPath()+".jpg");
            }
            d.downloadImage(file);
            JOptionPane.showMessageDialog(this, "下载完成");
        });
        btnNewButton.setBounds(10, 265, 546, 122);
        contentPane.add(btnNewButton);
    }
}

下载类
程序代码:
package main;

import java.awt.image.BufferedImage;
import import import import import import import javax.imageio.ImageIO;

import com.alibaba.fastjson.JSONObject;

public class Download {
    private String host = "https://cn. private URL url = null;
    
    public Download() {
        try {
            url = new URL("https://cn.);
            URLConnection uc = url.openConnection();
            uc.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0");
            uc.setRequestProperty("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            uc.setRequestProperty("connection","keep-alive");
            uc.setRequestProperty("charset", "utf-8");
            uc.connect();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(),"utf-8"));
            String line = "";
            StringBuilder result = new StringBuilder();
            while((line=br.readLine())!=null) {
                result.append(line);
            }
            JSONObject json = JSONObject.parseObject(result.toString());
            url = new URL(host+json.getJSONArray("images").getJSONObject(0).getString("url"));
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public URL getUrl() {
        return url;
    }

    public void downloadImage(File path) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            ImageIO.write(image, "JPEG", path);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
搜索更多相关主题的帖子: java URL Image import new 
2021-08-16 19:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506545-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.075389 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved