Use SOCKET to upload images

Source: Internet
Author: User

Implementation ideas:

The client obtains the file stream, writes the file stream to the output stream specified to a server through socket, and obtains the input stream through socket on the server, write data to a specified folder. To allow multiple users to upload data at the same time, the file operation on the client to be uploaded on the server must be put in another thread to run.

Complete code:

View plain
Import java.net .*;
Import java. Io .*;

/*
The server encapsulates the obtained client in a separate thread.
*/
Class jpgclient2
{
Public static void main (string [] ARGs) throws exception
{
// Check the file
If (ARGs. Length = 0)
{
System. Out. println ("specify a JPG file first! ");
Return;
}
File file = new file (ARGs [0]);
If (! (File. exists () & file. isfile () & file. getname (). endswith (". jpg ")))
{
System. Out. println ("incorrect file selection. Please select a correct file again. ");
Return;
}

// Read the file and write it to the server
Socket S = new socket ("192.168.137.199", 9006 );
Fileinputstream FCM = new fileinputstream (File );
Outputstream out = S. getoutputstream ();
Byte [] Buf = new byte [1, 1024];
Int Len = 0;
While (LEN = FS. Read (BUF ))! =-1)
{
Out. Write (BUF, 0, Len );
}

// Notify the server to end sending data
S. shutdownoutput ();

// Obtain the Server Response
Inputstream in = S. getinputstream ();
Byte [] bufin = new byte [1, 1024];
Int num = in. Read (bufin );
String STR = new string (bufin, 0, num );
System. Out. println (STR );

FCM. Close ();
S. Close ();
}
}

Class jpgthread implements runnable
{
Private socket S;
Jpgthread (socket S)
{
This. S = s;
}
Public void run ()
{
Int COUNT = 1;

String IP = S. getinetaddress (). gethostaddress ();

Try
{
// Obtain client data
Inputstream in = S. getinputstream ();

// Specify the file storage path to read the data submitted by the customer into the file
File dir = new file ("C: \ pic ");
File file = new file (Dir, IP + "(" + Count + ").jpg ");
While (file. exists ())
File = new file (Dir, IP + "(" + (count ++) + ").jpg ");
Fileoutputstream Fos = new fileoutputstream (File );
Byte [] Buf = new byte [1, 1024];
Int Len = 0;
While (LEN = in. Read (BUF ))! =-1)
{
FOS. Write (BUF, 0, Len );
}

// Return the upload status to the client
Outputstream out = S. getoutputstream ();
Out. Write ("File Uploaded successfully". getbytes ());

FOS. Close ();
S. Close ();
}
Catch (exception E)
{
System. Out. println (E. tostring ());
}
}
}
Class jpgserver2
{
Public static void main (string [] ARGs) throws exception
{
Serversocket Ss = new serversocket (9006 );

// Enable concurrent thread access
While (true)
{
Socket S = ss. Accept ();
String IP = S. getinetaddress (). gethostaddress ();
System. Out. println (IP + "... connected ");
New thread (New jpgthread (s). Start ();
}
}
}

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.