Write your own simple Web application Server (4)-File download with socket implementation

Source: Internet
Author: User

Directly on the source:

Server:

1  Packagedownload;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.File;6 ImportJava.io.FileInputStream;7 Importjava.io.IOException;8 ImportJava.io.OutputStream;9 ImportJava.net.ServerSocket;Ten ImportJava.net.Socket; One  A /** - * Objective: To use the socket to implement the file download. When a client connects to the server, the file is automatically downloaded from the server, and the client - * Name the downloaded file according to the number of milliseconds in the system.  the * Prepare: Prepare the file D:/1.mp3 to be downloaded on the server.    After the client accepts the file, save the file in the D drive, and the file takes the system millisecond number to name.  -  */ -  Public classServer { -      +     PrivateServerSocket SS; -     Private intPort = 8080;//Server-Open port number +     /**using the construction method, initialize the ServerSocket*/ A      PublicServer () { at         Try { -SS =NewServerSocket (port); -}Catch(IOException e) { - e.printstacktrace (); -         } -     } in     /**the server starts and waits for the client to enter. When the client enters, the socket object is generated and the object is given to the thread to execute*/ -      Public voidstart () { to          while(true){ +             Try { -Socket s = ss.accept ();//wait for the client to enter theThread thread =NewThread (NewHandler (s));//using constructors to pass parameters * Thread.run (); $}Catch(IOException e) {Panax Notoginseng e.printstacktrace (); -             } the         } +     } A     Private classHandlerImplementsRunnable { the         PrivateSocket S; +         Private byte[] buf =New byte[1024];//1K-size buffers -          PublicHandler (Socket s) { $              This. S =s; $         } -  -          Public voidrun () { the             Try { -File sourcefile =NewFile ("D:/1.mp3");WuyiFileInputStream FIS =NewFileInputStream (sourcefile); theBufferedinputstream bis =NewBufferedinputstream (FIS); -OutputStream OS =S.getoutputstream (); WuBufferedoutputstream BOS =Newbufferedoutputstream (OS); -                 intsize =-1; About                  while((Size=bis.read (BUF))!=-1){ $Bos.write (buf,0, size); - Bos.flush (); -                 } - s.close (); A fis.close (); +}Catch(Exception e) { the e.printstacktrace (); -             } $         } the  the     } the      Public Static voidMain (string[] args) { theServer s =NewServer (); - S.start (); in     } the}

Client:

1  Packagedownload;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.File;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 ImportJava.io.InputStream;9 ImportJava.net.Socket;Ten Importjava.net.UnknownHostException; One /**client program, responsible for downloading files from client*/ A  Public classClient { -     PrivateSocket S; -     Private byte[] buf =New byte[1024];//1K-size buffers the      -      Public voidstart () { -         Try { -s =NewSocket ("192.168.1.103", 8080); +String fileName = "d:/" +system.currenttimemillis () + ". mp3"; -File targetfile =NewFile (fileName); + targetfile.createnewfile (); AFileOutputStream fos =NewFileOutputStream (targetfile); atBufferedoutputstream BOS =NewBufferedoutputstream (FOS); -InputStream is =S.getinputstream (); -Bufferedinputstream bis =NewBufferedinputstream (IS); -             intsize =-1; -              while((Size=bis.read (BUF))!=-1){ -Bos.write (buf,0, size); in Bos.flush (); -             } to s.close (); + fos.close (); -}Catch(unknownhostexception e) { the e.printstacktrace (); *}Catch(IOException e) { $ e.printstacktrace ();Panax Notoginseng         } -     } the      Public Static voidMain (string[] args) { +Client C =NewClient (); A C.start (); the     } +}

Write your own simple Web application Server (4)-File download with socket implementation

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.