1. 程式人生 > >Swing簡單的檔案上傳

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

}

package com.iss.iaf.codemanagement;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

public class UpLoad {

 public void UpLoadFile(String title, Object selectedValue) {
  JFrame jframe = new JFrame(title);// 例項化一個JFrame
  JPanel jPanel = new JPanel(); // 建立一個輕量級容器
  JToolBar jToolBar = new JToolBar(); // 提供了一個用來顯示常用的 Action 或控制元件的元件
  jframe.setVisible(true);// 可見
  jframe.setSize(500, 500);// 窗體大小
  jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// close的方式
  jframe.setContentPane(jPanel); // 設定 contentPane 屬性。
  JLabel jl = new JLabel("請選擇:");// 建立一個Label標籤
  jl.setHorizontalAlignment(SwingConstants.LEFT);// 樣式,讓文字居中
  jPanel.add("North", jl);// 將標籤新增到容器中
  jPanel.add("North", jToolBar);
  JButton developer = new JButton("上傳檔案");
  developer.setHorizontalAlignment(SwingConstants.CENTER);
  jToolBar.add(developer);// 上傳檔案按鈕新增到容器
  jPanel.add("North", jToolBar);
  developer.addMouseListener(new MouseAdapter() { // 新增滑鼠點選事件
     public void mouseClicked(MouseEvent event) {
      eventOnImport(new JButton());
     }
    }); // 檔案上傳功能
 }

 /**
  * 檔案上傳功能
  *
  * @param developer
  *            按鈕控制元件名稱
  */
 public static void eventOnImport(JButton developer) {
  JFileChooser chooser = new JFileChooser();
  chooser.setMultiSelectionEnabled(true);
  /** 過濾檔案型別 * */
  FileNameExtensionFilter filter = new FileNameExtensionFilter("war",
    "xml", "txt", "doc", "docx");
  chooser.setFileFilter(filter);
  int returnVal = chooser.showOpenDialog(developer);
  if (returnVal == JFileChooser.APPROVE_OPTION) {
   /** 得到選擇的檔案* */
   File[] arrfiles = chooser.getSelectedFiles();
   if (arrfiles == null || arrfiles.length == 0) {
    return;
   }
   FileInputStream input = null;
   FileOutputStream out = null;
   String path = "./";
   try {
    for (File f : arrfiles) {
     File dir = new File(path);
     /** 目標資料夾 * */
     File[] fs = dir.listFiles();
     HashSet<String> set = new HashSet<String>();
     for (File file : fs) {
      set.add(file.getName());
     }
     /** 判斷是否已有該檔案* */
     if (set.contains(f.getName())) {
      JOptionPane.showMessageDialog(new JDialog(),
        f.getName() + ":該檔案已存在!");
      return;
     }
     input = new FileInputStream(f);
     byte[] buffer = new byte[1024];
     File des = new File(path, f.getName());
     out = new FileOutputStream(des);
     int len = 0;
     while (-1 != (len = input.read(buffer))) {
      out.write(buffer, 0, len);
     }
     out.close();
     input.close();
    }
    JOptionPane.showMessageDialog(null, "上傳成功!", "提示",
      JOptionPane.INFORMATION_MESSAGE);

   } catch (FileNotFoundException e1) {
    JOptionPane.showMessageDialog(null, "上傳失敗!", "提示",
      JOptionPane.ERROR_MESSAGE);
    e1.printStackTrace();
   } catch (IOException e1) {
    JOptionPane.showMessageDialog(null, "上傳失敗!", "提示",
      JOptionPane.ERROR_MESSAGE);
    e1.printStackTrace();
   }
  }
 }

}