In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use SMB to deal with shared files in remote servers in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction
We are used to using shared files, where sharing is limited to users in certain domains or certain users. Remote applications need to access shared resources and perform operations such as adding and deleting resources at the shared location.
The server has shared resources that typically use the Server message Block (SMB) Protocol (Server Message Block) in the Windows machine. The SMB protocol enables applications to access files on remote servers and other resources that allow client applications to open, read, move, and update files on remote servers. This resource is limited to certain users. Users need to pass credentials to access shared resources. The activities that users can perform on shared resources are defined in shared permissions. Let's assume that the user has read and write permissions.
In this article, I'll show you an example where an application needs to use a simple Java application to interact with shared files on a Windows server computer. The use case makes the following assumptions:
The server is a Windows machine.
There is a local user named "Test" whose password is "Password".
The shared location is "127.0.0.0.1\ temp".
The user "Test" has access to the shared location "temp", which can be located anywhere on a computer with an IP of "127.0.0.1".
Client applications can access the network.
Code base
The application we are designing is to build and manage dependencies using Maven in Java. I am using JCIFS, an open source client library that implements the CIFS/SMB network protocol. The library is available from Maven Repository.
Dependencies are shown in the following pom.xml files:
4.0.0 fileShare fileShare 1.0-SNAPSHOT org.apache.maven.plugins maven-compiler-plugin 7 7 jcifs jcifs 1.3.17
Currently, the application has a file, Main.java, that reads as follows:
Import jcifs.smb.NtlmPasswordAuthentication;import jcifs.smb.SmbException;import jcifs.smb.SmbFile;import java.net.MalformedURLException;public class Main {public static void main (String [] args) {String url = "smb://127.0.0.1/test/"; String userName = "test"; String password = "password"; String domain = null; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication (null, userName, password) Try {doRecursiveLookup (new SmbFile (url, auth));} catch (MalformedURLException e) {e.printStackTrace () }} / * * Recursively scans through the folder for files and prints the name of folder and file * / public static void doRecursiveLookup (SmbFile smb) {try {if (smb.isDirectory ()) {System.out.println (smb.getName ()) For (SmbFile f: smb.listFiles ()) {if (f.isDirectory ()) {doRecursiveLookup (f);} else {System.out.println ("\ t:" + f.getName ()) } else {System.out.println ("\ t:" + smb.getName ());}} catch (SmbException e) {e.printStackTrace ();}
Here, the main method uses the url mode of the for smb protocol and points to the shared location on line 8. The local variables for userName,password and domains are initialized on lines 9 to 11, and the user is authenticated on line 13, when the application has successfully connected to the shared folder.
The application SmbFile creates an object on line 15 and passes the folder to doRecursiveLookup (..) The method called on line 24. This method simply iterates through all the files and folders in the shared location and prints the name recursively in the console. Note that the objects of the SmbFile file or folder are the same, and the .isDirectory () method is used to test whether the objects in scope are files or folders.
This is the end of the content of "how to use SMB in Java to deal with shared files on remote servers". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.