Java network file transfer

xiaoxiao2021-03-05  19

Regarding the issue of document transmission, it is also the basic issue of IO reading and writing. For the network, it is also an IO read-write problem. Therefore, the so-called network file transmission is actually a comprehensive discussion of two IO issues. Here we first analyze one Diagram. Then discuss this illustration:

figure 1:

Analysis Figure 1 We basically know that from the server file system writes to the server in the stream file, and then pass the data in the process through the network IO system to the client, this phase, the data in the network The word stream is saved. When the word stream is accepted by the customer process, the customer process writes the local file system through the customer's local file stream.

According to the above analysis, we basically determine the problem I need to handle. First we need an operation interface that can be operated on the local file system IO, then an operation interface that can operate on the network IO system, which has been packaged The operation interface of the threads, they can provide two processes for both clients and servers. As shown below:

figure 2:

According to the above analysis, we can break the problem to the needs of the following programming interfaces:

1. Byte wrapper and byte uncers,

2. Network Transmitter and Network Receiver

3. Local file read / writer

These Java itself have already provided. They are all packaged in two packages in java.io and java.net, here I offer a TCP / IP implementation version, using connection-based way to complete work. We First introduce a few related JDK classes to complete the above tasks.

1. DataOutputStream and DataInputStream implementation classes provide the above-mentioned byte packaging and dispenser implementation

2. ServerSocket and Socekt provide connection-based network transfer and accept interface

3. File, FileInputStream and FileOutputStream provide basic local file input and output interfaces.

Server-side implementation code:

Import java.io. *;

Import java.net. *;

Public class fileserver {

Public static void main (string [] args) throws exception {

// Create a file flow to read the data in the file

File File = New File ("Lishengjie.jpg");

FileInputStream Fos = New FileInputStream (file);

// Create a web server to accept customer request

Serversocket SS = New ServerSocket (3108);

Socket client = ss.accept ();

// Create a network output stream and provide a data wrapper

OutputStream Netout = Client.getOutputStream ();

OutputStream Doc = New DataOutputStream (New BufferedoutputStream (Netout);)); NETOTPUTSTREAM (Netout);

// Create a file read buffer

BYTE [] BUF = New byte [2048];

INT NUM = Fos.read (BUF);

While (Num! = (- 1)) {// Do you read the file?

Doc.write (buf, 0, num); // Write file data from 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 ();

}

}

Customer party implementation code:

Import java.io. *;

Import java.net. *;

Public class fileclient {

Public static void main (string [] args) throws exception {

// Accept the network data using the local file system and store new files

File File = New File ("newfile.jpg");

File.createNewFile ();

RandomaccessFile Raf = new randomaccessfile (file, "rw"); // Connect to the file server via the Socket

Socket Server = New Socket (inetaddress.getlocalhost (), 3108);

// Create a network accept stream accepted 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 you read all data

Raf.write (buf, 0, num); // writing data to file

Raf.skipbytes (NUM); // write file byte

Num = in.read (buf); / / Continue to read files from the network

}

In.Close ();

Raf.close ();

}

}

Attributed the above code:

Server client

1. Server reads files from local file systems

2. Server Create a network service connection

3. Server provides data wrapper

4. Servers write local files to the data wrapper

5. The server is written to the network through the wrapper 1. The client creates a new file to prepare data from the network.

2. Client connection server

3. The client accepts server data over the network and performs data unpacking

4. Clients write data into buffers

5. The client writes the data from the buffer to the customer local file

to sum up:

In fact, the Java's development environment provides us with most programming interfaces, which simplifies the development workload. We use the documents provided by Java IO interfaces, the interfaces such as data wrappers are very convenient to solve our development workload. The socket provided at the same time in Java's NET interface also makes connection-based data acceptance and transmission becomes very easy.

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

New Post(0)