纯java代码文件上传

package aaa;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class FileUpload    implements ActionListener {
    String filesavepath="";
    String filename="";
    
    JFrame jframe=new JFrame("文件上传");
    JLabel jlabel=new JLabel("文件上传:");
    JTextField jtext=new JTextField();
    JButton jbutton=new JButton("选择文件");
    JButton upload=new JButton("上传");

    String columnNames[]={"A","B","C","D","E"};
    Vector<String> columnnameV=new Vector<String>();
    
    JMenuBar jmenubar=new JMenuBar();
    JMenu jMenu1=new JMenu("menu1");
    JMenu jMenu2=new JMenu("menu2");
    
    JMenu jMenu11=new JMenu("menu11");
    
    JMenuItem jemitem111=new JMenuItem("item111");
    
    public FileUpload(){
        jframe.setBounds(300, 200, 800, 600);
        jlabel.setBounds(50, 40, 80, 20);
        jtext.setBounds(120, 40, 300, 20);
        jbutton.setBounds(440, 40,80, 20);
        upload.setBounds(540, 40, 80, 20);
        jmenubar.setBounds(0, 0, 800, 30);
        
        for(int i=0;i<columnNames.length;i++){
            columnnameV.add(columnNames[i]);//获得表头
        }
        Vector<Object> tableValueV = new Vector<Object>();
        for(int row = 1;row<21;row++)    //获得数据
        {
            Vector<Object> rowV = new Vector<Object>();
            for(int column = 0;column<columnNames.length;column++)
            {
                rowV.add(columnNames[column]+row);  //数据
            }
            tableValueV.add(rowV);
        }
        
        JTable jtable=new JTable(tableValueV, columnnameV){
            /**
             *
             */
            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {//设置表格单元格不可以被编辑
                // TODO Auto-generated method stub
                return false;
            }
        };
        JScrollPane jsp=new JScrollPane(jtable,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jsp.setBounds(50, 100, 700, 320);
        jtable.setBorder(new LineBorder(Color.gray));//设置表格边框颜色
        //jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//关闭表格自动调整功能,不加这个,表格可以自适应滚动条的宽度
        jtable.setAutoscrolls(true);
        jMenu11.add(jemitem111);
        jMenu1.add(jMenu11);
        jmenubar.add(jMenu1);
        jmenubar.add(jMenu2);
        
        
        jtext.setEditable(false);
        jtext.setBackground(Color.white);
        jframe.add(jlabel);
        jframe.add(jtext);
        jframe.add(jbutton);
        jframe.add(upload);
        jframe.add(jsp);
        jframe.setLayout(null);
        jframe.setVisible(true);
        jframe.add(jmenubar);
        jbutton.addActionListener(this);
        upload.addActionListener(this);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource().equals(jbutton)){
            JFileChooser jfchooser=new JFileChooser("E:\\");
            jfchooser.setDialogTitle("选择文件");
            int rtn=jfchooser.showOpenDialog(jframe);
            if(rtn==JFileChooser.APPROVE_OPTION) {   
                System.out.println(jfchooser.getSelectedFile().getPath());
                String text=jfchooser.getSelectedFile().getPath();
                jtext.setText(text);
                filename=jfchooser.getSelectedFile().getName();
            }
        }else if(e.getSource().equals(upload)){
            if(!jtext.getText().equals("")){
                //开始上传所选择的的文件,获取文件名,文件存放位置
                JFileChooser fileChooser = new JFileChooser("D:\\");  
                fileChooser.setDialogTitle("选择保存路径");
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int returnVal = fileChooser.showOpenDialog(fileChooser);    
                if(returnVal == JFileChooser.APPROVE_OPTION){  
                    String filePath= fileChooser.getSelectedFile().getAbsolutePath();//这个就是你选择的文件夹的路径                             }     
                    System.out.println(filePath);
                    filesavepath=filePath;
                    try {
                        FileWriter fw = new FileWriter(new File(filesavepath+"/"+filename));
                        InputStream is = new FileInputStream(new File(jtext.getText()));
                        BufferedReader br = new BufferedReader(new InputStreamReader(is,"GBK"));
                        int ch = 0;
                        while((ch = br.read())!=-1 ){    
                            //System.out.print((char)ch);
                            fw.write(ch);
                        }  
                        fw.flush();  
                        fw.close();
                        JOptionPane.showMessageDialog(null, "文件上传成功,请到"+filesavepath+"目录下查看!");
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }else{
                JOptionPane.showMessageDialog(null, "请先选择要上传的文件!");
            }
        }
    }
    
    public static void main(String[] args) {
        FileUpload fileupload=new FileUpload();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值