基于applet的FTP断点上传组件(三)

客户端基本上写完了,剩下的事情就是写一个封装的组件业适合applet的插件的要求

 

FTPCOM.class

  1. package ftpupload;
  2. import java.io.IOException;
  3. public class FTPCOM {
  4.     
  5.     private FTPClient client;
  6.     volatile boolean finish;
  7.     volatile boolean over;
  8.     
  9.     public FTPCOM(String remotehost,int ftphost,String username,String password,int timeout) throws FTPException, IOException{
  10.         
  11.         client=new FTPClient();
  12.         client.setRemoteAddress(remotehost);
  13.         client.setTimeout(timeout);
  14.         client.connect();
  15.         client.login(username, password);
  16.         finish=false;
  17.     }
  18.     
  19.     private void inFolder(String remoteFolder) throws FTPException, IOException{
  20.         client.cd(" /");
  21.         if(!remoteFolder.equals("")){
  22.             String[] t=remoteFolder.split("/");
  23.             StringBuffer sb=new StringBuffer();
  24.             for(int i=0;i<t.length;i++){
  25.                 sb.append(t[i]+"/");
  26.                 client.mkdir(sb.toString());
  27.             }
  28.             client.cd(remoteFolder);
  29.         }
  30.     }
  31.     
  32.     public void transferData(String remoteFolder,String uploadName,String localFile) throws FTPException, IOException{
  33.         
  34.         inFolder(remoteFolder);
  35.         client.autoChooseType(uploadName);
  36.         client.transfer(localFile, uploadName);
  37.         client.quit();
  38.         finish=true;
  39.     }
  40.     
  41.     public void transferData(String remoteFolder,String uploadName,String localFile,FTPProcessListener listener) throws FTPException, IOException{
  42.         
  43.         inFolder(remoteFolder);
  44.         client.setListener(listener);
  45.         client.autoChooseType(uploadName);
  46.         client.transfer(localFile, uploadName);
  47.         client.quit();
  48.         finish=true;
  49.     }
  50.     
  51.     public void quit() throws FTPException, IOException{
  52.         client.quit();
  53.     }
  54.     
  55.     public void stop(){
  56.         
  57.         client.setCancelTransfer(true);
  58.     }
  59.     
  60.     public void recover(){
  61.         client.setCancelTransfer(false);
  62.     }
  63.     
  64.     public boolean isexist(String remoteFile,long rfSize) throws FTPException, IOException{
  65.         
  66.         long size=client.getSize(remoteFile);
  67.         if(size==rfSize)
  68.             return true;
  69.         return false;
  70.     }
  71.     
  72.     public long getStartSize(String remoteFile) throws FTPException, IOException{
  73.         client.autoChooseType(remoteFile);
  74.         return client.getStartSize(remoteFile);
  75.     }
  76.     
  77.     public boolean ishandling(){
  78.         return client.isHandling();
  79.     }
  80.     
  81.     public boolean isover(){
  82.         return client.isOver();
  83.     }
  84. }

OK,逻辑部分基本上算是完成了。下面就是UI部分了,首先是主界面

MainApp.class

  1. package ftpupload;
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.awt.GridLayout;
  9. import java.awt.Insets;
  10. import java.awt.Toolkit;
  11. import java.awt.datatransfer.DataFlavor;
  12. import java.awt.datatransfer.Transferable;
  13. import java.awt.datatransfer.UnsupportedFlavorException;
  14. import java.awt.dnd.DnDConstants;
  15. import java.awt.dnd.DropTarget;
  16. import java.awt.dnd.DropTargetDragEvent;
  17. import java.awt.dnd.DropTargetDropEvent;
  18. import java.awt.dnd.DropTargetEvent;
  19. import java.awt.dnd.DropTargetListener;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.MouseAdapter;
  23. import java.awt.event.MouseEvent;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.OutputStream;
  27. import java.io.UnsupportedEncodingException;
  28. import java.net.HttpURLConnection;
  29. import java.net.MalformedURLException;
  30. import java.net.URL;
  31. import java.net.URLEncoder;
  32. import java.text.DecimalFormat;
  33. import java.util.ArrayList;
  34. import java.util.EventObject;
  35. import java.util.Iterator;
  36. import java.util.List;
  37. import java.util.Properties;
  38. import javax.imageio.ImageIO;
  39. import javax.swing.ImageIcon;
  40. import javax.swing.JApplet;
  41. import javax.swing.JButton;
  42. import javax.swing.JFileChooser;
  43. import javax.swing.JLabel;
  44. import javax.swing.JMenuItem;
  45. import javax.swing.JOptionPane;
  46. import javax.swing.JPanel;
  47. import javax.swing.JPopupMenu;
  48. import javax.swing.JScrollPane;
  49. import javax.swing.JTable;
  50. import javax.swing.JTextField;
  51. import javax.swing.event.CellEditorListener;
  52. import javax.swing.table.DefaultTableModel;
  53. import javax.swing.table.TableCellEditor;
  54. /**
  55.  * 主应用程序
  56.  * @author BruceXX
  57.  *
  58.  */
  59. public class MainApp extends JApplet{
  60.     /**
  61.      * 主面板
  62.      */
  63.     private JPanel mainpanel=null,centerPanel=null,northPanel=null;
  64.     
  65.     private JScrollPane jscrollPane=null;
  66.     /**
  67.      * 图标
  68.      */
  69.     private ImageIcon fileIcon,folderIcon,delIcon;
  70.     /**
  71.      * 最大数量
  72.      */
  73.     private long maxSize;
  74.     /**
  75.      * 待上传的表单文件项
  76.      */
  77.     private List<uploadItem> uploads;
  78.     
  79.     int removeItem;
  80.     private JTextField textField;
  81.     private JButton addButton=null,uploadButton=null;
  82.     
  83.     /**
  84.      * 上传的远程文件夹路径
  85.      */
  86.     public String folderPath;
  87.     private JLabel notice=null,remind=null;
  88.     private JTable jtable=null;
  89.     private JPopupMenu jpopmenu=null;
  90.     private JMenuItem jmenuitem=null;
  91.     
  92.     /**
  93.      * 上传表格初始化模型
  94.      */
  95.     private UploadTableModel uploadmodel=null;
  96.     
  97.     Thread processJob;
  98.     /**
  99.      * HTTP接收servlet
  100.      */
  101.     String ServletAddress;
  102.     /**
  103.      * FTP接收servlet
  104.      */
  105.     String FTPServletAddress;
  106.     /**
  107.      * 当前传送所属文档id
  108.      */
  109.     String docId;
  110.     /**
  111.      * 当前传送所属的表名
  112.      */
  113.     String tabName;
  114.     /**
  115.      * 文档目录id
  116.      */
  117.     String categoryId;
  118.     
  119.     String uploadWay;
  120.     
  121.     Properties p;
  122.     
  123.     private String hostName;
  124.     private String ftpuser;
  125.     private String ftppass;
  126.     private int ftport;
  127.     /**
  128.      * 0表示不支持文件拖曳,1表示支持
  129.      */
  130.     private int isFolderDragSupport;
  131.     
  132.     public MainApp(){
  133.         /**
  134.          * 256MB
  135.          */
  136.         maxSize=  0x10000000L;
  137.         uploads=new ArrayList<uploadItem>();
  138.         
  139.         
  140.         
  141.     }
  142.     
  143.     
  144.     private JPanel getCenterPanel(){
  145.         
  146.         if(centerPanel==null){
  147.         centerPanel=new JPanel();
  148.         GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
  149.         gridBagConstraints4.gridx = 0;
  150.         gridBagConstraints4.anchor = 18;
  151.         gridBagConstraints4.gridy = 3;
  152.         GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
  153.         gridBagConstraints3.fill = 2;
  154.         gridBagConstraints3.gridy = 0;
  155.         gridBagConstraints3.weightx = 0.0D;
  156.         gridBagConstraints3.gridx = 1;
  157.         GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
  158.         gridBagConstraints1.gridx = 0;
  159.         gridBagConstraints1.weightx = 1.0D;
  160.         gridBagConstraints1.weighty = 1.0D;
  161.         gridBagConstraints1.insets = new Insets(0101010);
  162.         gridBagConstraints1.gridwidth = 3;
  163.         gridBagConstraints1.fill = 1;
  164.         gridBagConstraints1.gridy = 2;
  165.         
  166.         
  167.         notice =new JLabel();
  168.         notice.setText("<html><center>拖曳到这里<br>或是点击添加按钮添加/"add/"</html>");
  169.         notice.setHorizontalAlignment(0);
  170.         notice.setFont(new Font("Tahoma"014));
  171.         
  172.         GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
  173.         gridBagConstraints6.fill = 1;
  174.         gridBagConstraints6.gridy = 2;
  175.         gridBagConstraints6.weightx = 1.0D;
  176.         gridBagConstraints6.weighty = 1.0D;
  177.         gridBagConstraints6.gridwidth = 3;
  178.         gridBagConstraints6.insets = new Insets(010010);
  179.         gridBagConstraints6.gridx = 0;
  180.         GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
  181.         gridBagConstraints5.gridx = 0;
  182.         gridBagConstraints5.insets = new Insets(10101010);
  183.         gridBagConstraints5.anchor = 13;
  184.         gridBagConstraints5.gridwidth = 3;
  185.         gridBagConstraints5.gridy = 3;
  186.         GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
  187.         gridBagConstraints10.insets = new Insets(10101010);
  188.         gridBagConstraints10.gridy = 1;
  189.         gridBagConstraints10.weightx = 1.0D;
  190.         gridBagConstraints10.fill = 1;
  191.         gridBagConstraints10.gridx = 0;
  192.         GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
  193.         gridBagConstraints9.gridx = 1;
  194.         gridBagConstraints9.anchor = 13;
  195.         gridBagConstraints9.insets = new Insets(10101010);
  196.         gridBagConstraints9.gridy = 1;
  197.         
  198.         remind=new JLabel();
  199.         remind.setText("AMT源天软件");
  200.         
  201.         centerPanel.setLayout(new GridBagLayout());
  202.         centerPanel.add(getAddButton(), gridBagConstraints9);
  203.         centerPanel.add(remind, gridBagConstraints10);
  204.         centerPanel.add(getUploadButton(), gridBagConstraints5);
  205.         centerPanel.add(getScrollPanel(), gridBagConstraints6);
  206.         centerPanel.add(getNorthPanel(), gridBagConstraints1);
  207.         centerPanel.add(getTextField(), gridBagConstraints3);
  208.         //centerPanel.add(, gridBagConstraints4);
  209.         }
  210.         return centerPanel;
  211.     }
  212.     
  213.     private JTextField getTextField(){
  214.         
  215.         if(textField==null){
  216.             textField=new JTextField();
  217.             textField.setPreferredSize(new Dimension(30,19));
  218.             textField.setVisible(false);
  219.         }
  220.         return textField;
  221.     }
  222.     
  223.     private JPanel getNorthPanel(){
  224.         if(northPanel==null){
  225.          GridBagConstraints gridBagConstraints = new GridBagConstraints();
  226.          gridBagConstraints.fill = 2;
  227.          gridBagConstraints.gridwidth = 2;
  228.          gridBagConstraints.gridx = -1;
  229.          gridBagConstraints.gridy = -1;
  230.          gridBagConstraints.weightx = 1.0D;
  231.          gridBagConstraints.weighty = 1.0D;
  232.          gridBagConstraints.anchor = 10;
  233.          gridBagConstraints.insets = new Insets(030030);
  234.          
  235.          northPanel=new JPanel();
  236.          northPanel.setLayout(new GridLayout());
  237.          northPanel.add(notice,gridBagConstraints);
  238.         }
  239.          return northPanel;
  240.     }
  241.     
  242.     
  243.     
  244.     private JScrollPane getScrollPanel(){
  245.         if(jscrollPane==null){
  246.             
  247.             jscrollPane=new JScrollPane();
  248.             jscrollPane.setVisible(false);
  249.             jscrollPane.setViewportView(getTable());
  250.         }
  251.         return jscrollPane;
  252.         
  253.     }
  254.     /**
  255.      * 弹出菜单的实现,弹出菜单是一个可弹出并显示一系列选项的小窗口。
  256.      * JPopupMenu 用于用户在菜单栏上选择项时显示的菜单。
  257.      * 它还用于当用户选择菜单项并激活它时显示的 右拉式 (pull-right)”菜单。
  258.      * 最后,JPopupMenu 还可以在想让菜单显示的任何其他位置使用.
  259.      * @return
  260.      */
  261.     private JPopupMenu getPopMenu(){
  262.         
  263.         if(jpopmenu==null){
  264.             jpopmenu=new JPopupMenu();
  265.             jpopmenu.add(getMenuItem());
  266.         }
  267.         return jpopmenu;
  268.     }
  269.     
  270.     /**
  271.      * 菜单中的项的实现。菜单项本质上是位于列表中的按钮。当用户选择“按钮”时,
  272.      * 将执行与菜单项关联的操作。JPopupMenu 中包含的 JMenuItem 正好执行该功能
  273.      * 右键出现菜单项
  274.      * @return
  275.      */
  276.     
  277.     private JMenuItem getMenuItem(){
  278.         
  279.         if(jmenuitem==null)
  280.             jmenuitem=new JMenuItem();
  281.         jmenuitem.setText("删除该行");  
  282.         jmenuitem.addActionListener(new ActionListener(){
  283.             public void actionPerformed(ActionEvent e) {
  284.                 // TODO Auto-generated method stub
  285.                 uploads.remove(removeItem);
  286.                 getTable().updateUI();
  287.                 if(uploads.size()==0)
  288.                     deleteFileTrig();
  289.             }           
  290.         });
  291.         return jmenuitem;
  292.     }
  293.     
  294.     private UploadTableModel getUploadModel(){
  295.         
  296.         if(uploadmodel==null){
  297.             
  298.             uploadmodel=new  UploadTableModel();
  299.         }
  300.         
  301.         return uploadmodel;
  302.     }
  303.     
  304.     
  305.     private JTable getTable(){
  306.         
  307.         if(jtable==null){
  308.             jtable=new JTable();
  309.             jtable.setModel(getUploadModel());
  310.             /**
  311.              * 设置表是否绘制单元格之间的垂直线
  312.              */
  313.             jtable.setShowVerticalLines(false);
  314.             /**
  315.              * 设置表是否绘制单元格周围的网格线
  316.              */
  317.             jtable.setShowGrid(false);
  318.             
  319.             jtable.setIntercellSpacing(new Dimension(55));
  320.             jtable.setDefaultRenderer(jtable.getColumnClass(0), new UploadTableCellRender());
  321.             jtable.setRowHeight(32);
  322.             jtable.addMouseListener(new MouseAdapter(){
  323.                 public void mouseClicked(MouseEvent e) {
  324.                     // TODO Auto-generated method stub
  325.                     /**
  326.                      * 点击鼠标左键
  327.                      */
  328.                     if(e.getButton()==MouseEvent.BUTTON1){
  329.                         if(getTable().getColumnName(getTable().columnAtPoint(e.getPoint())).equals("操作")){
  330.                             
  331.                             uploads.remove(getTable().rowAtPoint(e.getPoint()));
  332.                             getTable().updateUI();
  333.                             if(uploads.size()==0)
  334.                                 deleteFileTrig();
  335.                         }
  336.                     }else{
  337.                         removeItem =getTable().rowAtPoint(e.getPoint());
  338.                         getPopMenu().show(e.getComponent(), e.getX(), e.getY());
  339.                         
  340.                     }
  341.                 }           
  342.             });
  343.             
  344.             jtable.setRowMargin(5);
  345.             
  346.             /**
  347.              * 初始化各个列的单元格
  348.              */
  349.             
  350.             UploadNullTableCellEditor nte=new UploadNullTableCellEditor();
  351.             jtable.getColumnModel().getColumn(0).setCellEditor(nte);
  352.             jtable.getColumnModel().getColumn(1).setCellEditor(nte);
  353.             jtable.getColumnModel().getColumn(2).setCellEditor(nte);
  354.             jtable.getColumnModel().getColumn(3).setCellEditor(nte);
  355.             jtable.getColumnModel().getColumn(5).setCellEditor(nte);
  356.             
  357.                 
  358.         }
  359.         return jtable;
  360.     }
  361.     
  362.     private JButton getAddButton(){
  363.         
  364.         addButton=new JButton();
  365.         addButton.setText("添加");
  366.         addButton.addActionListener(new ActionListener(){
  367.             public void actionPerformed(ActionEvent e) {
  368.                 // TODO Auto-generated method stub
  369.                 JFileChooser dlg=new JFileChooser();
  370.                 /**
  371.                  * 允许文件和目录
  372.                  */
  373.                 if(isFolderDragSupport==1)
  374.                     dlg.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  375.                 else
  376.                     dlg.setFileSelectionMode(JFileChooser.FILES_ONLY);
  377.                 /**
  378.                  * 允许多个文件选择
  379.                  */
  380.                 dlg.setMultiSelectionEnabled(true);
  381.                 if(dlg.showOpenDialog(mainpanel)!=JFileChooser.APPROVE_OPTION)
  382.                     return;
  383.                 File[] files=dlg.getSelectedFiles();
  384.                 for(int i=0;i<files.length;i++){
  385.                     File f=files[i];
  386.                     if(f.isFile() ){
  387.                         if(f.length()>maxSize)
  388.                             JOptionPane.showMessageDialog(null"单个文件不能超过"+maxSize/(1024*1024)); 
  389.                         else{
  390.                             uploadItem it=new uploadItem();
  391.                             it.setF(f);
  392.                             it.setUploadName(f.getName());
  393.                         uploads.add(it);
  394.                         
  395. //                      addFileTrig();
  396.                         }
  397.                     }else if(f.isDirectory()){
  398.                         addFolder(f);
  399.                         
  400.                     }
  401.                 }
  402.                 addFileTrig();
  403.                 getTable().updateUI();
  404.             }           
  405.         });
  406.         
  407.         return addButton;
  408.     }
  409.     
  410.     private void addFolder(File f){
  411.         File[] files=f.listFiles();
  412.         for(int i=0;i<files.length;i++){
  413.             addFile(files[i]);
  414.         }
  415.     }
  416.     
  417.     private void addFile(File f){
  418.         
  419.         if(f.isFile()){
  420.             uploadItem it=new uploadItem();
  421.             it.setF(f);
  422.             it.setUploadName(f.getName());
  423.             uploads.add(it);
  424.         }           
  425.         else if(f.isDirectory()){
  426.             File[] files=f.listFiles();
  427.             for(int i=0;i<files.length;i++){
  428.                 addFile(files[i]);
  429.             }
  430.         }
  431.     }
  432.     
  433.     
  434.     
  435.     
  436.     
  437.     
  438.     
  439.     
  440.     
  441.     private void ProcessJob(){
  442.         processJob=new Thread(new Runnable(){
  443.             public void run() {
  444.                 // TODO Auto-generated method stub
  445.                 
  446.                 
  447.                 final UploadProcessFrame frame=new UploadProcessFrame();
  448.                 Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
  449.                 
  450.                 frame.setLocation(dim.width/2-frame.getWidth()/2, dim.height/2-frame.getHeight()/2);
  451.                 
  452.                 
  453.                 while(uploads.size()!=0){
  454.                     frame.setAlwaysOnTop(true);
  455.                     frame.setVisible(true);
  456.                     frame.cancel.setEnabled(true);
  457.                     frame.quit.setEnabled(true);
  458.                     UploadProcessJob job=null;
  459.                     
  460.                     uploadItem it=uploads.get(0);
  461.                     File f=it.getF();
  462.                     /**
  463.                      * 显示文件名
  464.                      */
  465.                     String uploadName=it.getUploadName();
  466.                     /**
  467.                      * HTTP
  468.                      */
  469.                         
  470.                     /**
  471.                      * FTP
  472.                      */
  473.                         try {
  474.                             job= new  UploadProcessJob(hostName,ftport,f,folderPath,ftpuser,ftppass,uploadName);
  475.                         } catch (FTPException e) {
  476.                             // TODO Auto-generated catch block
  477.                             e.printStackTrace();
  478.                         } catch (IOException e) {
  479.                             // TODO Auto-generated catch block
  480.                             e.printStackTrace();
  481.                         }
  482.                     
  483.                     frame.job=job;
  484.                     job.setProcessControl(frame);
  485.                     job.start();
  486.                     
  487.                         try {
  488.                             Thread.sleep(200);
  489.                         } catch (InterruptedException e1) {
  490.                             // TODO Auto-generated catch block
  491.                             e1.printStackTrace();
  492.                         }
  493.                     
  494.                         while(!job.isfinish()){
  495.                             try {
  496.                                 Thread.sleep(500);
  497.                                 if(job.ftpcom.isover()){
  498.                                     frame.label7.setVisible(true);
  499.                                     frame.cancel.setEnabled(false);
  500.                                     frame.quit.setEnabled(false);
  501.                                 }
  502.                             } catch (InterruptedException e) {
  503.                                 // TODO Auto-generated catch block
  504.                                 e.printStackTrace();
  505.                             }
  506.                         }
  507.                         
  508.                         
  509.                         if(job.breakAll){
  510.                             uploads.removeAll(uploads);
  511.                             deleteFileTrig();
  512.                             break;
  513.                         }
  514.                         
  515.                     
  516.                     
  517.                     
  518.                     
  519.                     job.Curstop();
  520.                     frame.label7.setVisible(false);
  521.                     uploads.remove(0);                      
  522.                     jtable.updateUI();
  523. //                  System.out.println("第"+(i+1)+"个上传完毕!");                                     
  524.                 }
  525.                 
  526.                 
  527.                 
  528.                 if(0==uploads.size()){
  529.                     frame.setVisible(false);                    
  530.                     JOptionPane.showMessageDialog(null"上传任务完毕");
  531.                     deleteFileTrig();
  532.                     processJob.interrupt();
  533.                 }
  534.                 }
  535.                 
  536.                         
  537.         });
  538.         
  539.         processJob.start();
  540.         
  541.     }
  542.     
  543.     private JButton getUploadButton(){
  544.         
  545.         if(uploadButton==null){
  546.             
  547.             uploadButton=new JButton();
  548.             uploadButton.setText("开始上传");
  549.             uploadButton.setVisible(false);
  550.             uploadButton.addActionListener(new ActionListener(){
  551.                 public void actionPerformed(ActionEvent e) {
  552.                     ProcessJob();
  553.                 }
  554.                 
  555.             });
  556.         }
  557.         return uploadButton;
  558.     }
  559.     
  560.     long CaculateFileSize(File f){
  561.         
  562.         if(f.isFile()){
  563.             return f.length();
  564.         }
  565.         
  566.             File[] files=f.listFiles();
  567.             long s=0L;
  568.             for(int i=0;i<files.length;i++){
  569.                 s+=CaculateFileSize(files[i]);
  570.             }
  571.         return s;   
  572.         
  573.     }
  574.     
  575.     private String transSize(long s){
  576.         
  577.         if(s<1024L)
  578.             return new StringBuilder(String.valueOf(s)).append("Bytes").toString();
  579.         DecimalFormat df=new DecimalFormat("0.00");
  580.         if(s<0x100000L)
  581.             return new StringBuilder(String.valueOf(df.format((float)s/1024F))).append("KB").toString();
  582.         if(s<0x40000000L) 
  583.             return new StringBuilder(String.valueOf(df.format((float)s/(1024*1024F)))).append("MB").toString();
  584.         
  585.         return "";
  586.     }
  587.     
  588.     
  589.     public void addFileTrig(){
  590.         
  591.         getNorthPanel().setVisible(false);
  592.         getScrollPanel().setVisible(true);
  593.         getUploadButton().setVisible(true);
  594.         getAddButton().setEnabled(false);
  595.          
  596.     }
  597.     
  598.     public void deleteFileTrig(){
  599.         
  600.         
  601.             getNorthPanel().setVisible(true);
  602.             getScrollPanel().setVisible(false);
  603.             getUploadButton().setVisible(false);
  604.             getAddButton().setEnabled(true);
  605.         
  606.     }
  607.     
  608.      
  609.     public void init(){     
  610.         p=new Properties();
  611.         try {
  612.             p.load(this.getClass().getResourceAsStream("upload.properties"));
  613.         } catch (IOException e1) {
  614.             // TODO Auto-generated catch block
  615.             e1.printStackTrace();
  616.         }
  617.         
  618.         folderPath=this.getParameter("folderpath");
  619.         if(folderPath==null)        
  620.             folderPath=p.getProperty("remoteFolder");
  621.         
  622.         
  623.         String folderDragSupport=this.getParameter("isFolderDragSupport");
  624.         if(folderDragSupport==null)
  625.             isFolderDragSupport=1;
  626.         else{
  627.             if(folderDragSupport.equals("0"))
  628.                 isFolderDragSupport=0;
  629.             else
  630.                 isFolderDragSupport=1;
  631.         }
  632.         String maxSize=this.getParameter("maxSize");
  633.         if(maxSize!=null && !maxSize.equals("")){
  634.             try{
  635.             this.maxSize=Long.parseLong(maxSize);
  636.             }catch(Exception e){
  637.                 
  638.             }
  639.         }
  640.         hostName=this.getParameter("hostName");
  641.         if(hostName==null)
  642.             hostName=p.getProperty("hostName");
  643.         ftpuser=this.getParameter("ftpuser");
  644.         if(ftpuser==null)
  645.             ftpuser=p.getProperty("ftpuser");
  646.         ftppass=this.getParameter("ftppass");
  647.         if(ftppass==null)
  648.             ftppass=p.getProperty("ftppass");
  649.         
  650.         String port=this.getParameter("ftport");
  651.         if(port==null)
  652.             port="21";
  653.         ftport=Integer.parseInt(port);
  654.         
  655.         
  656.         
  657.         
  658.             mainpanel=new JPanel();
  659.             mainpanel.setLayout(new BorderLayout());
  660.             mainpanel.add(getCenterPanel(),"Center");
  661.             this.setContentPane(mainpanel);
  662.             try {
  663.                 fileIcon=new ImageIcon(ImageIO.read(getClass().getResourceAsStream("documents.png")));
  664.                 folderIcon =new ImageIcon(ImageIO.read(getClass().getResourceAsStream("folder open.png")));
  665.                 delIcon=new ImageIcon(ImageIO.read(getClass().getResourceAsStream("cancel.png")));
  666.             } catch (IOException e) {
  667.                 // TODO Auto-generated catch block
  668.                 e.printStackTrace();
  669.             }
  670.             new DropTarget(getCenterPanel(), new FileDropTargetListener());
  671.             new DropTarget(getTable(),new FileDropTargetListener());
  672.      }
  673.      
  674.      
  675.      
  676.      
  677.      private class UploadNullTableCellEditor implements TableCellEditor{
  678.         public Component getTableCellEditorComponent(JTable table,
  679.                 Object value, boolean isSelected, int row, int column) {
  680.             // TODO Auto-generated method stub
  681.             return null;
  682.         }
  683.         public void addCellEditorListener(CellEditorListener l) {
  684.             // TODO Auto-generated method stub
  685.             
  686.         }
  687.         public void cancelCellEditing() {
  688.             // TODO Auto-generated method stub
  689.             
  690.         }
  691.         public Object getCellEditorValue() {
  692.             // TODO Auto-generated method stub
  693.             return null;
  694.         }
  695.         public boolean isCellEditable(EventObject anEvent) {
  696.             // TODO Auto-generated method stub
  697.             return false;
  698.         }
  699.         public void removeCellEditorListener(CellEditorListener l) {
  700.             // TODO Auto-generated method stub
  701.             
  702.         }
  703.         public boolean shouldSelectCell(EventObject anEvent) {
  704.             // TODO Auto-generated method stub
  705.             return false;
  706.         }
  707.         public boolean stopCellEditing() {
  708.             // TODO Auto-generated method stub
  709.             return false;
  710.         }
  711.          
  712.          
  713.      }
  714.      
  715.      
  716.      
  717.      
  718.      private class UploadTableModel extends DefaultTableModel{
  719.             
  720.             public int getColumnCount(){
  721.                 return 6;
  722.             }
  723.             
  724.             public int getRowCount(){
  725.                 return uploads.size();
  726.             }
  727.             
  728.             public String getColumnName(int col){
  729.                 
  730.                 switch(col){
  731.                 case 0:return "";
  732.                 case 1:return "文件名称";
  733.                 case 2:return "文件路径";
  734.                 case 3:return "文件大小";
  735.                 case 4:return "上传名";
  736.                 case 5:return "操作";
  737.                 
  738.                 }
  739.                 return "";
  740.             }
  741.             
  742.             
  743.              public Object getValueAt(int row, int col){
  744.                  
  745.                  File f=uploads.get(row).getF();
  746.                  String uploadName=uploads.get(row).uploadName;
  747.                  int pos=uploadName.lastIndexOf(".");
  748.                  if(pos!=-1)
  749.                  uploadName=uploadName.substring(0,uploadName.lastIndexOf("."));
  750.                  
  751.                  if(col==0){
  752.                      if(uploads.get(row).getF().isDirectory())
  753.                          return folderIcon;
  754.                      if(uploads.get(row).getF().isFile())
  755.                          return fileIcon;
  756.                  }
  757.                  if(col==1){
  758.                      
  759.                      return uploads.get(row).getF().getName();
  760.                  }
  761.                  if(col==2){
  762.                      return uploads.get(row).getF().getPath();
  763.                  }
  764.                  if(col==3)
  765.                      return transSize(CaculateFileSize(uploads.get(row).getF())); 
  766.                  if(col==4)
  767.                      return uploadName;
  768.                  if(col==5)
  769.                      return delIcon;
  770.             
  771.                  
  772.                  return null;
  773.              } 
  774.             
  775.              
  776.              public void setValueAt(Object aValue, int row, int col){
  777.                  
  778.                  if(col==4){
  779.                      uploadItem it=uploads.get(row);
  780.                      String fileName=it.getF().getName();
  781.                      String suffix=fileName.substring(fileName.lastIndexOf("."));
  782.                      
  783.                      uploads.get(row).setUploadName(aValue.toString()+suffix);
  784.                  }
  785.                      
  786.              }
  787.             
  788.         }
  789.      
  790.      class FileDropTargetListener implements DropTargetListener {
  791.             
  792.             
  793.           public void dragEnter(DropTargetDragEvent event) {
  794.             if (!isDragAcceptable(event)) {
  795.               event.rejectDrag();
  796.               return;
  797.             }
  798.           }
  799.           public void dragExit(DropTargetEvent event) {
  800.           }
  801.           public void dragOver(DropTargetDragEvent event) { 
  802.           }
  803.           public void dropActionChanged(DropTargetDragEvent event) {
  804.             if (!isDragAcceptable(event)) {
  805.               event.rejectDrag();
  806.               return;
  807.             }
  808.           }
  809.           public void drop(DropTargetDropEvent event) {
  810.             if (!isDropAcceptable(event)) {
  811.               event.rejectDrop();
  812.               return;
  813.             }
  814.             event.acceptDrop(DnDConstants.ACTION_COPY);
  815.             Transferable transferable = event.getTransferable();
  816.             DataFlavor[] flavors = transferable.getTransferDataFlavors();
  817.             for (int i = 0; i < flavors.length; i++) {
  818.               DataFlavor d = flavors[i];       
  819.                 if (d.equals(DataFlavor.javaFileListFlavor)) {
  820.                   List fileList=new ArrayList();
  821.                 try {
  822.                     fileList = (List) transferable
  823.                           .getTransferData(d);
  824.                 } catch (UnsupportedFlavorException e) {
  825.                     // TODO Auto-generated catch block
  826.                     e.printStackTrace();
  827.                 } catch (IOException e) {
  828.                     // TODO Auto-generated catch block
  829.                     e.printStackTrace();
  830.                 }
  831.                 boolean containFolder=false;
  832.                   Iterator iterator = fileList.iterator();
  833.                   while (iterator.hasNext()) {
  834.                     File f = (File) iterator.next();
  835.                         
  836.                     if(f.isFile() ){
  837.                         if(f.length()>maxSize)
  838.                             JOptionPane.showMessageDialog(null"单个文件不能超过"+maxSize/(1024*1024)); 
  839.                         else{
  840.                             uploadItem it=new uploadItem();
  841.                             it.setF(f);
  842.                             it.setUploadName(f.getName());
  843.                         uploads.add(it);
  844.                         
  845.                         }
  846.                     }else if(f.isDirectory()){
  847.                         if(isFolderDragSupport==1)
  848.                             addFolder(f);
  849.                         else
  850.                             containFolder=true;
  851.                     }
  852.                         
  853.                   }
  854.                   if(containFolder){
  855.                       JOptionPane.showMessageDialog(null"当前用户不允许拖曳文件夹");
  856.                   }
  857.                   
  858.                   addFileTrig();
  859.                   jtable.updateUI();
  860.                 } 
  861.             }
  862.             event.dropComplete(true);
  863.           }
  864.           public boolean isDragAcceptable(DropTargetDragEvent event) { 
  865.             return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
  866.           }
  867.           public boolean isDropAcceptable(DropTargetDropEvent event) { 
  868.             return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
  869.           }
  870.           
  871.          }
  872.      
  873.      final class uploadItem {
  874.          
  875.          File f;
  876.          String uploadName;
  877.         public File getF() {
  878.             return f;
  879.         }
  880.         public void setF(File f) {
  881.             this.f = f;
  882.         }
  883.         public String getUploadName() {
  884.             return uploadName;
  885.         }
  886.         public void setUploadName(String uploadName) {
  887.             this.uploadName = uploadName;
  888.         }
  889.          
  890.      }
  891.     }
  892.     
  893.     
  894.     

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值