Java Network File Transfer

Source: Internet
Author: User
Tags continue file system flush socket unpack wrapper client
Network Reader scope:

This article is a short introductory article. This article assumes that readers have an understanding of Java IO Systems and Java Network systems.



Body:

The problem with file transfer is also a basic problem with IO reading and writing. It is also an IO read and write problem for the network. Therefore, the so-called network file transfer is actually a comprehensive discussion of two IO problems. Here we first analyze a diagram.






Figure 1:

Analysis Figure 1 We can basically know from the server file system through the flow of data in the file into the server process, the data in the process is then passed through the network IO system to the client, at which point the data in the network is saved as a byte stream. When the byte stream is accepted by the client process, The client process writes to the client's local file system through a client-local file stream.



Based on the above analysis, we can basically determine what I need to deal with the problem. First we need to be able to operate the local file system IO operation interface, and then a network IO system can operate operation interface, has a data can be packaged into a byte stream operation interface, They can provide both client and server two processes for read and write operations. The following illustration shows:





Figure 2:

Based on the above analysis, we can attribute the problem to the requirements of the following programming interfaces:

1. Byte wrapper and Byte unpack,

2. Network Transmitter and network receiver

3. local file read/write device



These Java APIs are already available. They were all packed into two packages, java.io and java.net, where I provided a version of the implementation based on TCP/IP, using a connection based approach to complete the work. Let's start by introducing the classes in several related JDK to do the above tasks,

1. DataOutputStream and DataInputStream implementation classes provide the above byte wrapper and the implementation of the package

2. ServerSocket and Socekt provide a connection-based network transport and acceptance interface

3. File,fileinputstream and FileOutputStream provide a basic local file input and output interface.





Server-side implementation code:

Import java.io.*;

Import java.net.*;



public class fileserver{

public static void Main (string[] args) throws exception{

Create a file stream to read data from a file

File File=new file ("Lishengjie.jpg");

FileInputStream fos=new fileinputstream (file);



Create a network server to accept customer requests

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));



Create a file read buffer

Byte[] Buf=new byte[2048];

int Num=fos.read (BUF);

while (num!= (-1)) {//whether to finish reading the file

Doc.write (buf,0,num)//write the file data to the network buffer

Doc.flush ()//flush buffer writes data to client

Num=fos.read (BUF);/Continue reading data from file

}

Fos.close ();

Doc.close ();

}

}



Client-side Implementation code:

Import java.io.*;

Import java.net.*;



public class fileclient{

public static void Main (string[] args) throws exception{

Use local file system to accept network data coexist as new file

File File=new file ("Newfile.jpg");

File.createnewfile ();

Randomaccessfile raf=new randomaccessfile (file, "RW");



Connecting to a file server via a socket

Socket server=new Socket (inetaddress.getlocalhost (), 3108);



Create a network accept stream to accept server file data

InputStream Netin=server.getinputstream ();

InputStream in=new DataInputStream (New Bufferedinputstream (Netin));



Creating buffer buffering Network data

Byte[] Buf=new byte[2048];

int Num=in.read (BUF);



while (num!= (-1)) {//Whether all data is read

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

Raf.skipbytes (num);//sequential Write file bytes

Num=in.read (BUF);/Continue reading files from the network

}

In.close ();

Raf.close ();

}

}

Due to the above code:

Server
Client

1. The server reads files from the local file system

2. Server Create NETWORK Service connection

3. Server provides data wrapper

4. Server writes local files to data wrapper

5. The server is written to the network through the wrapper
1. Clients create new files to store data from the network

2. Client Connection Server

3. Clients receive server data and unpack data over the network

4. Client writes data to buffer

5. Client writes data from buffer to client local file




Summarize:

In fact, the Java development environment provides us with most programming interfaces, simplifying the development effort for us. The files we provide through the Java IO interface, Data wrapper and other interfaces are very convenient to solve the development workload above us. The sockets provided by the Java NET interface also make it easy to accept and send data based on the connection.





Related Article

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.