Swing文件下载功能


package com.iss.iaf.codemanagement;
import javax.swing.JOptionPane;
/**
 * 代码管理应用程序--项目的入口
 * @author xinzhangah
 * @data 2016-12-02
 *
 */
public class JFrameMain {
 /**
  * @param args
  */
 public static void main(String[] args) {
  Object[] possibleValues = {"开发人员", "测试人员/客户/现场" };
  Object selectedValue =JOptionPane.showInputDialog(null, "请选择身份角色:",
    "选择角色:", JOptionPane.INFORMATION_MESSAGE, null, possibleValues,
    possibleValues[0]);
  if("开发人员".equals(selectedValue)){
   new UpLoad().UpLoadFile("上传文件",selectedValue);
  }else if("测试人员/客户/现场".equals(selectedValue)){
   new DownLoad();
  }
  
 }
}
-------------------------------文件下载类------------------------------------------
package com.iss.iaf.codemanagement;
import java.awt.Container;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class DownLoad implements ActionListener{
 JFrame frame = new JFrame("");// 框架布局 
    JTabbedPane tabPane = new JTabbedPane();// 选项卡布局 
    Container con = new Container();// 
    JLabel label1 = new JLabel("文件目录"); 
    JLabel label2 = new JLabel("选择文件"); 
    JTextField text1 = new JTextField("./");// TextField 目录的路径 
    JTextField text2 = new JTextField("./");// 文件的路径 
    JButton button1 = new JButton("...");// 选择 
    JButton button2 = new JButton("...");// 选择 
    JFileChooser jfc1 = new JFileChooser();// 文件选择器 
    JFileChooser jfc2 = new JFileChooser();// 文件选择器 
    JButton button3 = new JButton("确定");//  下载按钮
    JTextField jTextField1 = new JTextField();
     
    DownLoad() { 
        double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth(); 
        double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight(); 
        frame.setLocation(new Point((int) (lx / 2) - 200, (int) (ly / 2) - 200));// 设定窗口出现位置 
        frame.setSize(500, 500);// 设定窗口大小 
        frame.setContentPane(tabPane);// 设置布局 
        label1.setBounds(10, 10, 70, 20); 
        text1.setBounds(75, 10, 120, 20); 
        button1.setBounds(210, 10, 50, 20); 
        label2.setBounds(10, 35, 70, 20); 
        text2.setBounds(75, 35, 120, 20); 
        button2.setBounds(210, 35, 50, 20); 
        button3.setBounds(30, 60, 60, 20); 
        button1.addActionListener(this); // 添加事件处理 
        button2.addActionListener(this); // 添加事件处理 
        button3.addActionListener(this); // 添加事件处理 
        con.add(label1); 
        con.add(text1); 
        con.add(button1); 
        con.add(label2); 
        con.add(text2); 
        con.add(button2); 
        con.add(button3); 
        frame.setVisible(true);// 窗口可见 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 使能关闭窗口,结束程序 
        tabPane.add("下载页面", con);// 添加布局1 
    } 
    /**
     * 时间监听的方法
     */ 
    public void actionPerformed(ActionEvent e) { 
        if (e.getSource().equals(button1)) {// 判断触发方法的按钮是哪个 
            jfc1.setFileSelectionMode(1);// 设定只能选择到文件夹 
            int state = jfc1.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句 
            if (state == 1) { 
                return; 
            } else { 
                File f = jfc1.getSelectedFile();// f为选择到的目录 
                text1.setText(f.getAbsolutePath()); 
            } 
        } 
        // 绑定到选择文件,先择文件事件 
        if (e.getSource().equals(button2)) { 
            jfc2.setFileSelectionMode(0);// 设定只能选择到文件 
            int state = jfc2.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句 
            if (state == 1) { 
                return;// 撤销则返回 
            } else { 
                File f = jfc2.getSelectedFile();// f为选择到的文件 
                text2.setText(f.getAbsolutePath()); 
            } 
        } 
        if (e.getSource().equals(button3)) {
         
         String filePath= jfc2.getSelectedFile().getAbsolutePath(); //源文件路径
   String fName = jfc2.getSelectedFile().getName(); //源文件名
   String path= jfc1.getSelectedFile().getAbsolutePath();  //目标路径
   try {
    download(fName,filePath,path);
   } catch (IOException e1) {
    e1.printStackTrace();
   }
        }
    } 
    /**
     *
     * @param from_file_name  源文件名
     * @param path 源文件路径
     * @param to_path 目标路径
     * @throws IOException
     */
    public void download(String from_file_name,String path, String to_path)throws IOException {
   OutputStream output = null;
   FileInputStream input =null;
   try {
    input = new FileInputStream(path);
    byte[] buffer = new byte[1024];
    File des = new File(to_path, from_file_name);
    output = new FileOutputStream(des);
    int len = 0;
    while (-1 != (len = input.read(buffer))) {
     output.write(buffer, 0, len);
    }
    
    if (output != null) {
     try {
      if (input != null)
       output.close();
       input.close();
       JOptionPane.showMessageDialog(null, "下载代码", "提示", 2);
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   } catch (FileNotFoundException e1) {
    JOptionPane.showMessageDialog(null, "下载失败!", "提示",
      JOptionPane.ERROR_MESSAGE);
    e1.printStackTrace();
   } catch (IOException e1) {
    JOptionPane.showMessageDialog(null, "下载失败!", "提示",
      JOptionPane.ERROR_MESSAGE);
    e1.printStackTrace();
   }
    }
   
}

--------------------------
 
 

猜你喜欢

转载自blog.csdn.net/yingmengxuepingbao/article/details/53486272