Projects often need to have access to shared folders, such as shared folders to store photos, files, and so on. So how do you use Java to read and write Windows shared folders?
Java can read and write Windows shared folders using the JCIFS framework, which allows us to access remote folders just as we would access a local folder.
Jcifs's Web site: http://jcifs.samba.org/
Jcifs is an open source framework that uses pure Java development to access remote folders through the SMB protocol. The framework supports both Windows shared Folders and Linux shared folders, although the Linux shared folder requires the installation of the Samba Service software (official website: http://www.samba.org/).
SMB (Server Messages block, information service blocks) is a communication protocol that shares files and printers on a local area network, providing shared services for resources such as files and printers between different computers in a local area network. The SMB protocol is a client/server protocol through which clients can access shared file systems, printers, and other resources on the server. By setting up "NetBIOS over TCP/IP", samba can not only share resources with local network hosts, but also share resources with computers around the world.
This article is mainly about how to access Windows shared Folders using Java.
First find a Windows machine, create a folder in any location: SharedFolder, and set to share, set the shared user name: Share, Password: admin.
(Windows7 to set up Shared Folders method: Windows7 to set the shared folder method: Http://www.jb51.net/os/windows/78034.html)
The same code that uses Java SMB to access shared folders is the same for Windows and Linux shared folders, except that Windows is configured to share folders differently from Linux.
The test code is as follows:
InputStream in = null;
OutputStream out = null;
try {//get picture file LocalFile = new file ("C:/testjpg"); String Remotephotourl = "smb://share:admin@11/sharedfolder/";
The shared directory that holds the picture SimpleDateFormat FMT = new SimpleDateFormat ("Yyyymmddhhmmsssss_");
Smbfile remotefile = new Smbfile (Remotephotourl + "/" + Fmtformat (new Date ()) + localfilegetname ()); Remotefileconnect ();
Attempt to connect in = new Bufferedinputstream (new FileInputStream (LocalFile));
out = new Bufferedoutputstream (new Smbfileoutputstream (RemoteFile));
byte[] buffer = new byte[4096]; int len = 0;
Read length while (len = inread (buffer, 0, bufferlength))!=-1) {outwrite (buffer, 0, Len); } outflush ();
Refresh buffered output stream} catch (Exception e) {String msg = "Error occurred:" + egetlocalizedmessage ();
SYSTEMOUTPRINTLN (msg);
Finally {try {if (out!= null) {outclose ();
} if (in!= null) {inclose (); The catch (Exception e) {}}
The above code uses the Smbfile class provided by the JCIFS framework, which is similar to Java's file class, and uses objects of this class to handle the reading and writing of remote files. Use the file object to read the local file, and then write the remote file using the Smbfile object. The Smbfile Connect () method can attempt to connect to a remote folder, and if the account or password is incorrect, the connection exception is thrown.
When downloading a remote file, use the Smbfile object to read the remote file, the code is as follows:
InputStream in = null;
Bytearrayoutputstream out = null;
try {
//Create remote File object
String remotephotourl = "smb://share:admin@11/sharedfolder/testjpg";
Smbfile remotefile = new Smbfile (remotephotourl);
Remotefileconnect (); Attempt to connect
//create file stream in
= new Bufferedinputstream (new Smbfileinputstream (RemoteFile));
out = new Bytearrayoutputstream ((int) remotefilelength ());
Read the contents of the file
byte[] buffer = new byte[4096];
int len = 0; Read length
while (len = inread (buffer, 0, bufferlength))!=-1) {
outwrite (buffer, 0, Len);
}
Outflush (); Flushes the buffered output stream return
Outtobytearray ();
}
catch (Exception e) {
String msg = "Error downloading remote file:" + egetlocalizedmessage ();
SYSTEMOUTPRINTLN (msg);
Finally {
try {
if (out!= null) {
outclose ();
}
if (in!= null) {
inclose ();
}
}
catch (Exception e) {}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.