java socket 实现多个客户端向服务器上传文件

服务器端:

package cn.com.test09;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class t10 {

    public static void main(String[] args) {
          new ServerO("F:\",8080);
    }
}

class ServerO {
    private String path;
    private int port;
    private ServerSocket ss;

    public ServerO(String s, int p) {
        this.path = s;
        this.port = p;
        try {
            ss = new ServerSocket(port);
            while (true) {
                Socket soc = ss.accept();
                new Thread(new OOOO(soc,path)).start();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            try {
                ss.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

}

class OOOO implements Runnable {
    private Socket soc;
    private String path;
    public OOOO(Socket soc,String pa) {
        this.soc = soc;
        this.path=pa;
    }
    public void setFile(String name,byte[] b,int i){
        try {
            FileOutputStream bos = new FileOutputStream(new File(path+name),true);
            bos.write(b,0,i);
            bos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("保存文件出错");
        }
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        DataInputStream in = null; 
        String name;
        byte[] b= new byte[1024];
        int i;
             try {
                in = new DataInputStream(soc.getInputStream());
                name=in.readUTF();
                while((i=in.read(b))!=-1){
                    setFile(name, b ,i);
                }
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("run中出现异常");
                e.printStackTrace();
            }finally{
                try {
                    in.close();
                    soc.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
    }

}

客户端:

package cn.com.test09;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class t11 {

    public static void main(String[] args) {
        Socket s = null;
        DataOutputStream data = null;
        InputStream is = null;
      try {
        s= new Socket("192.168.0.104", 8080);
        data = new DataOutputStream(s.getOutputStream());
        File f= new File("F:\58bb.zip");
        is= new FileInputStream(f);
        byte[] b= new byte[(int) f.length()];
        is.read(b);
        data.writeUTF(f.getName());
        data.flush();
        data.write(b);
        data.flush();
        
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        
        try {
            is.close();
            data.close();
            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    }

}
原文地址:https://www.cnblogs.com/anholt/p/3660053.html