用java GUI 写上传和下载图片

一、前言

  • 学习概述:使用JFrame,JPanel,JTextField,JButton等完成功能
  • 学习目标:按钮的监听器、向面板中添加组件等

二、上传下载图片例题

1.概述

通过GUI上传和下载图片,在例题中,我是写死的,如果需要联合数据库,需要自己去解决哦

2.编码实现

package Student.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class View {
    private JFrame jf=new JFrame();
    private JPanel total=new JPanel(new BorderLayout());
    private JPanel pUpload=new JPanel(new FlowLayout(FlowLayout.LEFT));
    private JLabel lFileName=new JLabel("选择要添加的路径");
    private JTextField tFileName=new JTextField(15);
    private JButton bBrowse=new JButton("浏   览");
    private JFileChooser uploadChooser=new JFileChooser();
    private JButton bUpload=new JButton("上   传");
    private JPanel pDownLoad=new JPanel(new FlowLayout(FlowLayout.LEFT));
    private JLabel lDownload=new JLabel("下载图片");
    private JFileChooser downloadChooser=new JFileChooser();
    private JButton bDownload=new JButton("下载");
    private JPanel pIcon=new JPanel(new FlowLayout(FlowLayout.LEFT));
    private ImageIcon icon=new ImageIcon(new ImageIcon(
            ""
    ).getImage().getScaledInstance(320,320,0));
    private JLabel lIcon=new JLabel(icon);
    Dimension faceSize=new Dimension(500,200);
    Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    public void init(){
        pUpload.add(lFileName);
        pUpload.add(tFileName);
        pUpload.add(bBrowse);
        pUpload.add(bUpload);
        bBrowse.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                downloadChooser.setCurrentDirectory(new File("."));
                int result=downloadChooser.showOpenDialog(jf);
                if (result==JFileChooser.APPROVE_OPTION){
                    String path=downloadChooser.getSelectedFile().getPath();
                    tFileName.setText(path);
                }
            }
        });
        bUpload.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fromFileName=tFileName.getText();
                String toFileName="src/Courseimage/image/"+System.currentTimeMillis()+".jpg";
                write(fromFileName,toFileName);
                JOptionPane.showMessageDialog(jf,"上传成功!","提示",JOptionPane.WARNING_MESSAGE);
            }
        });
        pDownLoad.add(lDownload);
        pDownLoad.add(bDownload);
        bDownload.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                downloadChooser.setCurrentDirectory(new File("."));
                int result=uploadChooser.showOpenDialog(jf);
                if (result==JFileChooser.APPROVE_OPTION){
                    String path=uploadChooser.getSelectedFile().getPath();
                    String fromFileName="bin/Courseimage/image/009.png";
                    write(fromFileName,path);
                    JOptionPane.showMessageDialog(jf,"下载成功!","提示",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        pIcon.add(lIcon);
        total.add(pUpload,BorderLayout.NORTH);
        total.add(pDownLoad,BorderLayout.CENTER);
        total.add(pIcon,BorderLayout.SOUTH);
        jf.add(total);
        jf.setSize(faceSize);
        jf.setLocation((int)(screenSize.width-faceSize.getWidth())/2,(int)(screenSize
        .height-faceSize.getHeight())/2);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }
    public void write(String fromFile,String toFile){
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream(fromFile);
            fos=new FileOutputStream(toFile);
            byte[] buf=new byte[1024];
            int hasRead=0;
            while ((hasRead=fis.read(buf))>0){
                fos.write(buf,0,hasRead);
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new View().init();
    }
}

4.输出结果

运行:

上传:

点击浏览:

点击打开 后 ,点击上传,上传在src/Courseimage/image/中,这里的地址需要自己去更改,是用来存放上传图片的地址

下载:

下载的图片我是写死的,需要下载的图片的地址是bin/Courseimage/image/009.png

需要自行更改。

 点击下载:

点击打开,就能完成下载图片

  

我觉得如果出问题,那主要问题应该就是在路径上,路径是可以写相对地址和绝对地址的,这个去理解一下吧。

记得点赞哦

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

絔宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值