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
- [Client] input stream, read the file data from the hard disk to the program.
- [Client] output stream, write out the file data to the server.
- [server] input stream, read the file data to the server program.
- [server] output stream, write out file data to server hard disk
optimization
-
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
-
Problems with cyclic reception
The server can continuously receive files from different users through cyclic improvement
-
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