⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientth.java

📁 java网络文件传输源代码
💻 JAVA
字号:
package filetran;import java.net.*;import java.io.*;import javax.swing.*;import java.util.*;import javax.swing.table.*;/** * <p>Title: FileTran</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author Netf * @version 1.0 */public class clientth extends Thread{  Socket client=null;  OutputStream out=null;  FileInputStream fis=null;  String filestr=null;  FileTranFram frame;  JTextField jtf;  JLabel status;  private JLabel status2;  JTable jtb;  JProgressBar jpb;  DefaultTableModel dtm=null;  Vector v;  private float fileda,sps,speed,filesize;  private int data;  private byte buf[]=new byte[1024];  private static int ConNum;  private int rowNum=0;  private String cadd=null;  private float shour,smin,ssec,sn,ehour,emin,esec,en,spss,rsize,rtime,fv;  public clientth(Socket s,String filestr,FileTranFram f,int cn) {    this.frame=f;    status=frame.statusBar;    fileda=0;    this.client=s;    jtf=frame.files;    jpb=frame.jpb;    jtb=frame.jtb;    status2=frame.status;    cadd=frame.Ipstr.getText();    this.filestr=jtf.getText().trim();   // ConNum++; //   this.rowNum=cn;    dtm=frame.dtm;    v=frame.v;      rowNum=dtm.getRowCount();  //  sdata sd=new sdata(filestr,cadd,"发送",String.valueOf(filesize),"jd",String.valueOf(speed),     //                  "fin",String.valueOf(fileda));   // v.addElement(sd);  }  public void run(){    try{      status.setText("已连接...");      out=client.getOutputStream();    }catch(IOException e){}    try{       dtm.addRow(new Object[5]);       //rowNum=ConNum-1;      // JOptionPane.showMessageDialog(frame,jtf.getText().trim());       fis=new FileInputStream(filestr);      filesize=fis.available()/1024;     //JOptionPane.showMessageDialog(frame,filesize+"");      // jtb.setValueAt(new Integer(rowNum),rowNum,0);       jtb.setValueAt(new String("发送 |"+Math.round(filesize/1024)+"MB"),rowNum,1);       jtb.setValueAt(new String(filestr),rowNum,2);       jtb.setValueAt(new String(cadd),rowNum,0);       status.setText("开始发送...");       jpb.setVisible(true);        while((data=fis.read(buf))!=-1){          ////////starttime/////////////          Calendar start=new GregorianCalendar();        shour=start.get(Calendar.HOUR_OF_DAY);        smin=start.get(Calendar.MINUTE);        ssec=start.get(Calendar.SECOND);        sn=start.get(Calendar.MILLISECOND);       // Date stime=new Date();    //  float sdate=stime.getTime();         ////////////////////          out.write(buf,0,data);          ////////////endtime///////////////          Calendar end=new GregorianCalendar();         ehour=end.get(Calendar.HOUR_OF_DAY);         emin=end.get(Calendar.MINUTE);         esec=end.get(Calendar.SECOND);         en=end.get(Calendar.MILLISECOND);      //   Date etime=new Date();       //  float edate=etime.getTime();        spss=(ehour-shour)*3600+(emin-smin)*60+(esec-ssec)+(en-sn)/1000;        // spss=(edate-sdate)/1000;         sps+=spss;          fileda+=1;          rsize=filesize-fileda;       //   if(sps>0){              speed=fileda/sps;          rtime=rsize/speed;      //    }         fv=(fileda/filesize)*100;          int fx=Math.round(fv);          jpb.setValue(fx);          String st=new String(fx+"%估计剩余:"+Math.round(rtime)+"秒");          jtb.setValueAt(st,rowNum,4);          String sx=new String("已发送:"+fileda+"KB,  "                        +Math.round(speed)+"KB/秒,用时:"+sps+"秒");          jtb.setValueAt(sx,rowNum,3);          status.setText("正在发送"+filestr+"...");         // jpb.setString(fx+"%");          //upstatus us=new upstatus(rowNum,fileda,filesize,sps,frame);          //us.start();        // status2.setText("正在发送文件...已发送:"+fileda+"KB,  "+fx+"%, 速度"            //            +Math.round(speed)+"KB/秒,估计剩余:"+Math.round(rtime)+"秒");        }        out.close();        fis.close();        client.close();        status.setText("文件"+filestr+"发送完毕...");      // JOptionPane.showMessageDialog(frame,sn+"/"+en+"");     //  status2.setText("...共:"+fileda+"kb,用时:"+Math.round(sps)        //              +"秒, 速度:"+Math.round(speed)+"KB/秒");     }catch(IOException e){        JOptionPane.showMessageDialog(frame,"clientth err");       }  }}class upstatus extends Thread{  static int row;  private float fileda;  private float filesize;  float sps;  float speed;  FileTranFram ftf2;  //float spss;  JTable jtb2;  public upstatus(int row,float fileda,float filesize,float sps,FileTranFram ftf){    this.row=row;    this.fileda=fileda;    this.sps=sps;    this.ftf2=ftf;    jtb2=ftf.jtb;    this.filesize=filesize;  }  public void run(){        speed=fileda/sps;       float fv=(fileda/filesize)*100;       int fx=Math.round(fv);         String st=new String(fx+"%");         jtb2.setValueAt(st,row,4);         String sx=new String("已发送:"+fileda+"KB,  "                       +Math.round(speed)+"KB/秒"+filesize);         jtb2.setValueAt(sx,row,3);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -