LAN file transmission and replication, including file names

Source: Internet
Author: User
Tags sendfile

LAN file transmission and replication, including file names
 

Copy files in the LAN, including the file names. The feature is that two ports are used for file name transfer and file transfer. The Process completes this function: every 24 hours

Upload the files in the root directory of the two machines once.

1: the server side, also waiting for upload. Run the command: Java fileserver 3107
Import java. Io .*;
Import java.net .*;

Public class fileserver implements runnable {

Private Static int ipsocket = 0;

Public static void main (string [] ARGs) throws exception {
Ipsocket = integer. parseint (ARGs [0]);
// 3107 is recommended.
System. Out. println ("system on port:" + ipsocket + "waiting for sending ");
Thread T1 = new thread (New fileserver ());
T1.start ();
}

Public void run (){
While (true ){
Try {
String [] filenames = seachfilename (1 );
If (filenames = NULL | filenames. Length <1) return;
For (INT I = 0; I <filenames. length; I ++ ){
Sendfilename (filenames [I]);
Sendfile (filenames [I]);
}
Thread. Sleep (24x60*60*1000); // 24 hours
} Catch (exception e ){
System. Out. println (E );
}
}
}

/*
Listtype = 0 returns folders and files
Listtype = 1 only returns objects
*/
Public static string [] seachfilename (INT listtype) throws exception {
String directory = ".";
File dir = new file (directory );
File dirtemp;
String [] filestemp;
String [] filenames;
String [] Re;
Int length = 0;
If (! Dir. isdirectory ())
Throw new illegalargumentexception ("filelist: no such directory ");

Filestemp = dir. List ();
Java. util. arrays. Sort (filestemp );

For (INT I = 0; I <filestemp. length; I ++ ){
Dirtemp = new file (directory + "//" + filestemp [I]);
If (! Dirtemp. isdirectory ()){
Length ++;
}
}
Filenames = new string [length];
Length = 0;
For (INT I = 0; I <filestemp. length; I ++ ){
Dirtemp = new file (directory + "//" + filestemp [I]);
If (! Dirtemp. isdirectory ()){
Filenames [length] = filestemp [I];
}
Length ++;
}
If (listtype = 0) Re = filestemp;
Else Re = filenames;
Return re;

}

/* Send File Name
*/
Private Static void sendfilename (string filenames) throws exception {
If (filenames = NULL) return;
// Create a network server to accept customer requests
Serversocket Ss = new serversocket (ipsocket );
Socket Client = ss. Accept ();

// Create a network output stream and provide a data package
Outputstream netout = client. getoutputstream ();
Outputstream Doc = new dataoutputstream (New bufferedoutputstream (netout ));

// Create a file read buffer
Byte [] Buf = NULL;
String filename = filenames;
Buf = filename. getbytes ();
Int num = Buf. length;
If (Num> 0) {// whether to read the file
Doc. Write (BUF, 0, num); // Write File data into the network Buffer
Doc. Flush (); // refresh the buffer to write data to the client.
}
Doc. Close ();
SS. Close ();
}
/* File Sending itself
*/
Private Static void sendfile (string filename) throws exception {
If (filename = NULL) return;
// Create a file stream to read data in the file
File file = new file (filename );
Fileinputstream Fos = new fileinputstream (File );

// Create a network server to accept customer requests
Serversocket Ss = new serversocket (ipsocket + 1 );
Socket Client = ss. Accept ();

// Create a network output stream and provide a data package
Outputstream netout = client. getoutputstream ();
Outputstream Doc = new dataoutputstream (New bufferedoutputstream (netout ));

// Create a file read buffer
Byte [] Buf = new byte [1, 2048];
Int num = FOS. Read (BUF );
While (num! = (-1) {// whether to read the file
Doc. Write (BUF, 0, num); // Write File data into the network Buffer
Doc. Flush (); // refresh the buffer to write data to the client.
Num = FOS. Read (BUF); // continue to read data from the file
}
FOS. Close ();
Doc. Close ();
SS. Close ();

}
}

2: the client is also the file receiver. Run the command: Java fileclient 127.0.0.1 3107. Make sure that the port is the same as the server port.
Import java. Io .*;
Import java.net .*;
 
Public class fileclient implements runnable {

Private Static string IP = "";
Private Static int ipsocket = 0;

Public static void main (string [] ARGs) throws exception {
IP = ARGs [0];
Ipsocket = integer. parseint (ARGs [1]);
// 3107 is recommended.
System. Out. println ("system address @" + IP + ":" + ipsocket + "");
Thread T1 = new thread (New fileclient ());
T1.start ();
}

Public void run (){
While (true ){
Try {
String strtemp = getfilename ();
GetFile (strtemp );
} Catch (exception e ){}
Try {
Thread. Sleep (5*1000); // 5 seconds
} Catch (exception e ){
System. Out. println (E );
}
}
}

Private Static string getfilename () throws exception {
// Connect to the file server through socket
Socket server = new socket (IP, ipsocket );

// Create a network to accept the stream and accept the Server File data
Inputstream netin = server. getinputstream ();
Inputstream in = new datainputstream (New bufferedinputstream (netin ));

// Create buffer Network Data
Byte [] Buf = new byte [1, 2048];
Int num = in. Read (BUF );

While (num! = (-1) {// whether to read all data
Num = in. Read (BUF); // continue to read files from the network
}
String filename = new string (BUF );
Filename = filename. Trim ();
In. Close ();
Server. Close ();
Return filename;
}

Private Static void GetFile (string strtemp) throws exception {
// Use the local file system to accept the coexistence of network data and the new file
File file = new file (strtemp );

// If the object already exists, delete it first
If (file. exists () file. Delete ();
For (INT I = 0; I <10000; I ++ ){}
File. createnewfile ();
Randomaccessfile RAF = new randomaccessfile (file, "RW ");

// Connect to the file server through socket
Socket server = new socket (IP, (ipsocket + 1 ));

// Create a network to accept the stream and accept the Server File data
Inputstream netin = server. getinputstream ();
Inputstream in = new datainputstream (New bufferedinputstream (netin ));

// Create buffer Network Data
Byte [] Buf = new byte [1, 2048];
Int num = in. Read (BUF );

While (num! = (-1) {// whether to read all data
Raf. Write (BUF, 0, num); // write data to a file
Raf. skipbytes (Num); // write the file bytes in sequence.
Num = in. Read (BUF); // continue to read files from the network
}
In. Close ();
Raf. Close ();
Server. Close ();
}
}

This article is for reference only. If you have any suggestions, please refer to BT.
Luminji@hotmail.com or www.10ms.net.

 

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.