public static void main (String [] args) throws IOException {
//1. To create local FileInputStream object, read data
FileInputStream fis=new FileInputStream (" E: \ \ Java \ \ test JPG ");
//2. Create a client Socket object, the construction method of binding IP address and port number
The Socket s=new Socket (" 127.0.0.1 ", 8888);
//using methods on the Socket getOutStream, access to the network output byte stream OutputStream object
OutputStream OS=s.g etOutputStream ();
//4. Local FileInputStream the read method is used to read local data
byte[] bytes=new byte[1024];
int len=0;
While ((len=fis. Read (bytes))!=1) {
//5. Using the network output byte stream OutStream way to write, to read files uploaded to the server
OS. Write (bytes, 0, len);
}
//6. Use the method getInputStream Socket, to obtain network InputStream object in the input stream of bytes
InputStream is=s.g etInputStream ();
//7. Using the network byte input stream method of InputStream read read data server to write
While ((len=is. Read (bytes))!=1) {
System. The out. Println (new String (bytes, 0, len));
}
//8. Release resources
S.c lose ();
fis.close();
}
Have bosses have a look at ah, thank you
CodePudding user response:
Emmm, changed after a class, but why not show the server response message[code=Java]//1. ServeSocket, create a server object and system to port
ServerSocket serve=new ServerSocket (8888);
//2. The use of ServeSocket object the accept () method gets the client object
The Socket Socket=serve. The accept ();
//3. Use the method getInputStream in the Socket, access network InputStream object in the input stream of bytes
InputStream ins=socket. GetInputStream ();
//4. Query whether there is a destination
The File File=new File (" E: \ \ Java \ \ Test ");
if (! File. The exists ()) {
File. The mkdir ();
}
/*
* custom naming conventions
*/
The String filename="\ \ yf" + System. CurrentTimeMillis () + new Random () nextInt + ". JPG "(8888);
//5. Create a local bytes output stream FileOutputStream, bind the output destination
//FileOutputStream fos=new FileOutputStream (file + "\ \ sc. JPG");
FileOutputStream fos=new FileOutputStream (file + filename);
//6. Using the method of InputStream network bytes read, read to upload file
byte[] bytes=new byte[1024];
int len=0;
While ((len=ins. Read (bytes))!=1) {
//7. Using local output stream object FileOutStream way to write, read the harbinger of file to the server hard disk
Fos. Write (0 bytes, len);
}
//8. Use a socket object getOutputStream () method gets to the network output byte stream OutputStream object
//9. Use the Internet OutputStream object, write back to upload success
Socket. GetOutputStream (). The write (" uploaded successfully. "getBytes ());
//10. Close the flow
fos.close();
Socket. The close ();
Serve. The close (); The