Projects often need to access shared folders, such as shared folders storing photos, files, etc. So how to read and write Windows shared folders using Java?
Java can use the JCIFS framework to read and write Windows shared folders. This framework allows us to access remote folders like accessing local folders.
JCIFS's URL: http://jcifs.samba.org/
JCIFS is an open source framework developed using pure Java, accessing remote folders through the SMB protocol. This framework supports both Windows shared folders and Linux shared folders. However, the Linux shared folders require the installation of Samba service software (official website: http://www.samba.org/).
SMB (Server Messages Block) is a communication protocol for sharing files and printers on a local area network. It provides sharing services for files, printers and other resources between different computers in the 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 "NetBIOS over TCP/IP", Samba can not only share resources with local network hosts, but also with computers around the world.
This article mainly learns how to use Java to access shared folders for Windows.
First, find a Windows machine, create a folder: sharedFolder at any location, and set it to share, and set the shared username: share, password: admin.
(How to set a shared folder under Windows 7: How to set a shared folder under Windows 7: //www.VeVB.COM/os/windows/78034.html)
Whether it is a shared folder for Windows or Linux, the code for using Java smb to access shared folders is the same, but the way Windows and Linux configure shared folders is different.
The test code is as follows:
InputStream in = null; OutputStream out = null; try { //Get the image File localFile = new File("C:/testjpg"); String remotePhotoUrl = "smb://share:admin@11/sharedFolder/"; //Shared directory for storing pictures SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_"); SmbFile remoteFile = new SmbFile(remotePhotoUrl + "/" + fmtformat(new Date()) + localFilegetName()); remoteFileconnect(); //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 = inread(buffer, 0, bufferlength)) != -1) { outwrite(buffer, 0, len); } outflush(); //Fresh the buffered output stream} catch (Exception e) { String msg = "Error occurred:" + egetLocalizedMessage(); Systemoutprintln(msg); } finally { try { if(out != null) { outclose(); } if(in != null) { inclose(); } } catch (Exception e) {} }In the above code, the SmbFile class provided by the JCIFS framework is used. This class is similar to the Java File class. Using objects of this class can handle the reading and writing of remote files. Use the File object to read the local file and then use the SmbFile object to write to the remote file. SmbFile's connect() method can try to connect to a remote folder. If the account or password is wrong, a connection exception will be 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(); //Try to connect//Create file stream in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); out = new ByteArrayOutputStream((int)remoteFilelength()); //Read file content byte[] buffer = new byte[4096]; int len = 0; //Read length while ((len = inread(buffer, 0, bufferlength)) != - 1) { outwrite(buffer, 0, len); } outflush(); //Flush 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 all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.