Java Server multi-thread processing File Upload

Source: Internet
Author: User

 

Import Java. io. *; import java.net. *; Class uploadthread implements runnable // encapsulate the upload to the thread {private Socket Client; Public uploadthread (socket s) {This. client = s;} public void run () {string IP = client. getinetaddress (). gethostaddress (); // get the IP address try {bufferedinputstream sin = new bufferedinputstream (client. getinputstream (); // socket reads the stream file = new file ("C: \ 1.jpg"); bufferedoutputstream fout = new buffere Doutputstream (New fileoutputstream (File); // file output stream byte Buf [] = new byte [1024]; int Len = 0; system. out. println (IP + "... connecting... "); // start to read data from the network while (LEN = sin. read (BUF ))! =-1) {fout. write (BUF, 0, Len);} // bufferedoutputstream sout = new bufferedoutputstream (client. getoutputstream (); printstream sout = new printstream (client. getoutputstream (); sout. write ("sent successfully ". getbytes (); // sout. flush (); // although it is a byte stream, it uses bufferedoutputstreamfout. close (); sin. close (); sout. close ();} catch (exception ex) {system. out. println ("File Upload Failed") ;}} public class server {public static void main (string ARGs []) throws exception {serversocket Server = new serversocket (2222 ); while (true) {Socket Client = server. accept (); New thread (New uploadthread (client )). start () ;}}/ ** the server handles multithreading ** 1. because the server requires a lot of people to access, it must be processed using multiple threads. Otherwise, only one person can access the server, which is also called the y server ** 2, taking the upload example of this file as an example, it encapsulates every user connected to it into a thread and defines the operation to be executed by the user into the run Method * One user takes one thread, when the thread is obtained, execute it by yourself. If other users come, allocate a new thread to the new user * to solve the problem of multi-thread processing on the server * 3. when transmitting data between the server and the client, we should pay special attention to the following reasons: ** 1. when the client sends data to the data end, when the character is sent, the first line is sent. When the server reads the data, it also reads the data in the * line, Readline () when sending a line feed, the line feed is often sent, but the line feed cannot be sent. * when the server reads the line feed, the Readline () will not stop * 2. when sending data, the client must refresh the stream if it uses a response stream or a buffer stream. Otherwise, data will not be sent out * 3 when I/O is used to read the data in the file and then sent to the server, the current user reads the file while (in. read () when the file is read, while the receiving program on the * server while (sin. read () will not receive a message indicating that the file has been sent, so it will wait for a while. So when we are dealing with this * problem, we will also send a mark indicating that the file has been read, tell the receiving end that the file has been read. Do not wait. * The socket encapsulates two shutdowninput shutdownoutput operations for us to disable the stream, you can also tell the * recipient that the file has been transferred *****/

 

Import java.net. *; import Java. io. *; public class upload {public static void main (string ARGs []) throws exception {Socket Client = new socket ("172.16.215.105", 2222 ); file file = new file ("F: \ e.jpg"); bufferedinputstream fin = new bufferedinputstream (New fileinputstream (File )); // File Read stream printstream sout = new printstream (client. getoutputstream (), true); // obtain the socket stream byte Buf [] = new byte [1024]; int Len = 0; W Hile (LEN = fin. Read (BUF ))! =-1) {sout. write (BUF, 0, Len); system. out. println (LEN + "... sending ");} client. shutdownoutput (); bufferedinputstream sin = new bufferedinputstream (client. getinputstream (); Len = sin. read (BUF); system. out. println (LEN); system. out. println (new string (BUF, 0, Len); sin. close (); sout. close (); Fin. close ();}}

 

 

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.