File upload principle
Principle: the client reads the local file, uploads the file to the server, and the server saves the uploaded file to the hard disk of the server
1. The client uses the local byte input stream to read the file to be uploaded
2. The client uses the network byte output stream to upload the read file to the server
3. The server uses the network byte input stream to read the files uploaded by the client
4. The server uses the local byte output stream to save the read file to the hard disk of the server
5. The server uses the network byte output stream to write back a "upload successful" to the client
6. The client uses the network byte input stream to read the data written back by the server
7. Release resources
be careful:
Clients, servers and local hard disks need to use their own byte stream objects (local streams) to read and write
For reading and writing between the client and the server, the byte stream object (network stream) provided in the Socket must be used
The principle of file uploading is file copying
Client of file upload case
Read local files, upload them to the server, and read the data written back by the server
2.1 clarify:
Data source: G:\ \ picture \1 png
Destination: Server
2.2 implementation steps:
1. Create a local byte input stream FileInputStream object, and bind the data source to be read in the construction method
2. Create a client Socket object, and bind the IP address and port number of the server in the construction method
3. Use the method getOutputStream in the Socket to obtain the network byte output stream OutputStream object
4. Use the method read in the FileInputStream object of the local byte input stream to read the local file
5. Use the method write in the OutputStream object of the network byte output stream to upload the read file to the server
6. Use the method getInputStream in the Socket to obtain the network byte input stream InputStream object
7. Use the method read in the InputStream object of the network byte input stream to read the data written back by the service
8. Release resources (FileInputStream,Socket)
public class TCPClient { public static void main(String[] args) throws IOException { //1.Create a local byte input stream FileInputStream Object, the data source to be read is bound in the construction method FileInputStream fis = new FileInputStream("G:\\picture\\1.png"); //2.Create a client Socket Object, which is bound to the server in the construction method IP Address and port number Socket socket = new Socket("127.0.0.1", 8888); //3.use Socket Methods in getOutputStream,Get network byte output stream OutputStream object OutputStream os = socket.getOutputStream(); //4.Use local byte input stream FileInputStream Methods in objects read,Read local file byte[] bytes = new byte[1024]; int len; while ((len=fis.read(bytes))!=-1){ //5.Use network byte output stream OutputStream Methods in objects write,Upload the read file to the server os.write(bytes,0,len); } //6.use Socket Methods in getInputStream,Get network byte input stream InputStream object InputStream is = socket.getInputStream(); //7.Use network byte input stream InputStream Methods in objects read Read the data written back by the service while ((len=is.read(bytes))!=-1){ System.out.println(new String(bytes,0,len)); } //8.Release resources( FileInputStream,Socket) fis.close(); socket.close(); } }
Server side of file upload case
Read the file uploaded by the client, save it to the hard disk of the server, and write back "upload succeeded" to the client
to make clear:
Data source: files uploaded by the client
Destination: hard disk of server g:\upload\a.png
Implementation steps:
1. Create a server ServerSocket object and the port number to be specified by the system
2. Use the method accept in the ServerSocket object to obtain the requested client Socket object
3. Use the method getInputStream in the Socket object to obtain the network byte input stream InputStream object
4. Judge whether the d:\upload folder exists, and create it if it does not exist
5. Create a local byte output stream FileOutputStream object, and bind the destination to be output in the construction method
6. Use the method read in the InputStream object of the network byte input stream to read the file uploaded by the client
7. Use the method write in the FileOutputStream object of the local byte output stream to save the read file to the hard disk of the server
8. Use the method getOutputStream in the Socket object to obtain the network byte output stream OutputStream object
9. Use the method write in the OutputStream object of the network byte output stream to write back "upload succeeded" to the client
10. Release resources (FileOutputStream,Socket,ServerSocket)
public class TCPServer { public static void main(String[] args) throws IOException { //1.Create a server ServerSocket Object, and the port number to be specified by the system ServerSocket server = new ServerSocket(8888); //2.use ServerSocket Methods in objects accept,Get the requested client Socket object Socket socket = server.accept(); //3.use Socket Methods in objects getInputStream,Get network byte input stream InputStream object InputStream is = socket.getInputStream(); //4.judge d:\\upload Whether the folder exists or not. If it does not exist, create it File file = new File("G:\\upload"); if (!file.exists()){ file.mkdirs(); } //5.Create a local byte output stream FileOutputStream Object, binding the destination to be output in the construction method FileOutputStream fos = new FileOutputStream(file+"\\a.png"); //6.Use network byte input stream InputStream Methods in objects read,Read the file uploaded by the client byte[] bytes = new byte[1024]; int len; while ((len=is.read(bytes))!=-1){ //7.Use local byte output stream FileOutputStream Methods in objects write,Save the read file to the hard disk of the server fos.write(bytes,0,len); } //8.use Socket Methods in objects getOutputStream,Get network byte output stream OutputStream object OutputStream os = socket.getOutputStream(); //9.Use network byte output stream OutputStream Methods in objects write,Write back "upload succeeded" to the client os.write("Upload successful".getBytes()); //10.Release resources( FileOutputStream,Socket,ServerSocket) fos.close(); socket.close(); server.close(); } }