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

来源:互联网 发布:阿里云app有域名交易吗 编辑:程序博客网 时间:2024/05/20 22:48

 OK,主界面的表格中需要相关的修饰器,和作业调度以及作业窗口情况

 

UploadTableCellRender.class

 

  1. package ftpupload;
  2. import java.awt.Component;
  3. import java.io.File;
  4. import javax.swing.Icon;
  5. import javax.swing.JLabel;
  6. import javax.swing.JTable;
  7. import javax.swing.table.TableCellRenderer;
  8. /**
  9.  * 
  10.  * @author BruceXX
  11.    单元格渲染器,用来将单元格内的东东转换为其它显示内容
  12.  */
  13. public class UploadTableCellRender implements TableCellRenderer {
  14.     public Component getTableCellRendererComponent(JTable table, Object value,
  15.             boolean isSelected, boolean hasFocus, int row, int column) {
  16.         // TODO Auto-generated method stub
  17.         JLabel label=null;
  18.         if(value instanceof Icon){
  19.             label=new JLabel((Icon)value);
  20.         }else if(value instanceof String){
  21.             label=new JLabel(value.toString());
  22.             label.setToolTipText(value.toString());
  23.         }else if(value instanceof File){
  24.             
  25.             File file=(File)value;
  26.             label=new JLabel(file.getName());
  27.             label.setToolTipText(file.getPath());
  28.         }
  29.         
  30.         return label;
  31.     }
  32. }

UploadProcessJob.class

 

  

  1. package ftpupload;
  2. import java.awt.Component;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.text.DecimalFormat;
  6. /**
  7.  * 
  8.  * @author BruceXX
  9.  *  上传进度计时作业
  10.  */
  11. public class UploadProcessJob {
  12.     
  13.       long totalbytes;
  14.      long starttime;
  15.     /**
  16.      * 被更新的组件,这里为uploadProcessFrame
  17.      */
  18.     Component control;
  19.     /**
  20.      * 更新线程
  21.      */
  22.     Thread updateThread;
  23.     
  24.     Thread uploadThread;
  25.     
  26.     Thread processThread;
  27.     FTPCOM ftpcom;
  28.     
  29.     FTPProcessMointor monitor;
  30.     
  31.     File localFile;
  32.     
  33.     String remoteFolder;
  34.     
  35.     
  36.     String uploadName;
  37.     
  38.     boolean breakAll=false;
  39.     boolean breakCur=false;
  40.     
  41.     volatile long startSize;
  42.     
  43.     UploadProcessJob(String remoteHost,int ftpport,File localFile,String remoteFolder,String username,String pass,String uploadName) throws  IOException, FTPException{
  44.         
  45. //      this.way=2;
  46.         totalbytes=localFile.length();      
  47.         starttime=System.currentTimeMillis();
  48.         this.remoteFolder=remoteFolder;
  49.         this.localFile=localFile;
  50.         this.uploadName=uploadName;
  51.         monitor=new FTPProcessMointor();
  52.         ftpcom=new FTPCOM(remoteHost,ftpport,username,pass,100*1000);
  53.         
  54.     }
  55.     
  56. //  UploadProcessJob(File localFile,String remoteFolder,String ServletAddress,String uploadName){
  57. //      this.way=1;     
  58. //      totalbytes=localFile.length();
  59. //      starttime=System.currentTimeMillis();
  60. //      this.remoteFolder=remoteFolder;
  61. //      this.localFile=localFile;
  62. //      this.uploadName=uploadName;
  63. //      httpcom=new HTTPCOM(ServletAddress);
  64. //      
  65. //  }
  66.     
  67.     public void start(){
  68.         
  69.         backgroundFlush();
  70.         reUpload();
  71.         
  72.     }
  73.     
  74.     
  75.     void transferData(){
  76.         uploadThread = new Thread(new Runnable() {
  77.             public void run() {
  78.                 // TODO Auto-generated method stub
  79.                     try {
  80.                         startSize=ftpcom.getStartSize(uploadName);
  81.                         ftpcom.transferData(remoteFolder, uploadName, localFile.getPath(), monitor);
  82.                     } catch (IOException e1) {
  83.                         // TODO Auto-generated catch block
  84.                         e1.printStackTrace();
  85.                     } catch (FTPException e) {
  86.                         // TODO Auto-generated catch block
  87.                         e.printStackTrace();
  88.                     }
  89.                 
  90.             }
  91.         });
  92.         uploadThread.start();
  93.     }
  94.     
  95.     void backgroundFlush(){
  96.         
  97.         updateThread=new Thread(
  98.             new Runnable(){
  99.                 public void run(){
  100.                     while(!ftpcom.isfinish()){
  101.                         UpdateUI();
  102.                         try{
  103.                             Thread.sleep(500);
  104.                         }catch(InterruptedException e){}
  105.                         
  106.                     }
  107.                     
  108.                 }
  109.             }
  110.         );
  111.         updateThread.start();       
  112.     }
  113.     
  114.     
  115.     
  116.     /**
  117.      * 停止FTP上传
  118.      */
  119.     public void stop(){
  120.             ftpcom.stop();
  121.     }
  122.     
  123.     /**
  124.      * 重新开始上传
  125.      */
  126.     public void reUpload(){
  127.             ftpcom.recover();
  128.         transferData();
  129.     }
  130.     
  131.     
  132.     public void UpdateUI(){
  133.         
  134.         
  135.         UploadProcessFrame dlg= (UploadProcessFrame)control;
  136.         
  137.         /**
  138.          * 更新uploadprocessFrame中的内容
  139.          */
  140.         long endSize=0;
  141.         long donebytes=0;
  142.         endSize=startSize+monitor.uploadSize;       
  143.         donebytes=monitor.uploadSize;
  144.         dlg.percent.setText((new StringBuilder(String.valueOf((int)((float)(100L * (endSize)) / (float)totalbytes)))).append("%").toString());
  145.         String filepath=localFile.getPath();
  146.         int len=filepath.length();
  147.         filepath="当前上传:"+(filepath.length()>30?filepath.substring(0,10)+"..."+filepath.substring(len-10, len):filepath);
  148.         dlg.curFilepath.setText(filepath);
  149.         dlg.getProcessBar().setValue((int)((float)(10000L * endSize) / (float)totalbytes));
  150.         dlg.totalBytes.setText(shortSize(totalbytes));
  151.         dlg.doneBytes.setText(shortSize(endSize));
  152.         
  153.         long timepast=System.currentTimeMillis()-starttime;
  154.         dlg.timePast.setText(getCurTime(timepast));
  155.             
  156.             if(timepast!=0L)
  157.                 dlg.avgSpeed.setText(new StringBuilder(String.valueOf(shortSize((long)((float)1000*donebytes/(float)(timepast))))).append("/秒").toString());
  158.             if(donebytes!=0L){
  159.                 /**
  160.                  * 估计剩余的时间
  161.                  */
  162.                 if(timepast!=0){
  163.                 long speed=donebytes/timepast;
  164.                 if(speed!=0){
  165.                 long timeleft=(totalbytes-endSize)/speed;
  166.                 dlg.timeLeft.setText(getCurTime(timeleft));
  167.                 }
  168.                 }
  169.             }               
  170.         
  171.         
  172.     }
  173.     
  174.     protected String shortSize(long s){
  175.         if(s<1024L)
  176.             return new StringBuilder(String.valueOf(s)).append("Bytes").toString();
  177.         DecimalFormat df=new DecimalFormat("0.00");
  178.         if(s<0x100000L)
  179.             return new StringBuilder(String.valueOf(df.format((float)s/1024F))).append("KB").toString();
  180.         else 
  181.             return new StringBuilder(String.valueOf(df.format((float)s/(1024*1024F)))).append("MB").toString();
  182.         
  183.     }
  184.     
  185.     private String getCurTime(long psec){
  186.         long sec=psec/1000L;
  187.         if(sec<60)
  188.             return new StringBuilder(String.valueOf(sec)).append("秒").toString();
  189.         else if(sec<60*60 && sec>=60)
  190.             return new StringBuilder(String.valueOf(sec/60)).append("分").append(String.valueOf(sec%60)).append("秒").toString();
  191.         else{
  192.             int hour=(int)sec/(60*60);
  193.             int temp=(int)sec%(60*60);
  194.             int min=temp/60;
  195.             int second=temp%60;
  196.             return new StringBuilder(String.valueOf(hour)).append("小时").append(String.valueOf(min)).append("分").append(String.valueOf(second)).append("秒").toString();
  197.         }
  198.     }
  199.     
  200.     
  201.     public void Curstop(){
  202.         stop();
  203.         if(uploadThread!=null)
  204.             uploadThread.interrupt();
  205.         if(updateThread!=null)
  206.             updateThread.interrupt();       
  207.         UpdateUI();
  208.         
  209.     }
  210.     
  211.     public void quitAll(){
  212.         
  213.         stop();
  214.         breakAll=true;
  215.     }
  216.     
  217.     
  218.     
  219.     public boolean isfinish(){
  220.         
  221.         return ftpcom.isfinish();
  222.     }
  223.     
  224.     void setProcessControl(Component d){
  225.         
  226.         control=d;
  227.         UpdateUI();
  228.     }
  229.     
  230.     void setProcessThread(Thread t){
  231.         this.processThread=t;   
  232.     }
  233.     
  234. }

  UploadProcessFrame.class

 

  1. package ftpupload;
  2. import java.awt.Dimension;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JProgressBar;
  13. /**
  14.  * 
  15.  * @author BruceXX
  16.  * 上传进度条窗口
  17.  */
  18. public class UploadProcessFrame extends JFrame {
  19.     
  20.     
  21.     
  22.      JPanel mainPanel=null,centerPanel=null;
  23.      JProgressBar processbar=null;
  24.      JLabel label1=null,label2=null,label3=null,label4=null,label5=null,label6=null,label7=null;
  25.     
  26.      JLabel done=null,totalBytes=null,doneBytes=null,avgSpeed=null,timePast=null,timeLeft=null,percent=null,curFilepath=null;
  27.      JButton cancel=null,pause=null,quit=null;
  28.      UploadProcessJob job;
  29.      Thread t=null;
  30.     
  31.     public UploadProcessFrame(){
  32.         
  33.         init();
  34.     }
  35.     
  36.     private void init(){
  37.         
  38.         setSize(600240);
  39.         setMinimumSize(new Dimension(600240));
  40. //        setPreferredSize(new Dimension(600, 240));
  41.         setTitle("上传进度条");
  42.         setDefaultCloseOperation(2);
  43.         setContentPane(getMainPanel());
  44.         
  45.     }
  46.     
  47.     private JPanel getMainPanel(){
  48.         
  49.         if(mainPanel==null){
  50.             
  51.             /**
  52.              * 主界面标签初始化
  53.              */
  54.             GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
  55.             gridBagConstraints14.gridx = 0;
  56.             gridBagConstraints14.gridwidth = 4;
  57.             gridBagConstraints14.weightx = 0.0D;
  58.             gridBagConstraints14.fill = 2;
  59.             gridBagConstraints14.gridy = 5;
  60.             GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
  61.             gridBagConstraints9.gridx = 1;
  62.             gridBagConstraints9.anchor = 16;
  63.             gridBagConstraints9.insets = new Insets(01000);
  64.             gridBagConstraints9.fill = 0;
  65.             gridBagConstraints9.weightx = 0.0D;
  66.             gridBagConstraints9.gridy = 0;
  67.             percent = new JLabel();
  68.             percent.setText("0%");
  69.             curFilepath=new JLabel();
  70.             
  71.              
  72.             GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
  73.             gridBagConstraints8.gridx = 1;
  74.             gridBagConstraints8.anchor = 17;
  75.             gridBagConstraints8.insets = new Insets(010520);
  76.             gridBagConstraints8.fill = 0;
  77.             gridBagConstraints8.weightx = 0.5D;
  78.             gridBagConstraints8.gridy = 4;
  79.             timeLeft = new JLabel();
  80.             timeLeft.setText("N/A");
  81.             timeLeft.setMinimumSize(new Dimension(9014));
  82.             timeLeft.setMaximumSize(new Dimension(9014));
  83.             timeLeft.setPreferredSize(new Dimension(7014));
  84.             
  85.             GridBagConstraints gridBagConstraints71 = new GridBagConstraints();
  86.             gridBagConstraints71.gridx = 0;
  87.             gridBagConstraints71.anchor = 13;
  88.             gridBagConstraints71.insets = new Insets(02050);
  89.             gridBagConstraints71.weightx = 0.0D;
  90.             gridBagConstraints71.gridy = 4;
  91.             label1 = new JLabel();
  92.             label1.setText("剩余时间:");
  93.             GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
  94.             gridBagConstraints61.gridx = 1;
  95.             gridBagConstraints61.anchor = 16;
  96.             gridBagConstraints61.insets = new Insets(010520);
  97.             gridBagConstraints61.fill = 0;
  98.             gridBagConstraints61.ipadx = 0;
  99.             gridBagConstraints61.weightx = 0.5D;
  100.             gridBagConstraints61.gridy = 3;
  101.             timePast = new JLabel();
  102.             timePast.setText("0 小时 0 分 0秒");
  103.             timePast.setMinimumSize(new Dimension(9014));
  104.             timePast.setMaximumSize(new Dimension(9014));
  105.             timePast.setPreferredSize(new Dimension(7014));
  106.             
  107.             
  108.             GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
  109.             gridBagConstraints5.gridx = 0;
  110.             gridBagConstraints5.anchor = 13;
  111.             gridBagConstraints5.insets = new Insets(02050);
  112.             gridBagConstraints5.weightx = 0.0D;
  113.             gridBagConstraints5.gridy = 3;
  114.             label2 = new JLabel();
  115.             label2.setText("已用时间:");
  116.             GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
  117.             gridBagConstraints31.gridx = 3;
  118.             gridBagConstraints31.insets = new Insets(010520);
  119.             gridBagConstraints31.anchor = 16;
  120.             gridBagConstraints31.weightx = 0.5D;
  121.             gridBagConstraints31.fill = 0;
  122.             gridBagConstraints31.gridy = 3;
  123.             avgSpeed = new JLabel();
  124.             avgSpeed.setText("0 B/秒");
  125.             avgSpeed.setMinimumSize(new Dimension(9014));
  126.             avgSpeed.setMaximumSize(new Dimension(9014));
  127.             avgSpeed.setPreferredSize(new Dimension(7014));
  128.             GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
  129.             gridBagConstraints12.gridx = 2;
  130.             gridBagConstraints12.insets = new Insets(0050);
  131.             gridBagConstraints12.anchor = 14;
  132.             gridBagConstraints12.weightx = 0.0D;
  133.             gridBagConstraints12.gridy = 3;
  134.             label3 = new JLabel();
  135.             label3.setText("上传平均速度:");
  136.             
  137.             
  138.             GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
  139.             gridBagConstraints7.gridx = 3;
  140.             gridBagConstraints7.insets = new Insets(010520);
  141.             gridBagConstraints7.anchor = 16;
  142.             gridBagConstraints7.weightx = 0.5D;
  143.             gridBagConstraints7.fill = 0;
  144.             gridBagConstraints7.gridy = 2;
  145.             doneBytes = new JLabel();
  146.             doneBytes.setText("0B");
  147.             doneBytes.setMinimumSize(new Dimension(9014));
  148.             doneBytes.setMaximumSize(new Dimension(9014));
  149.             doneBytes.setPreferredSize(new Dimension(7014));
  150.             GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
  151.             gridBagConstraints6.gridx = 2;
  152.             gridBagConstraints6.anchor = 14;
  153.             gridBagConstraints6.insets = new Insets(20050);
  154.             gridBagConstraints6.weightx = 0.0D;
  155.             gridBagConstraints6.gridy = 2;
  156.             label4 = new JLabel();
  157.             label4.setText("已上传大小:");
  158.             GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
  159.             gridBagConstraints3.gridx = 1;
  160.             gridBagConstraints3.anchor = 16;
  161.             gridBagConstraints3.insets = new Insets(2010520);
  162.             gridBagConstraints3.weightx = 0.5D;
  163.             gridBagConstraints3.fill = 0;
  164.             gridBagConstraints3.gridy = 2;
  165.             totalBytes = new JLabel();
  166.             totalBytes.setText("0B");
  167.             totalBytes.setMinimumSize(new Dimension(9014));
  168.             totalBytes.setMaximumSize(new Dimension(9014));
  169.             totalBytes.setPreferredSize(new Dimension(7014));
  170.             GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
  171.             gridBagConstraints2.gridx = 0;
  172.             gridBagConstraints2.insets = new Insets(202950);
  173.             gridBagConstraints2.anchor = 13;
  174.             gridBagConstraints2.weightx = 0.0D;
  175.             gridBagConstraints2.gridy = 2;
  176.             label5 = new JLabel();
  177.             label5.setText("总共大小:");
  178.             GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
  179.             gridBagConstraints1.gridx = 0;
  180.             gridBagConstraints1.anchor = 17;
  181.             gridBagConstraints1.insets = new Insets(202000);
  182.             gridBagConstraints1.gridwidth = 1;
  183.             gridBagConstraints1.gridy = 0;
  184.             label6 = new JLabel();
  185.             label6.setText("当前进度:");
  186.             GridBagConstraints gridBagConstraints = new GridBagConstraints();
  187.             gridBagConstraints.gridx = 0;
  188.             gridBagConstraints.weightx = 0.0D;
  189.             gridBagConstraints.fill = 2;
  190.             gridBagConstraints.insets = new Insets(520020);
  191.             gridBagConstraints.anchor = 11;
  192.             gridBagConstraints.weighty = 0.0D;
  193.             gridBagConstraints.gridwidth = 5;
  194.             gridBagConstraints.ipady = 0;
  195.             gridBagConstraints.gridy = 1;
  196.             label7=new JLabel();
  197.             label7.setText("正在等待服务器传送完毕....");
  198.             label7.setVisible(false);
  199.             mainPanel = new JPanel();
  200.             mainPanel.setLayout(new GridBagLayout());
  201.             mainPanel.add(getProcessBar(), gridBagConstraints);
  202.             mainPanel.add(label6, gridBagConstraints1);
  203.             mainPanel.add(label5, gridBagConstraints2);
  204.             mainPanel.add(totalBytes, gridBagConstraints3);
  205.             mainPanel.add(label4, gridBagConstraints6);
  206.             mainPanel.add(doneBytes, gridBagConstraints7);
  207.             mainPanel.add(label3, gridBagConstraints12);
  208.             mainPanel.add(avgSpeed, gridBagConstraints31);
  209.             mainPanel.add(label2, gridBagConstraints5);
  210.             mainPanel.add(timePast, gridBagConstraints61);
  211.             mainPanel.add(label1, gridBagConstraints71);
  212.             mainPanel.add(timeLeft, gridBagConstraints8);
  213.             mainPanel.add(percent, gridBagConstraints9);
  214.             mainPanel.add(curFilepath);
  215.             mainPanel.add(label7);
  216.             mainPanel.add(getCenterPanel(), gridBagConstraints14);
  217.         }
  218.         return mainPanel;
  219.     }
  220.     
  221.      JProgressBar getProcessBar(){
  222.         
  223.         if(processbar==null){
  224.             
  225.             processbar=new JProgressBar();
  226.             processbar.setPreferredSize((new Dimension(20020)));
  227.             processbar.setMaximum(10000);
  228.         }
  229.         return processbar;
  230.     }
  231.     
  232.     private JButton getCancelButton(){
  233.         
  234.         if(cancel==null){
  235.             cancel=new JButton();
  236.             cancel.setText("取消当前");
  237.             cancel.addActionListener(new ActionListener(){
  238.                 public void actionPerformed(ActionEvent e) {
  239.                     // TODO Auto-generated method stub
  240.                     job.breakCur=true;
  241.                     job.Curstop();
  242.                     label7.setVisible(true);
  243.                     cancel.setEnabled(false);
  244.                     quit.setEnabled(false);
  245.                     waitover();
  246.                 }
  247.             });
  248.         }
  249.         return cancel;
  250.     }
  251.     
  252.     
  253.     private void waitover(){         
  254.         t=new Thread(new Runnable(){
  255.             public void run() {
  256.                 // TODO Auto-generated method stub
  257.                 while(job.ftpcom.ishandling()){
  258.                     
  259.                     try {
  260.                         Thread.sleep(200);
  261.                     } catch (InterruptedException e) {
  262.                         // TODO Auto-generated catch block
  263.                         e.printStackTrace();
  264.                     }
  265.                 }
  266.                 label7.setVisible(false);               
  267.                 setVisible(false);
  268.                 t.interrupt();
  269.             }           
  270.         }); 
  271.         t.start();
  272.         
  273.     }
  274.     
  275.     
  276.     private JButton getQuitButton(){
  277.         
  278.         if(quit==null){
  279.             quit=new JButton();
  280.             quit.setText("取消全部");
  281.             quit.addActionListener(new ActionListener(){
  282.                 public void actionPerformed(ActionEvent e) {
  283.                     // TODO Auto-generated method stub
  284.                     
  285.                     job.quitAll();
  286.                     label7.setVisible(true);
  287.                     cancel.setEnabled(false);
  288.                     quit.setEnabled(false);
  289.                     waitover();
  290.                 }
  291.             });
  292.         }
  293.         return quit;
  294.         
  295.     }
  296.     
  297.     
  298.     
  299.     private JPanel getCenterPanel(){
  300.         if(centerPanel==null){
  301.             
  302.              GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
  303.                 gridBagConstraints10.fill = 0;
  304.                 gridBagConstraints10.gridwidth = 1;
  305.                 gridBagConstraints10.gridx = -1;
  306.                 gridBagConstraints10.gridy = -1;
  307.                 gridBagConstraints10.weightx = 0.5D;
  308.                 gridBagConstraints10.insets = new Insets(100100);
  309.                 GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
  310.                 gridBagConstraints4.insets = new Insets(100100);
  311.                 gridBagConstraints4.gridx = -1;
  312.                 gridBagConstraints4.gridy = -1;
  313.                 gridBagConstraints4.weightx = 0.5D;
  314.                 gridBagConstraints4.gridwidth = 1;
  315.                 centerPanel = new JPanel();
  316.                 centerPanel.setLayout(new GridBagLayout());
  317.                 centerPanel.add(getCancelButton(), gridBagConstraints4);
  318.                 centerPanel.add(getQuitButton(),gridBagConstraints10);
  319. //              centerPanel.add(getPauseButton(), gridBagConstraints10);
  320.         }
  321.         return centerPanel;
  322.     }
  323.     
  324.     final class ServerWaitingFrame extends JFrame{
  325.         
  326.         JPanel panel;
  327.         JLabel jlabel;
  328.         
  329.         public ServerWaitingFrame(){
  330.             
  331.              setSize(20070);
  332.              setContentPane(getSelfPanel());
  333.         }
  334.         
  335.         private JPanel getSelfPanel(){
  336.             if(panel==null){
  337.                 panel=new JPanel();
  338.                 jlabel=new JLabel();
  339.                 jlabel.setText("正在等待服务器传送完毕...");
  340.                 
  341.             }
  342.             return panel;
  343.         }
  344.     
  345.     }
  346.     
  347. }

还有一个就是相关的配置文件,upload.properties


ftpuser=BruceXX
ftppass=183320433
ftpport=21
hostName=localhost
remoteFolder=lovetq/

 

运行MainApp

界面如下: