I wrote a simple follow tutorial file upload process, file upload is successful, but the client is read less than the server back to write data, for bosses to help look at what the problem is, the code is as follows:
The client code:
Package com. Java. Gy. Day20. Upload;
Import the Java. IO. FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
The import java.net.Socket;
The import java.net.UnknownHostException;
Public class TCPClient {
Public static void main (String [] args) throws UnknownHostException, IOException {
//TODO automatically generated method stub
//1. To create local byte input stream FileInputStream object
FileInputStream fis=new FileInputStream (" D: \ \ Resources \ \ 123 \ \ 1. JPG ");
//2. Create the client Socket object
The Socket Socket=new Socket (" 127.0.0.1 ", 8888);
//3. The use of the Socket object getOutputStream method, create network output stream object OutputStream
OutputStream OS=socket. GetOutputStream ();
//4. Using local byte input stream FileInputStream object read methods, read a local file
//5. Using the network output stream OutputStream object write method, send data to the server
Int len=0;
Byte [] bytes=new byte [1024].
While ((len=fis. Read (bytes))!=1) {
OS. Write (bytes, 0, len);
}
//6. The use of the Socket object getInputStream method, create network input stream InputStream object
System. The out. Println (" 1 ");
InputStream is=socket. GetInputStream ();
//7. Using the network input stream InputStream object the read method, data read from the server back to write
While ((len=is. Read (bytes))!=1) {
System. The out. Println (new String (bytes, 0, len));
}
System. The out. Println (" 3 ");
//8. Release resources
fis.close();
Socket. The close ();
}
The server-side code:
Package com. Java. Gy. Day20. Upload;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
The import java.net.ServerSocket;
The import java.net.Socket;
Public class TCPServer {
Public static void main (String [] args) throws IOException {
//TODO automatically generated method stub
//1. Create a server side ServerSocket object, and bind the designated port
ServerSocket server=new ServerSocket (8888);
//2. The use of a ServerSocket object accept method, to obtain the request of the Socket object
The Socket Socket=server. The accept ();
//3. Create a FileOutputStream object and specify the location of the file on the server storage
FileOutputStream fos=new FileOutputStream (" D: \ \ Resources \ \ \ \ new folder (1).jpg ");
//4. Use the Socket object getInputStream method, to obtain network input stream InputStream object
InputStream is=socket. GetInputStream ();
//5. Using the network input stream InputStream object the read method, read the client transfer data
//6. Write method, using a FileOutputStream object file is written to the server specify the location of the
Int len=0;
Byte [] bytes=new byte [1024].
While ((len=is. Read (bytes))!=1) {
//System. Out. Println (len);
Fos. Write (0 bytes, len);
}
System. The out. Println (" 2 ");
//7. Use the Socket object getOutputStream method, access to the network output stream object OutputStream
OutputStream OS=socket. GetOutputStream ();
//8. Using the network output stream OutputStream object of the write method to write data back to the client "uploaded successfully
"OS. Write (" uploaded successfully. "getBytes ());
//9. Release resources
Fos. Close ();
Socket. The close ();
Server. The close ();
}
}