博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java文件传输
阅读量:6988 次
发布时间:2019-06-27

本文共 5892 字,大约阅读时间需要 19 分钟。

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();
        }   
    }
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/09/21/902016.html,如需转载请自行联系原作者
你可能感兴趣的文章
第十二章 简单工厂模式(Simple Facotry)
查看>>
LeetCode - 70. 爬楼梯
查看>>
visualVm监控tomcat
查看>>
Jquery script for document preview?
查看>>
【Magedu】Week02
查看>>
写给MongoDB开发者的50条建议Tip12
查看>>
我的友情链接
查看>>
linux下查看nginx,apache,mysql,php编译命令
查看>>
JQUERY学习第三天之浮动和弹出窗口
查看>>
python中asynchat异步socket命令/响应处理
查看>>
动态编译
查看>>
linux下批量解压缩
查看>>
使用xcopy进行日增量备份
查看>>
知之者不如好之者,好之者不如乐之者
查看>>
测试Application.Idle
查看>>
sizeof与strlen的区别与联系
查看>>
Citrix发布支持Framehawk技术的HDX协议,用户体验优势进一步扩大
查看>>
Android各种访问权限Permission详解
查看>>
RHEL5.5安装中文支持
查看>>
web前端开发中浏览器兼容问题(五)
查看>>