Java Socket-based file UpLoad code

Source: Internet
Author: User

Write a simple code for file transfer. You can transfer files in local folders to the Server.

Server code:

Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java.net. ServerSocket;
Import java.net. Socket;

Public class FileUpLoadProjServer extends Thread {

Public FileUpLoadProjServer (Socket s, String c) throws IOException {
}

Public void run (){
}

Public static void main (String [] args ){
Try {
ServerSocket server = new ServerSocket (8110 );
Socket connection = null;
While (true ){
Try {
Connection = server. Accept ();
Inputstreamreader in = new inputstreamreader (connection
. Getinputstream ());

Long time = system. currenttimemillis ();
String T = "C: // temp _" + time;
File myfile = NULL;
If (new file (T). mkdir ())){
Myfile = new file (t + "// temp.txt ");
} Else {
System. Out. println ("create folder failed! ");
}

FileOutputStream fos = new FileOutputStream (myFile );

Int ch = 0;

While (ch = in. read ())! =-1 ){
System. out. print (char) ch );
Fos. write (ch );
}
Fos. close ();
Connection. close ();
} Catch (IOException ioe ){
System. err. println (ioe );
} Finally {
Try {
If (connection! = Null)
Connection. close ();
} Catch (IOException e ){
}
}
}
} Catch (IOException ee ){
System. err. println (ee );
}
}
}

 

Client code:

Import java. io. BufferedWriter;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. IOException;
Import java. io. OutputStreamWriter;
Import java. io. PrintWriter;
Import java.net. InetAddress;
Import java.net. Socket;

Public class FileUpLoadProjClient extends Thread {

Private Socket socket;
Private PrintWriter out;
Final int port = 8110;
String path = "C: // src // test ";

Public FileUpLoadProjClient (InetAddress addr ){
Try {
Socket = new Socket (addr, port );
} Catch (IOException e ){

}

Try {
Out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (
Socket. getOutputStream (), true );
Start ();
} Catch (IOException e ){
Try {
Socket. close ();
} Catch (IOException e2 ){
}
}
}

Public void run (){
Try {
File root = new File (path );
String [] colum = root. list ();
System. out. println ("The file's num is:" + colum. length );

For (int I = 0; I System. out. println ("The colum's content is:" + colum [I]);
String filePath = path + "//" + colum [I];
System. out. println ("The file's absolutePath is:" + filePath );
FileInputStream FCM = new FileInputStream (filePath );
Int ch = 0;

While (ch = FCM. read ())! =-1 ){
System. out. print (char) ch );
Out. write (ch );
}
FCM. close ();
}
Out. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
Socket. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}

Public static void main (string [] ARGs) throws ioexception,
Interruptedexception {
Inetaddress ADDR = inetaddress. getbyname ("127.0.0.1 ");
New fileuploadprojclient (ADDR );
}

}

You can use it together.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 1921438

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.