Java -- optimize the use of TCP to achieve file upload (multithreading)

Keywords: Programming socket Java

Java -- optimize the use of TCP to achieve file upload (multithreading)

Blog description

The information involved in this article comes from Internet collation and personal summary, which means personal learning and experience summary. If there is any infringement, please contact me to delete, thank you!

graphic

step

  1. [Client] input stream, read the file data from the hard disk to the program.
  2. [Client] output stream, write out the file data to the server.
  3. [server] input stream, read the file data to the server program.
  4. [server] output stream, write out file data to server hard disk

optimization

  1. The problem of dead file name

    On the server side, if the name of the saved file is written dead, only one file will be kept on the hard disk of the server. It is recommended to use system time optimization to ensure that the file name is unique

  2. Problems with cyclic reception

    The server can continuously receive files from different users through cyclic improvement

  3. Efficiency issues

    When receiving large files, the server may take a few seconds. At this time, it cannot receive other users' uploads. Therefore, multithreading technology is used for optimization

code implementation

The server
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author ServerTCP
 * @date 2020/4/25 10:51 morning
 */
public class ServerTCP {
    public static void main(String[] args) throws IOException {
        System.out.println("Service started, waiting for connection");
        //Create ServerSocket object, bind port, start waiting for connection
        ServerSocket ss = new ServerSocket(8888);
        //Cyclic reception
        while (true) {
            //accept method, return socket object
            Socket server = ss.accept();

            //Turn on Multithreading
            new Thread(() -> {
                try (
                        //Get input stream object
                        BufferedInputStream bis = new BufferedInputStream(server.getInputStream());
                        //Create an output stream object and save it locally
                        FileOutputStream fis = new FileOutputStream(System.currentTimeMillis() + ".jpg");
                        BufferedOutputStream bos = new BufferedOutputStream(fis);
                ) {
                    //Read and write data
                    byte[] b = new byte[1024 * 8];
                    int len;
                    while ((len = bis.read(b)) != -1) {
                        bos.write(b, 0, len);
                    }
                    //Close resources
                    bos.close();
                    bis.close();
                    server.close();
                    System.out.println("File upload saved");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

client
import java.io.*;
import java.net.Socket;

/**
 * @author ClientTCP
 * @date 2020/4/25 10:58 morning
 */
public class ClientTCP {
    public static void main(String[] args) throws IOException {
        //Create input stream
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt"));
        //Create Socket
        Socket client = new Socket("127.0.0.1", 8888);
        //Output stream
        BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
        //Write data
        byte[] b = new byte[1024 * 8];
        int len;
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
            bos.flush();
        }
        System.out.println("File uploaded");

        //close resource
        bos.close();
        client.close();
        bis.close();
        System.out.println("File upload completed");
    }
}

Thank

Black horse programmer

And the industrious self

Posted by pastijalan on Sat, 25 Apr 2020 07:39:42 -0700