Java Socket-based file transfer instance

Source: Internet
Author: User

1: client code:

@ Test
Public void socketclient () throws ioexception {
Socket S = NULL;
String filepath = "f :\\ yingtao. inw ";
File file = new file (filepath );
S = new socket ("127.0.0.1", 10000 );
Datainputstream FD = new datainputstream (New bufferedinputstream (New fileinputstream (File )));
Dataoutputstream PS = new dataoutputstream (S. getoutputstream ());
Try {
Long filesize = file. Length ();
System. Out. println ("filesize --->" + filesize );
String filename = file. getname ();
// Send the file name and length to the receiver
PS. writeutf (filename );
PS. Flush ();

// Send the file length to the receiver
PS. writelong (filesize );
PS. Flush ();
Byte [] Buf = new byte [1, 1024];
While (true ){
Int size = 0;
Int Len = FCM. Read (BUF );
Size + = Len;
If (LEN =-1 ){
Break;
}
PS. Write (BUF, 0, Len );
}

} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
} Finally {
Try {
If (PS! = NULL ){
PS. Flush ();
PS. Close ();
}
If (FS! = NULL)
FCM. Close ();
If (s! = NULL)
S. Close ();
} Catch (exception E2 ){
// Todo: handle exception
}
}
}


2: receiver code.


@ Test
Public void socket_server (){
Serversocket Ss = NULL;
Try {
Ss = new serversocket (10000 );
System. Out. println ("waiting for client connection! ");
Int I = 1;
While (true ){
Socket S = ss. Accept ();
System. Out. println ("the" + I + "client is connected! ");
Acceptfilethread at = new acceptfilethread (S, I );
At. Start ();
I ++;
}
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
}

}


Class acceptfilethread extends thread {
Private socket S;
Private int filenumber;
Public acceptfilethread (socket S, int filenumber ){
This. S = s;
This. filenumber = filenumber;
}
@ Override
Public void run (){
// Todo auto-generated method stub
Datainputstream in = NULL;
Dataoutputstream out = NULL;
Try {
In = new datainputstream (New bufferedinputstream (S. getinputstream ()));
System. Out. println ("no." + filenumber + "file start upload ");
String filename = in. readutf ();
System. Out. println ("file name ---->" + filename );
Long filesize = in. readlong ();
System. Out. println ("file length --->" + filesize );
System. Out. println ("start to accept files! ");
Stringbuilder builder = new stringbuilder ();
Builder. append ("F: \ server_socket \"). append (filename );
File SaveFile = new file (builder. tostring ());
Out = new dataoutputstream (New bufferedoutputstream (New fileoutputstream (SaveFile )));
Byte [] buff = new byte [1024];
Int size = 0;
While (true ){
Int Len = in. Read (buff, 0,1024 );
System. Out. println ("Len --->" + Len );
If (LEN =-1 ){
Break;
}
Out. Write (buff, 0, Len );
Size + = Len;
System. Out. println ("size ---->" + size );
System. Out. println ("file accepted" + (size * 100/filesize) + "% ");
}
System. Out. println ("file" + filenumber + "accepted! ");
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
} Finally {
Try {
If (OUT! = NULL ){
Out. Flush ();
Out. Close ();
}
If (in! = NULL ){
In. Close ();
}

} Catch (exception E2 ){
// Todo: handle exception
E2.printstacktrace ();
}
}
Super. Run ();
}


}

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.