File upload of network programming 1

Posted by jbog91 on Fri, 23 Aug 2019 11:19:21 +0200

package com.itheima.demo02.FileUpload;

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

/*

Client of file upload case: read local file, upload to server, read data written back by server

To make clear:
    Data source: c:\1.jpg
    Destination: Server

Implementation steps:
    1. Create a FileInputStream object for the local byte input stream and bind the data source to be read in the constructor
    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 Socket to get the OutputStream object of network byte output stream
    4. Read the local file using the method read in the FileInputStream object of the local byte input stream
    5. Use the method write in the OutputStream object of the network byte output stream to upload the read files to the server
    6. Use the method getInputStream in Socket to get the InputStream object of network byte input stream
    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. File Input Stream (Socket)

*/
public class TCPClient {

public static void main(String[] args) throws IOException {
    //1. Create a FileInputStream object for the local byte input stream and bind the data source to be read in the constructor
    FileInputStream fis = new FileInputStream("c:\\1.jpg");
    //2. Create a client Socket object and bind the IP address and port number of the server in the construction method.
    Socket socket = new Socket("127.0.0.1",8888);
    //3. Use the method getOutputStream in Socket to get the OutputStream object of network byte output stream
    OutputStream os = socket.getOutputStream();
    //4. Read the local file using the method read in the FileInputStream object of the local byte input stream
    int len = 0;
    byte[] bytes = new byte[1024];
    while((len = fis.read(bytes))!=-1){
        //5. Use the method write in the OutputStream object of the network byte output stream to upload the read files to the server
        os.write(bytes,0,len);
    }

    /*
        Solution: Upload the file and write an end tag to the server
        void shutdownOutput() Disable the output stream of this socket.
        For TCP sockets, any previously written data will be sent, followed by the normal connection termination sequence of TCP.
     */
    socket.shutdownOutput();

    //6. Use the method getInputStream in Socket to get the InputStream object of network byte input stream
    InputStream is = socket.getInputStream();

    System.out.println("333333333333333333333");

    //7. Use the method read in the InputStream object of the network byte input stream to read the data written back by the service
    while((len = is.read(bytes))!=-1){
        System.out.println(new String(bytes,0,len));
    }

    System.out.println("444444444444444444  while Dead-loop printing is not possible");

    //8. File Input Stream (Socket)
    fis.close();
    socket.close();
}

}

package com.itheima.demo02.FileUpload;

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

/*

File upload case server side: read the file uploaded by the client, save it to the server's hard disk, and write back "upload success" to the client side.

To make clear:
    Data Source: Files Uploaded by Client
    Destination: Server's hard disk d:\upload\1.jpg

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 get the requested client Socket object
    3. Use the method getInputStream in the Socket object to get the InputStream object of the network byte input stream.
    4. Determine whether the d:\upload folder exists or not, and create it if it does not exist.
    5. Create a FileOutputStream object for the local byte output stream and bind the destination to be output in the constructor
    6. Read the files uploaded by the client using the method read in the InputStream object of the network byte input stream
    7. Use the method write in the FileOutputStream object of the local byte output stream to save the read files to the server's hard disk
    8. Use the method getOutputStream in the Socket object to get the network byte output stream OutputStream object
    9. write back the "upload success" to the client using the method in the OutputStream object of the network byte output stream.
    10. Release Resources (File Output Stream, Socket, Server Socket)

*/
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 the method accept in the ServerSocket object to get the requested client Socket object
    Socket socket = server.accept();
    //3. Use the method getInputStream in the Socket object to get the InputStream object of the network byte input stream.
    InputStream is = socket.getInputStream();
    //4. Determine whether the d:\upload folder exists or not, and create it if it does not exist.
    File file =  new File("d:\\upload");
    if(!file.exists()){
        file.mkdirs();
    }


    //5. Create a FileOutputStream object for the local byte output stream and bind the destination to be output in the constructor
    FileOutputStream fos = new FileOutputStream(file+"\\1.jpg");
    //6. Read the files uploaded by the client using the method read in the InputStream object of the network byte input stream

    System.out.println("11111111111111111111");

    int len =0;
    byte[] bytes = new byte[1024];
    while((len = is.read(bytes))!=-1){
        //7. Use the method write in the FileOutputStream object of the local byte output stream to save the read files to the server's hard disk
        fos.write(bytes,0,len);
    }

    System.out.println("22222222222222222222222  while Dead-loop printing is not possible");

    //8. Use the method getOutputStream in the Socket object to get the network byte output stream OutputStream object
    //9. write back the "upload success" to the client using the method in the OutputStream object of the network byte output stream.
    socket.getOutputStream().write("Upload Success".getBytes());
    //10. Release Resources (File Output Stream, Socket, Server Socket)
    fos.close();
    socket.close();
    server.close();
}

}
It won't go to 2222 or 44 because it's blocked after read.

Topics: Java socket network