📄 getfile.java
字号:
package filetran;import java.io.*;import java.net.*;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 getfile { Socket sc=null; int sport; String addstr; InputStream in; FileOutputStream fout; String filepath; FileTranFram ftf; JLabel status; JLabel status2; JTable jtb; private int rowNum; private byte buf[]=new byte[1024]; private float fileda; private int data; private float sps; private float sps2; private float speed,fv,rtime; private long flen; DefaultTableModel dtm=null; public getfile(String add,int port,String filep,FileTranFram f,int row,long fl) { this.addstr=add; this.sport=port; this.filepath=filep; this.ftf=f; status=ftf.statusBar; fileda=0; sps=0; status2=ftf.status; jtb=ftf.jtb; //this.rowNum=row; dtm=ftf.dtm; this.flen=fl; } public float getspeed(){ return speed; } public void get(){ try{ rowNum=dtm.getRowCount(); dtm.addRow(new Object[5]); sc=new Socket(InetAddress.getByName(addstr),sport); status.setText("已与发送方连接..."); in=sc.getInputStream(); jtb.setValueAt(new String("接收 |"+Math.round(flen/1024)+"MB"),rowNum,1); jtb.setValueAt(new String(filepath),rowNum,2); jtb.setValueAt(new String(addstr),rowNum,0); fout=new FileOutputStream(filepath); status.setText("正在接收数据..."); Calendar start1=new GregorianCalendar(); float shour1=start1.get(Calendar.HOUR_OF_DAY); float smin1=start1.get(Calendar.MINUTE); float ssec1=start1.get(Calendar.SECOND); float sn1=start1.get(Calendar.MILLISECOND); // JOptionPane.showMessageDialog(ftf,""+flen); while((data=in.read(buf))!=-1){ // try{ // Thread.sleep(1); // }catch(InterruptedException e){} Calendar end1=new GregorianCalendar(); float ehour1=end1.get(Calendar.HOUR_OF_DAY); float emin1=end1.get(Calendar.MINUTE); float esec1=end1.get(Calendar.SECOND); float en1=end1.get(Calendar.MILLISECOND); float spss1=(ehour1-shour1)*3600+(emin1-smin1)*60+(esec1-ssec1)+(en1-sn1)/1000; sps=spss1-sps2; Calendar start=new GregorianCalendar(); float shour=start.get(Calendar.HOUR_OF_DAY); float smin=start.get(Calendar.MINUTE); float ssec=start.get(Calendar.SECOND); float sn=start.get(Calendar.MILLISECOND); //////////////////////// fout.write(buf,0,data); ////////////// Calendar end=new GregorianCalendar(); float ehour=end.get(Calendar.HOUR_OF_DAY); float emin=end.get(Calendar.MINUTE); float esec=end.get(Calendar.SECOND); float en=end.get(Calendar.MILLISECOND); float spss2=(ehour-shour)*3600+(emin-smin)*60+(esec-ssec)+(en-sn)/1000; sps2+=spss2; fileda+=1; // if(sps>0){ speed=fileda/sps; float rsize=flen-fileda; rtime=rsize/speed; // } // float rtime=rsize/speed; fv=(fileda/flen)*100; int fx=Math.round(fv); 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("正在接收"+filepath+"..."); status.setText("正在接收"+filepath+"..."); // status2.setText("已接收 "+fileda+"KB,速度:" // +Math.round(speed)+"KB/秒,估计剩余:"); } fout.close(); in.close(); sc.close(); status.setText("文件"+filepath+"接收完毕..."); // status2.setText("...共:"+fileda+"KB, 用时:"+Math.round(sps)+ // "秒, 速度:"+Math.round(fileda/sps)+"KB/秒"); //JOptionPane.showMessageDialog(ftf,String.valueOf(ssec)+" "+String.valueOf(esec)); }catch(IOException e){ JOptionPane.showMessageDialog(ftf,"getfile err"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -