Simple use of socket 01-upload and download files

Source: Internet
Author: User
Tags readfile

PC Remote Host Program

Send. Java

Import java. AWT. image. databuffer;
Import java. AWT. image. databufferbyte;
Import java. Io. bufferedinputstream;
Import java. Io. bufferedreader;
Import java. Io. bufferedwriter;
Import java. Io. datainputstream;
Import java. Io. dataoutputstream;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. Io. outputstream;
Import java. Io. outputstreamwriter;
Import java.net. serversocket;
Import java.net. Socket;

/**
* Filename: Send. Java
* Company: *** Limited (Android) 2012-2015
* Compiler: Eclipse 3.7.1
* Remark: Todo
* Created: 2012-3-5 weeds
* Changed: latecomer
*/

/**
* Filename: Todo
*/
Public class send {

/**
* @ Param ARGs
*/
Static file fname;
Static int Port = 8888;
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Try {
Readfile ("d.txt ");
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Public static void sendfile (string filename)
{
Fname = new file (filename );
Try {
Fname. createnewfile ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}
Public static void readfile (string filename) throws ioexception
{
Socket cs = NULL;
Try {
System. Out. println ("waiting for connection ...");
Serversocket Ss = new serversocket (port );
While (true ){
String filepath = "d :\\ yc.rar ";
File Fi = new file (filepath );
System. Out. println ("file length:" + (INT) Fi. Length ());
Cs = ss. Accept ();
System. Out. println ("establish socket link ");
Datainputstream Dis = new datainputstream (New bufferedinputstream (CS. getinputstream ()));
Dis. readbyte ();
Datainputstream fdata = new datainputstream (New bufferedinputstream (New fileinputstream (filepath )));
Dataoutputstream PS = new dataoutputstream (CS. getoutputstream ());
PS. writeutf (Fi. getname ());
PS. Flush ();
PS. writelong (long) Fi. Length ());
PS. Flush ();
Byte [] B = new byte [102400];
While (true ){
Int READ = 0;
If (fdata! = NULL ){
Read = fdata. Read (B );

}
If (read =-1 ){
Return;
}
PS. Write (B, 0, read );
PS. Flush ();
Fdata. Close ();

Dis. Close ();
CS. Close ();
SS. Close ();
}
}

} Catch (exception e ){
// Todo: handle exception

}
}






}

 

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.