Access shared file system with Java

xiaoxiao2021-03-06  72

Access shared file system with Java

Author: Xue Guyu

Foreword

In the Microsoft Network System, the SMB (Server Message Block, Service Message Block) protocol is Windows for Workgroup (WFWG), Windows 95, Windows NT, and LanManager for implementing a shared local domain file and printer. For a local domain network constructed by Linux and Windows NT, Samba is a software package for the SMB household sequence / server program provided for Linux, which is a magnetic disk space and printer that realizes Windows and Linux. General Network File System is referred to as CIFS, which is actually a standard protocol shared by Windows platform files, which is the bottom level implementation protocol for Windows Explorer, network neighbor, and mapping network drives. Java has natural platform innocence, using Java access to any type of server or shared file system on the client, and the written software products can run in any platform, so the Java access shared file system has a unique advantage in enterprise applications. .

CIFS implementation in Java

JCIFS is an implementation of CIFS in Java. It is a Samba organization in the spirit of Linux and is responsible for maintenance of development. This project focuses on the design and implementation of the CIFS protocol using the Java language. They designed JCIFS into a complete, rich, scalable and thread-safe client library. This library can be applied to a variety of Java virtual machine access to comply with the network resources of the CIFS / SMB network transfer protocol. Similar to the interface of java.io.file, it is proven to be effective and easy to use in multi-threaded mode of operation. At present, the latest version of JCIFS is JCIFS-0.8.0B, you can get the URL for downloading this category in the last list of references. This version also supports distributed file systems. This article does not involve this content. .

Java / CIFS program design

If you have used the Java file operation function, you will easily master the development method of JCIFS. JCIFS positions a resource with a resource URL similar to the following:

SMB: // Guest: 1234@192.168.3.56/share/a.txt

The beginning of this URL SMB: // illustrates this is an SMB type URL; the next GUEST and 1234 are user names and passwords for accessing the shared resource; @ is the host name or IP address of the resource to be accessed. Finally, the resource shared folder name and shared resource name.

In the Java program, use the handle of a remote shared file using the following:

SMBFILE FILE = New SMBFile ("SMB: // Guest: 1234@192.168.3.56/share/a.txt");

The handle here is not limited to remote sharing files, but also a shared folder. The isFile () method and isDirectory () are used to determine the true properties of the resource corresponding to the handle. If it is a shared folder, a list of resources will be obtained by calling its list () method. The List method supports the filter mechanism, there are two filters available, one is the SMBFileFilter, the other is SMBFileNameFilter, which appears as an interface in JCIFS, you can derive personalized filters according to your own needs To realize the Accept method in the interface to meet the needs of different services.

SMBFileInputStream is the input stream of the SMB file. Its function is to open a SMBFile in a stream:

SMBFILINPUTSTREAM IN = New SMBFILINPUTSTREAM (file); SMBFileInputStream provides an read method, you can read all the contents of the remote file from this stream.

At this point, you have mastered the basic method of Java accessing the SMB file system.

SMBFILEOUTPUTSTREAM is the input stream of the SMB file, and the remote file system can be written to the data through the SMB, and the method is similar to the above, and details will not be described again.

The source code included in this article is a complete example, which is simple to complete, which is to copy some of the files under another computer to the specified directory of the machine. Through this example, I believe that you easily transplant it into your development product.

The first step is to set the operating environment. Decompress the source code, add the name of the name of the name of the lib directory to the ClassPath of your operating system, so that this library can be found when the sample program is run.

In the second step, establish a shared folder named Share on another computer, the user name is GUEST, the password is also guest. Suppose this computer's IP address is 192.168.0.2.

In the third step, put two files under the shared folder just established, named hello.txt, bye.txt. The content of the file does not matter.

In the third step, establish an empty directory named TMP in the C drive on the local computer, which can be written in this directory.

In the fourth step, compile SAMPLE1.JAVA and run:

Java Sample1 192.168.3.52 Guest Guest / Share / C: / TMP /

Check the C: / TMP folder, you will find that hello.txt is copied to the unit, and Bye.txt is not copied, because the filter is used in Sample1.java, only the file that is named hello is copied.

postscript

Access to the shared file system is an unavoidable problem in the development of software products such as file migration, JCIFS is a complete set of solutions for this topic, through which you can implement any shared files. The accesses of the system.

Some useful resources and reference materials

All source code of this article, compile, for readers, download source code.

For more information on JCIFS, please visit its official website http://jcifs.samba.org/.

You can get the latest version of the JCIFS library at http://users.erols.com/mpallen/jcifs/.

If you are not very familiar with SMB, here there is a good article to introduce you what is SMB: http://samba.rantu.edu.au/cifs/docs/what-is-smb.html.

About author

Xue Guyu is Nordsan (Beijing) Information Technology Development Co. 126.com got contact with him.

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

New Post(0)