Java——网络编程案例——文件的上传解析

第一次学习Java网络编程,在服务器端和客户端之间关系的理解上花费了一段时间,近期学习了一个小的案例,文件的上传,特此写一下自己的理解。首先附上代码

服务器端代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {
    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket=new ServerSocket(10001);
        while(true){
            //调用accept()方法接收客户端请求,得到Socket对象
            Socket s=serverSocket.accept();
            //每当和客户端建立Socket连接后,单独开启一个线程处理和客户端的交互
            new Thread(new ServerThread(s)).start();
        }

    }
}
class ServerThread implements Runnable{
    private Socket socket;
    public ServerThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        String ip=socket.getInetAddress().getHostAddress();//获取客户端的IP地址
        int count=1;//上传图片个数
        try {
            InputStream is=socket.getInputStream();
            File parentFile=new File("C:\\Users\\Ding\\Desktop\\upload\\");//创建上传图片目录的File对象
            if(!parentFile.exists()){//如果不存在,就创建这个目录
                parentFile.mkdir();
            }
            //把客户端的IP地址作为上传文件的文件名
            File file=new File(parentFile,ip+"("+count+").jpg");
            while (file.exists()){
                //如果文件存在,则把count++
                file=new File(parentFile,ip+"("+(count++)+").jpg");
            }
            //创建FileOutputStream对象
            FileOutputStream fos=new FileOutputStream(file);
            byte[] buf=new byte[1024];//定义一个字节数组
            int len=0;//定义一个int类型的变量len,初始值为0
            while((len=is.read(buf))!=-1){//循环读取数据
                fos.write(buf,0,len);
            }
            //回应客户端
            OutputStream out=socket.getOutputStream();//获取服务端的输出流
            out.write("上传成功".getBytes());
            fos.close();
            socket.close();
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }
}

客户端代码:

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class FileClient {
    public static void main(String[] args) throws Exception{
        Socket socket=new Socket("127.0.0.1",10001);//创建客户端Socket
        OutputStream out=socket.getOutputStream();//获取Socket的输出流对象
        //创建FileInputStream对象
        FileInputStream fis=new FileInputStream("C:\\Users\\DingLeXiao\\Desktop\\psb.jpg");
        byte[] buf=new byte[1024];
        int len;
        while ((len=fis.read(buf))!=-1){//循环读取数据
            out.write(buf,0,len);
        }
        socket.shutdownOutput();//关闭客户端输入流
        InputStream in=socket.getInputStream();//获取Socket的输入流对象
        byte[] bufMsg=new byte[1024];//定义一个字节数组
        int num=in.read(bufMsg);
        String Msg=new String(bufMsg,0,num);
        System.out.println(Msg);
        fis.close();
        socket.close();

    }
}

理解:首先启动服务器,当调用accept()方法的时候,发生阻塞,等待客户端的接入,在客户端中创建了一个客户端Socket,接着获取到输出流对象,在这个输出流中,客户端主要做的任务就是将在主机中读取到的要上传的数据写入进去。然后在服务器端,获取到了客户端的输入流,把流中的数据读取出来写入事先创建好的File对象,这样就在服务器中实现了文件的上传,然后服务器会给客户端一个回应,也就是上传成功。

你可能感兴趣的:(Java)