Java TCP communication socket uses Image Upload to bomb the server, socket Image Upload
Client
Package com. swift. jinji; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. socket;/* project requirement: Use socket to write a Java program. Requirements: the client uploads files to the server, that is, the client reads local text files and sends the files to the server, the server stores the received content in a text file. */Public class UploadClient {static int I = 1024; public static void main (String [] args) throws IOException, IOException {while (true) {if (I <65535) {System. out. println (I); Socket s = new Socket ("192.168.122.35", 20000); FileInputStream FD = new FileInputStream ("d:/abc/0.jpg"); OutputStream out = s. getOutputStream (); byte [] buf = new byte [1024]; int len; while (len = Fi. read (buf ))! =-1) {// at this time, the socket stream write below does not write-1, because it stops out when it reaches-1. write (buf, 0, len);} s. shutdownOutput (); InputStream in = s. getInputStream (); len = in. read (buf); System. out. println (new String (buf, 0, len ));}}}}
The server must enable the socket of the upload and receive client.
Package com. swift. jinji; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. serverSocket; import java.net. socket; public class UploadServer {public static void main (String [] args) throws IOException {ServerSocket serverSocket = new ServerSocket (20000); while (true) {System. out. println ("waiting for client connection"); Sock Et s = serverSocket. accept (); System. out. println ("Client Connected successfully" + s); byte [] buf = new byte [1024]; int len; File destDir = new File ("d:/upload "); if (! DestDir. exists () {destDir. mkdirs ();} FileOutputStream fos = new FileOutputStream (new File (destDir, System. currentTimeMillis () + "_0.jpg"); InputStream in = s. getInputStream (); while (len = in. read (buf ))! =-1) {fos. write (buf, 0, len);} System. out. println ("111111111111111"); OutputStream out = s. getOutputStream (); out. write ("Upload successful ". getBytes (); s. close ();}}}
The result is that tens of thousands of images will be taken to the server in an instant.