Client
PackageCom.swift.jinji;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.Socket;/*Project requirements: Using the socket to write a Java program, requires: the implementation of the client to upload files to the server, that is, the client to read the local text file, the file sent to the server, the server will receive the content into a text file. */ Public classuploadclient {Static inti = 1024; Public Static voidMain (string[] args)throwsIOException, IOException { while(true) { if(I < 65535) {System.out.println (i); Socket s=NewSocket ("192.168.122.35", 20000); FileInputStream FIS=NewFileInputStream ("D:/abc/0.jpg"); OutputStream out=S.getoutputstream (); byte[] buf =New byte[1024]; intLen; while(len = Fis.read (BUF))! =-1) { //this time the following socket stream write write no write-1, because to 1 stopsOut.write (buf, 0, Len); } s.shutdownoutput (); InputStream in=S.getinputstream (); Len=In.read (BUF); System.out.println (NewString (buf, 0, Len)); } } }}
Server side need to open the upload receive client socket only line
PackageCom.swift.jinji;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classUploadserver { Public Static voidMain (string[] args)throwsIOException {serversocket serversocket=NewServerSocket (20000); while(true) {System.out.println ("Waiting for client connections"); Socket s=serversocket.accept (); System.out.println ("Client Connection Successful" +s); byte[] buf =New byte[1024]; intLen; File DestDir=NewFile ("D:/upload"); if(!destdir.exists ()) {Destdir.mkdirs (); } FileOutputStream Fos=NewFileOutputStream (NewFile (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 succeeded". GetBytes ()); S.close (); } }}
The effect is that there will be tens of thousands of images going to the server in a flash.
Java TCP communication socket socket with image upload bombing server