Java TCP communication socket socket with image upload bombing server

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.