java文件传输

200792101.jpg
200792102.jpg
200792103.jpg

文件接收部分

FileReceiver.java

package com.vista.Util;

import java.io.IOException;
import java.net.ServerSocket;

public class FileReceiver  extends Thread
{
    
private ServerSocket s = null;    
    
private int FILE_PORT ;//文件传输的监听端口
    private User user = null;
    
public  FileReceiver(User us)
    {
        
this.FILE_PORT = us.getUdpPort();//设置文件端口
        this.user=us;
    }
    
public void run()
    {
        
try
        {
            s 
= new ServerSocket(FILE_PORT);
            System.
out.println("用户端开始监听:"+s);
            
while(true)
                {
                java.net.Socket  socket 
= s.accept();
                System.
out.println("连接接受"+socket);
                
new FileReceiverThread(user,socket);
            }
        }
        
catch(IOException e)
        {
            e.printStackTrace();
        }
        
catch(java.lang.Exception ex)
        {
            System.err.println(ex.getMessage().toString());
            ex.printStackTrace();
        }
        
finally
        {
            
try 
            {
                s.close();
            } 
            
catch (IOException e) 
            {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }    
    }

}

FileReceiverThread.java
package com.vista.Util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

import com.vista.Form.SingleChatForm;

public class FileReceiverThread extends Thread
{
    
private SingleChatForm chatForm = null;
    
private java.net.Socket socket = null;
    
private java.io.InputStream in = null;//从客户端读数据的
    private java.io.PrintWriter out = null;//向客户端写数据
    private String serverMsg;
    
private User sender =null;
    
private User receiver =null;
    
private com.vista.Util.Sender MsgSender = null;
    

    
public FileReceiverThread (User us,Socket socket)
    {
        
this.receiver=us;
            
try 
            {
                
this.socket = socket;
            
this.in = this.socket.getInputStream();
                
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())),true);
            } 
            
catch (IOException e) 
            {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
            
this.start();//启动线程
    }

    
public void run()
    {
         String info  
= "";
        
try
        {      
            
while(true)   
            {   
                
//第一个参数为命令   
                Date befor = new Date();
                
     
                
byte cmd[] = new byte[128];   
                
int b = 0;   
                
while(b<cmd.length)
                { 
                    b 
+= in.read(cmd, b, cmd.length-b);   
                }   
                
int ends = 0;   
                
for(int i=0;i<cmd.length;i++)
                {
                    
if(cmd[i]==-0)
                    {   
                        ends 
= i;   
                        
break;   
                    }   
                }   
                String cmds 
= new String(cmd,0,ends);   
                
if("cp".equals(cmds))
                {   
                    
byte[] filename = new byte[256];   
                    b 
= 0;   
                    
while(b<cmd.length)
                    { 
                        b 
+= in.read(filename, b, filename.length-b);   
                    }   
                    ends 
= 0;   
                    
for(int i=0;i<cmd.length;i++)
                    { 
                        
if(filename[i]==-0)
                        {   
                            ends 
= i;   
                            
break;   
                        }   
                    }   
                    String filenames 
= new String(filename,0,ends);
                    filenames 
= "F://"+filenames;
                    
                    File fileout 
= new File(filenames);   

                    
                    FileOutputStream fos 
= new FileOutputStream(fileout);   
                       
                    
byte[] filesize = new byte[64];   
                    b 
= 0;   
                    
while(b<filesize.length)
                    { 
                        b 
+= in.read(filesize, b, filesize.length-b);   
                    }   
                       
                    ends 
= 0;   
                    
for(int i=0;i<filesize.length;i++)
                    {
                        
if(filesize[i]==-0)
                        {   
                            ends 
= i;   
                            
break;   
                        }   
                    }   
                    String filesizes 
= new String(filesize,0,ends);   
                       
                    System.
out.println("filesize:"+filesizes);   
                    
int ta = Integer.parseInt(filesizes);   
                    
byte[] buf = new byte[1024*10];   
 
                    while(true)
                    {   
                        
if(ta==0)
                        {   
                            
break;   
                        }   
                        
int len = ta;   
                        
if(len>buf.length)
                        {   
                            len 
= buf.length;   
                        }   
                        
int rlen = in.read(buf, 0, len);   

                        ta -= rlen;   
                        
if(rlen>0)
                        {   
                            fos.write(buf,
0,rlen);   
                            fos.flush();   
                        }   
                        
else
                        {   
                            
break;   
                        }   
                    }   
                    Date after 
= new Date();
                    
long diff =after.getTime() - befor.getTime();
                    diff 
/=1000;

                   info = "传输完成!文件已经保存到:"+filenames+",文件大小:"+filesizes+"字节,"+"共用时间:"+diff+"";
                   
int result = javax.swing.JOptionPane.showConfirmDialog(null, info, "文件传输",  javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.INFORMATION_MESSAGE);
                
                   fos.close(); 
                   
break;   
                }   
            }   
            
//socket.close();   
            }
        
catch(Exception e)
        {
            e.printStackTrace();   
        }   
          
try 
            {
                
this.socket.close();
            } 
            
catch (IOException ex1)
            {
            }
             
    }
}

文件发送线程
FileSender.java
package com.vista.Util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class FileSender extends Thread
{
    
private Socket socket = null;//客户端socket
    private OutputStream out = null;//向服务器写数据
    private int RECEIVER_PORT = -1;//接收者端口
   

    private File fileToSend = null;//要传输的文件
    
    
public FileSender(File file,int Receiver_Port)
    {
        
this.fileToSend = file;
        
this.RECEIVER_PORT = Receiver_Port;//设置接收方端口
        try 
        {
            
this.socket = new Socket(InetAddress.getLocalHost(),RECEIVER_PORT);
            
out = socket.getOutputStream();//向接收者发数据的
        } 
        
catch (UnknownHostException e) 
        {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
catch (IOException e) 
        {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    
public void run()
    {
        
byte[] cmd = new byte[128];
        
byte[] tcmd = "cp".getBytes();
        
for(int i=0;i<tcmd.length;i++)
        {
            cmd[i] 
= tcmd[i];   
        }   
        cmd[tcmd.length] 
= -0;   
        
try 
        {
            
out.write(cmd,0,cmd.length);
        } 
        
catch (IOException e)
        {
            e.printStackTrace();
        }   
         
//文件名   
        byte[] file = new byte[256];   
        
byte[] tfile = this.fileToSend.getName().getBytes();   
        
for(int i=0;i<tfile.length;i++)
        {
            file[i] 
= tfile[i];   
        }   
        file[tfile.length] 
= -0;   
        
try 
        {
            
out.write(file,0,file.length);
        } 
        
catch (IOException e) 
        {
            e.printStackTrace();
        }          
         
//大小     
        byte[] size = new byte[64];   
        
byte[] tsize = (""+this.fileToSend.length()).getBytes();   
           
        
for(int i=0;i<tsize.length;i++)
        { 
            size[i] 
= tsize[i];   
        }   
           
        size[tsize.length] 
= -0;   
        
try 
        {
            
out.write(size,0,size.length);
        } 
        
catch (IOException e) 
        {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }   
           
        FileInputStream fis 
= null;   
        
byte[] buf = new byte[1024*10];    
        
try 
        {
            fis 
= new FileInputStream(this.fileToSend);
        } 
        
catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }   
        
int readsize = 0;    
         
try 
         {
            
while((readsize = fis.read(buf, 0, buf.length))>0)
            {   
                
out.write(buf,0,readsize);   
                
out.flush();  
            }
        } 
        
catch (IOException e)
        {
            e.printStackTrace();
        }   

    }

}

posted on 2007-09-21 23:26  Phinecos(洞庭散人)  阅读(11583)  评论(31编辑  收藏  举报

导航