Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Projects often require access to shared folders, such as shared folders that store photos, files, and so on. So how do you read and write Windows shared folders using Java?
Java can use the JCIFS framework to read and write Windows shared Folders, which allows us to access remote folders like a local folder.
Jcifs's website: http://jcifs.samba.org/
Jcifs is an open-source framework developed using pure Java to access remote folders through the SMB protocol. The framework supports both Windows shared Folders and Linux shared folders, but the Linux shared folder requires the installation of Samba Services software (official website: http://www.samba.org/).
SMB (Server Messages block, information service blocks) is a kind of communication protocol for sharing files and printers on LAN, which provides shared services of files and printers among different computers in LAN. The SMB protocol is a client/server protocol that allows clients to access shared file systems, printers, and other resources on the server. By setting up "NetBIOS over TCP/IP", Samba will not only share resources with local network hosts, but also share resources with computers around the world.
This article focuses on 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 Settings shared folder method: HTTP://HI.BAIDU.COM/51_XUEXI/ITEM/5A90A20CD732A8CE75CD3C6D)
The code for accessing shared folders using Java SMB is the same regardless of the shared folder for Windows or Linux, except that Windows has a different way of configuring shared folders with Linux.
The test code is as follows:
[Java]View Plaincopy print?
- InputStream in = null;
- OutputStream out = null;
- try {
- //Get Pictures
- File LocalFile = new File ("c:/test.jpg");
- String Remotephotourl = "Smb://share:[email protected]/sharedfolder/";//shared directory where the picture is stored
- SimpleDateFormat FMT = new SimpleDateFormat ("Yyyymmddhhmmsssss_");
- Smbfile remotefile = new Smbfile (Remotephotourl + "/" + Fmt.format (new Date ()) + localfile.getname ());
- Remotefile.connect (); //try 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 = in.read (buffer, 0, buffer.length))! =-1) {
- Out.write (buffer, 0, Len);
- }
- Out.flush (); //Refresh buffered output stream
- }
- catch (Exception e) {
- String msg = "error occurred:" + e.getlocalizedmessage ();
- SYSTEM.OUT.PRINTLN (msg);
- }
- finally {
- try {
- if (out! = null) {
- Out.close ();
- }
- if (in = null) {
- In.close ();
- }
- }
- catch (Exception e) {}
- }
InputStream in = null; OutputStream out = null; try {//get picture file LocalFile = new file ("C:/test.jpg"); String Remotephotourl = "smb://share:[email protected]/sharedfolder/"; The shared directory where the images are stored simpledateformat FMT = new SimpleDateFormat ("Yyyymmddhhmmsssss_"); Smbfile remotefile = new Smbfile (Remotephotourl + "/" + Fmt.format (new Date ()) + localfile.getname ()); Remotefile.connect (); Try 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 = in.read (buffer, 0, buffer.length))! =-1) {out.write (buffer, 0, Len); } out.flush (); Flush buffered output stream} catch (Exception e) {String msg = "Error occurred:" + e.getlocalizedmessage (); SYSTEM.OUT.PRINTLN (msg); } finally {try {if (out! = null) {out.close (); } if (in = null) {in.close (); }} catch (Exception e) {}}
The above code uses the Smbfile class provided by the JCIFS framework, which is similar to the Java file class, and uses this class object to handle the read and write of remote files. Use the file object to read the local file, and then use the Smbfile object to write to the remote file. The Smbfile Connect () method tries to connect to the remote folder and throws a connection exception if the account or password is incorrect.
When downloading remote files, use the Smbfile object to read remote files, the code is as follows:
[Java]View Plaincopy print?
- InputStream in = null;
- Bytearrayoutputstream out = null;
- try {
- //Create remote file Object
- String Remotephotourl = "Smb://share:[email protected]/sharedfolder/test.jpg";
- Smbfile remotefile = new Smbfile (Remotephotourl);
- Remotefile.connect (); //try to connect
- //Create a file stream
- in = new Bufferedinputstream (new Smbfileinputstream (RemoteFile));
- out = new Bytearrayoutputstream ((int) remotefile.length ());
- //Read file contents
- byte[] buffer = new byte[4096];
- int len = 0; //Read length
- While (len = in.read (buffer, 0, buffer.length))! =- 1) {
- Out.write (buffer, 0, Len);
- }
- Out.flush (); //Refresh buffered output stream
- return Out.tobytearray ();
- }
- catch (Exception e) {
- String msg = "Error downloading remote file:" + e.getlocalizedmessage ();
- SYSTEM.OUT.PRINTLN (msg);
- }
- finally {
- try {
- if (out! = null) {
- Out.close ();
- }
- if (in = null) {
- In.close ();
- }
- }
- catch (Exception e) {}
- }
InputStream in = null; Bytearrayoutputstream out = null; try {//Create remote file object String remotephotourl = "Smb://share:[email protected]/sharedfolder/test.jpg"; Smbfile remotefile = new Smbfile (Remotephotourl); Remotefile.connect (); Attempt to connect//create file stream in = new Bufferedinputstream (new Smbfileinputstream (RemoteFile)); out = new Bytearrayoutputstream ((int) remotefile.length ()); Read file contents byte[] buffer = new byte[4096]; int len = 0; Read length while (len = in.read (buffer, 0, buffer.length))! =-1) {out.write (buffer, 0, Len); } out.flush (); Refreshes the buffered output stream return Out.tobytearray (); } catch (Exception e) {String msg = "Error downloading remote file:" + e.getlocalizedmessage (); SYSTEM.OUT.PRINTLN (msg); } finally {try {if (out! = null) {out.clOSE (); } if (in = null) {in.close (); }} catch (Exception e) {}}
Java reads and writes Windows shared folders.