Clientfiles.java
/** * More information Welcome to the Kay Academy website: http://kaige123.com * @author Small Mo * * PackageCOM.FILES.MC;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.Socket;ImportJava.net.UnknownHostException; Public class clientfiles extends Thread {Public void run() {Try{Socket socket =NewSocket ("127.0.0.1",8080); InputStream fin = Socket.getinputstream (); OutputStream fout = Socket.getoutputstream (); File File =NewFile ("e:/Thunder download/170322-04.mp4");LongTimes = System.currenttimemillis (); System.out.println ("\ t is verifying"+ file.getname () +"File MD5 please later ... \ n"); String MD5 = md5fileutil.getfilemd5string (file);//MD5 encryption of this file dataSystem.out.println ("[MD5 Verification is time consuming:"+ (System.currenttimemillis ()-times)/ ++"SEC is performing data transfer]\n"); Fout.write (Md5.getbytes ());//Send the encrypted MD5 to the serverFout.flush ();if(Fin.read () = =0) {//The server is transmitting 0 instructions on the server with the same data . //Print seconds passSystem.out.println ("Data transfer complete!" Size: "+ file.length () +"bytes Time: 0 seconds! Second transmission mechanism ");}Else{//Print is not 0, then start transferring data to the server LongTime = System.currenttimemillis (); System.out.println ("\t\t data transfer ... \ n"); FileInputStream Filein =NewFileInputStream (file);byte[] B =New byte[1024x768*1024x768*2]; while(Filein.available ()! =0) {intLen = Filein.read (b); Fout.write (b),0, Len); Fout.flush ();} System.out.println ("File transfer complete!" Size: "+ file.length () +"bytes time consuming:"+ (System.currenttimemillis ()-time)/ ++"Seconds"); Filein.close ();} Fout.close (); Fin.close (); Socket.close ();}Catch(IOException e) {E.printstacktrace ();}}Public static void main(string[] args) {NewClientfiles (). Start ();//Start the client thread}}
Serverfiles.java
/** * More information Welcome to the Kay Academy website: http://kaige123.com * @author Small Mo * * PackageCOM.FILES.MC;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.ServerSocket;ImportJava.net.Socket;ImportJava.util.concurrent.ExecutorService;ImportJava.util.concurrent.Executors;ImportJava.util.zip.InflaterOutputStream; Public class serverfiles implements Runnable {PrivateSocket socket;Public serverfiles(socket socket) { This. socket = socket;}Public void run() {Try{InputStream fin = Socket.getinputstream (); OutputStream fout = Socket.getoutputstream ();byte[] B =New byte[1024x768];intLen =0; len = Fin.read (b); String MD5 =NewString (b,0, Len);//Receive client-pass MD5SYSTEM.OUT.PRINTLN (MD5); File File =NewFile ("Filess"); file[] files = file.listfiles ();BooleanState =false;//state defaults to False to determine if MD5 exists for(File file2:files) {//If the same MD5 are found if(File2.getname (). Equalsignorecase (MD5)) {state =true;the//status is changed to True to indicate that there is the same MD5 Break;}}if(state = =false) {//did not find the same MD5 to send to the client 1 past let the client start to send the data overFout.write (1); Fout.flush (); FileOutputStream fileout =NewFileOutputStream (NewFile ("Filess", MD5));byte[] B1 =New byte[1024x768*1024x768*2];//Store the data sent by the client in a file while(len = Fin.read (b))! =-1) {Fileout.write (B1,0, Len);} Fileout.close ();}Else{//If the same MD5 is found then send 0 to the clientFout.write (0); Fout.flush ();} Fout.close (); Fin.close (); Socket.close ();}Catch(IOException e) {E.printstacktrace ();}}Public static void openserver() throws Exception{ServerSocket Server =NewServerSocket (8080); Executorservice executor = Executors.newfixedthreadpool (Ten);//thread pool while(true) {ServerFiles socket =NewServerFiles (Server.accept ()); Executor.execute (socket);}}Public static void main(string[] args) {Try{OpenServer ();//Start service side}Catch(Exception e) {E.printstacktrace ();}}}
File Second Pass