//Client Section PackageTestsix;ImportJava.awt.Graphics;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.InetAddress;ImportJava.net.Socket;ImportJava.util.Iterator;ImportJavax.imageio.ImageIO;ImportJavax.imageio.ImageReader;ImportJavax.imageio.stream.ImageInputStream;ImportJavax.swing.ImageIcon;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;classMypanelextendsjpanel{PrivateImageIcon II; PublicMypanel (ImageIcon II) { This. II =II; } protected voidpaintcomponent (Graphics g) {Super. paintcomponent (g); G.drawimage (Ii.getimage (),0, 0, 300, 400, This); }} Public classSocketdemoextendsjframe{PrivateSocket st =NULL; PublicSocketdemo () {Try{ Try{ //Establish clientSt =NewSocket (Inetaddress.getlocalhost (), 10000); //returns the output stream of a socket that has an associated channel, the resulting output stream delegates all of its operations to the channelOutputStream OS =St.getoutputstream (); byte[] bt = "Buddy, give me a picture of the background!" ". GetBytes (); Os.write (BT); //returns a socket with an associated channel input stream, the resulting input stream delegates all of its operations to the channelInputStream is =St.getinputstream (); intLen =Is.read (BT); System.out.println (NewString (BT, 0, Len)); //the input stream is handled in the following manner, depending on whether the iterator<imagereader> Itimage can//The successful return of a ImageReader object confirms that the stream file is a picture file! //and ImageReader the Getformatname () in the class to get the file format! //the ImageIcon object can be established by the ImageIcon Byte[] constructor at the end! //Finally, the picture is displayed on the panel! Imageinputstream IIS =Imageio.createimageinputstream (IS); Iterator<ImageReader> Itimage =imageio.getimagereaders (IIS); if(Itimage.hasnext ()) {ImageReader Reader=Itimage.next (); byte[] Imagebyte =New byte[102400*10]; Iis.read (Imagebyte); ImageIcon II=NewImageIcon (Imagebyte, Reader.getformatname ()); Add (NewMypanel (ii)); } }finally{st.close (); } }Catch(IOException e) {e.printstacktrace (); } setSize (300, 400); SetVisible (true); Setdefaultcloseoperation (Jframe.exit_on_close); } Public Static voidMain (string[] args) {NewSocketdemo (); }}
//service-side Section PackageTestsix;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classserversocketdemo{PrivateServerSocket SST =NULL; PrivateSocket st =NULL; PublicServersocketdemo () {Try{ Try { //set up the service sideSST =NewServerSocket (10000); //listens for and accepts connections to this service socket. This method is blocked until the connection is made. Create a new socketSt =sst.accept (); //get the stream that the client sent overInputStream is =St.getinputstream (); byte[] bt =New byte[1024]; intLen =Is.read (BT); System.out.println (NewString (BT, 0, Len)); OutputStream OS=St.getoutputstream (); Os.write ("Well, know it!" ". GetBytes ()); //transfer the picture file to the pastFileInputStream FIS =NewFileInputStream (NewFile ("Q12.jpg")); while(len = Fis.read (BT))! =-1) {//change the stream file to byte[] and then send it to the client using the socket's output streamOs.write (BT); Os.flush (); } }finally{st.close (); Sst.close (); } }Catch(IOException e) {e.printstacktrace (); } } Public Static voidMain (string[] args) {NewServersocketdemo (); }}
Transfer and display of picture files in Java (socket and ServerSocket demo)