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

📄 client.java

📁 飞鸽传书的源代码 快速的局域网传输软件
💻 JAVA
字号:
/*
 * Client.java
 *
 * Created on 2007年7月1日, 下午4:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package trans;

/**
 *
 * @author 鹏飞
 */
import java.awt.GridLayout;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Client implements  Runnable
{
       private Vector Vname=new Vector();
       private InetAddress address;
       private Vector sendDir = new Vector();
       private Vector fileVector = new Vector();
       private long totalSize;
       private long fileSize;
       private String fileName;
       private long receivedSize;
       private long skipSize;
   
        Client( Vector file , InetAddress address)
        {
            fileVector.addAll ( file ) ; 
            this.address = address;
        }
        public  void getFiles(File file,String lastDir ,String localPath)
        {
            if(file.isFile())
            {
                totalSize += file.length();
                sendDir.add (localPath);
		Vname.add(new String(lastDir + "/"+file.getName()));
            }
            else if(file.isDirectory())
            {
		lastDir = lastDir + "/" + file.getName();
		String strs[] = file.list();
		for(int i = 0 ;i < strs.length; i ++)
		{
			File newfile = new File( file.getAbsolutePath() + "/" + strs[i] );
			getFiles( newfile , lastDir , localPath );
		}
            }
        }
	public void run()
	{    
               totalSize = 0;
               String send = "";
               
               try
		{
                   byte[] buffer = new byte[1024];
                   Socket sendSocket = new Socket(address, MainFrame.PORT );
                   DataInputStream  in = new  DataInputStream(sendSocket.getInputStream());
                   BufferedOutputStream  outb = new BufferedOutputStream(sendSocket.getOutputStream(),512);
                   DataOutputStream  out = new  DataOutputStream(outb);
                   
                   ObjectOutputStream oout = new ObjectOutputStream (out);
                   oout.writeObject ( fileVector );  
                   oout.flush ();
                   ObjectInputStream oin = new ObjectInputStream ( in );               
                   Vector accVector = (Vector) oin.readObject ();
                   if ( MainFrame.m.accRe )
                   JOptionPane.showMessageDialog(null, "消息已发送到!","提示", JOptionPane.DEFAULT_OPTION);  
                   for (int i = 0 ; i < accVector .size (); i ++)
                   {  
                       File temp = (File) accVector.get ( i );
                       send += "   " + temp.getName ();
                       getFiles( temp ,"",temp.getParent ());
                   }
                   //receive file
                   out.writeLong(totalSize);
                   out.flush();
                   int rSize;
                    

                File tempFile;
                String tempString;
                long count ;
                JFrame frame = new JFrame();
                frame.setTitle("正在发送文件");
                JLabel lable1 = new JLabel();
                JLabel lable2 = new JLabel();
                JLabel lable3 = new JLabel();
                frame.add( lable1,"North" );
                frame.add( lable3,"South" );
                receivedSize = 0;
                JProgressBar bar = new JProgressBar ( 0 , (int)(totalSize/1024));
                JProgressBar bar1 = new JProgressBar ();
                bar.setForeground(new java.awt.Color(102, 255, 102));
                bar.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)), "\u603b\u8fdb\u5ea6", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 13), new java.awt.Color(0, 0, 255)));
                bar.setDoubleBuffered(true);
                bar.setStringPainted ( true ) ;
                bar1.setSize (320,20);
                bar.setSize (320,20);
                bar1.setForeground(new java.awt.Color(102, 255, 102));
                bar1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)), "当前进度", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 13), new java.awt.Color(0, 0, 255)));
                bar1.setDoubleBuffered(true);
                bar1.setStringPainted ( true );
                JPanel jp = new JPanel();
                jp.setLayout ( new GridLayout( 3 , 1 ));
//                            jp.add ( new JLabel ("总进度:    "));
                jp.add ( bar );
//                            jp.add ( new JLabel( "当前进度:  " ));
                jp.add ( bar1 );
                jp.add ( lable2 );
                frame.add ( jp );

                String  Lmessage1 = "总共要发送:  "+String.valueOf( totalSize / 1024) + "  KB";
                String  Lmessage2 = "速度: 0";
                lable1.setText( Lmessage1 );
                lable2.setText( Lmessage2 );
                lable3.setText ( "共" + Vname.size () + "个文件,已完成" + 0 + "个");
                RandomAccessFile inFile;
                frame.setBounds( 250 , 200 ,  400 , 200);
                frame.setResizable ( false );
                frame.setVisible(true);
                long baseTime = new Date().getTime();
                for(int i = 0;i < Vname.size();i++)
                {   

                    count = 0;
                    lable3.setText ( "共" + Vname.size () + "个文件,已完成" + i + "个");
                    bar1.setMinimum ( 0 );
                    tempString = ( String ) Vname.get(i);
                    tempFile = new File( sendDir.get(i) + tempString);
                    fileSize = tempFile.length();
                    jp.setToolTipText ( "正发送:" + tempString.substring ( 1 ) );
                    out.writeLong(fileSize); 
                    int locate = new Random().nextInt ( 1000 );
                    byte [] fileRandom = new byte [10];
                    if ( tempFile.length () < 1024 )
                        locate = -1 ;
                    else
                    fileRandom = getFile( locate,tempFile );
                    out.writeInt ( locate );
                    out.write ( fileRandom );
                    frame.setTitle ( "正发送文件:" + tempString.substring ( 1 ) );
                    bar1.setMaximum ( ( int )fileSize / 1024 );
                    bar1.setValue ( 0 );
                    byte data2[] = tempString.getBytes( "UTF-8" );
                    out.write(data2);
                    out.flush();

                    skipSize = in.readLong();
                    rSize = in.read(buffer);
                    String reBack = new String(buffer,0,rSize,"UTF-8");
                    if("S".equals(reBack))
                    {
                        receivedSize += fileSize;
                        count = fileSize;
                        bar.setValue ( (int) receivedSize / 1024);
                        bar1.setValue ((int) count / 1024);
                        continue;
                    }
                    else
                    {
                        if("C".equals(reBack))
                        {
                            receivedSize += skipSize;
                            count = skipSize;
                            bar.setValue ( ( int )receivedSize / 1024 );
                            bar1.setValue ( ( int )count /1024 );
                            inFile = new RandomAccessFile(tempFile, "r");
                            inFile.seek( skipSize );

                        }
                        else
                        {
                            inFile=new RandomAccessFile(tempFile, "r");
                        }

                        while((rSize = inFile.read(buffer)) != -1)
                        {
                            receivedSize += rSize;
                            count += rSize;
                            bar.setValue ( ( int )receivedSize / 1024 );
                            bar1.setValue ( ( int )count / 1024 );
                            lable2.setText("速度: "+
                                    String.valueOf(receivedSize * 1000 / (new Date().getTime() - baseTime + 1) / 1024 )+"  KB/s");
                            outb.write( buffer, 0 ,rSize );
                        }
                        outb.flush();
                        inFile.close();
                    }
                }
                fileSize = -1;
                out.writeLong(fileSize);
                out.writeInt ( -1 );
                out.write( new byte [10]);
                byte data3[] = "#发送完毕#".getBytes("UTF-8");
                out.write(data3);
                out.flush();
                frame.setVisible(false);
                JOptionPane.showMessageDialog(null, "发送完成!","提示", JOptionPane.DEFAULT_OPTION);    
                                                                                  
                   in.close();
                   out.close();
                   sendSocket.close();
		}
		catch(Exception e) 
		{
			JOptionPane.showMessageDialog(null, "发送文件出错l:"+e,"错误", JOptionPane.ERROR_MESSAGE);
		} 
	}
        
     public static  byte [] getFile( int n , File file)
     { 
          byte [] b = new byte [10];
         try
         {
            
             RandomAccessFile rf = new RandomAccessFile(file,"r");
             rf.seek ( n );
             rf.read ( b, 0 ,b.length);
             rf.close ();
         }
         catch (Exception e)
         {
             
         }
     return b;
     }
}

⌨️ 快捷键说明

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