LAN file transfer and replication, including file name

xiaoxiao2021-03-06  42

LAN file transfer and replication, including file name

Implement a local area file replication, including the need to copy the file name. The feature is that the file name transfer and file transfer have two ports. The process completes this function: every 24 hours

On the line, the file is maintained in the root directory of the two machines.

1: The server side, also penetrates the end. Run command: Java File Server 3107Import 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]); // recommended 3107 System.out.println ( " The system is in port: " ipsocket " Wait to send "); 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

FileStemp = dir.list (); java.util.arrays.Sort (fileStemp); for (int i = 0; i

} / * Send file name * / private static void sendFileName (String fileNames) throws Exception {if (fileNames == null) return; // Create The Web server accepts a client request ServerSocket ss = new ServerSocket (ipsocket); Socket client = ss. Accept (); // Create a network output stream and provide data wrapper 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) {// Do not read file Doc.Write (buf, 0, num); // Putting files Data writing network buffer DOC.FLUSH (); // Refresh buffer to write data to client} Doc.close (); ss.close ();} / * send file itself * / private static void sendfile (String FILENAME) THROWS Exception {if (filename == null) Return; // Create file flow to read files Data file file = new file (fileiname); fileinputstream fos = new fileInputStream (file); // Create a web server Accept customer request serversocket ss = new Serversocket (IPSocket 1); socket client = ss.accept (); / / create a network and provides data output stream wrapper OutputStream netOut = client.getOutputStream (); OutputStream doc = new DataOutputStream (new BufferedOutputStream (netOut)); // Create a file read buffer byte [] buf = new byte [2048] INT NUM = fos.read (buf); while (Num! = (- 1)) {// Do not read file Doc.write (buf, 0, num);

// Write the file data from the network buffer doc.flush (); // refresh the buffer to write the data to the client Num = fos.read (buf); // Continue to read data from the file} fos.close ); Doc.close (); ss.close ();}} 2: Client, also a file acceptable end. Run command: java fileclient 127.0.0.1 3107. Note that the server port should be consistent. 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]); // Recommended 3107 system.out.println ("System at address @" ip ":" ipSocket "listening"); Thread T1 = New Thread (New FileClient ()); T1.Start ();} public void Run () {while {Try {string straTemp = getFileName (); getfile (strtemp);} catch (exception e) {} Try {thread.sleep (5 * 1000); // 5 second} catch (exception e) {system.out.println (e);}}} private static string getFileName () throws exception {// Connect to the Socket Connection file server Socket Server = New Socket (IP, IPSocke t);

// Create a network accept stream accept server file data inputStream Netin = Server.getinputStream (); InputStream in = New DataInputStream (New BufferedInputStream (NetIn));

// Create buffer buffer network data BYTE [] buf = new byte [2048]; int num = in.read (buf); while (num! = (- 1)) {// Do not 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 straTemp) throws exception {// Accept network data using local file systems and store file file = new file (strTemp); // If the file already exists, first delete the IF (file.exists ()) File .delete (); for (int i = 0; i <1000; i ) {} file.createNewFile (); randomaccessfile raf = new randomaccessfile (file, "rw"); // Connect to Socket Server Socket Server = New Socket (IP, (IPSocket 1);

// Create a network accept stream accept server file data inputStream Netin = Server.getinputStream (); InputStream in = New DataInputStream (New BufferedInputStream (NetIn));

// Create buffer buffer network data Byte [] buf = new byte [2048]; int num = in.read (buf); while (num! = (- 1)) {// Do not read all data Raf.write (BUF, 0, NUM); // Write the data to file Raf.skipbytes (NUM); // Sequential write file byte num = in.read (buf); // Continue to read files from the network} in. Close (); raf.close (); server.close ();}}

About this article: Pure reference, pleasant Btluminji@hotmail.com or www.10ms.net

转载请注明原文地址:https://www.9cbs.com/read-68013.html

New Post(0)