Java reads and writes method instances of Windows Shared folders

  • 2020-05-17 05:39:27
  • OfStack

Projects often need to have access to Shared folders, such as Shared folders for storing photos, files, and so on. So how do you read and write Windows Shared folders using Java?

Java can read and write to Windows Shared folders using the JCIFS framework, which allows us to access remote folders like local folder 1.

JCIFS url: http: / / jcifs samba. org /

JCIFS is an open source framework developed using pure Java, which accesses remote folders through the smb protocol. The framework supports both Windows Shared folder and Linux Shared folder, however, Linux Shared folder software need to install Samba service (website: http: / / www. samba. org /).

SMB (Server Messages Block) is a communication protocol for sharing files and printers on a local area network (LAN). It provides the sharing of files, printers and other resources between different computers on a LAN. The SMB protocol is a client/server type protocol through which clients can access Shared file systems, printers, and other resources on the server. By setting "NetBIOS over TCP/IP", Samba can share resources not only with LAN hosts, but also with computers around the world.

This article focuses on how to use Java to access Windows Shared folders.

First, find an Windows machine, create a folder anywhere: sharedFolder, and set it to share. Set the Shared user name: share, and password: admin.

Method of setting the Shared folder (Windows7: Windows7 setting method of the Shared folder: / / www ofstack. com/os/windows / 78034. html)

Whether it is the Shared folder of Windows or Linux, the code to access the Shared folder using Java smb is the same, except that Windows and Linux do not configure the Shared folder in the same way.

The test code is as follows:


InputStream in = null; 
OutputStream out = null; 
try { 
  // Get photo  
  File localFile = new File("C:/testjpg"); 
  String remotePhotoUrl = "smb://share:admin@11/sharedFolder/"; // A Shared directory for storing images  
  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 the length  
  while ((len = inread(buffer, 0, bufferlength)) != -1) { 
    outwrite(buffer, 0, len); 
  } 
  outflush(); // Refreshes the buffered output stream  
} 
catch (Exception e) { 
  String msg = " Error: " + 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, which is similar to the File class of Java. Objects of this class can be used to handle reading and writing of remote files. The local file is read using the File object, and the remote file is written using the SmbFile object. The connect() method of SmbFile can attempt to connect to a remote folder and throw a connection exception if the account or password is wrong.

When downloading the remote file, the SmbFile object can be used to read the remote file. The code is as follows:


InputStream in = null ; 
ByteArrayOutputStream out = null ; 
try { 
  // Create a remote file object  
  String remotePhotoUrl = "smb://share:admin@11/sharedFolder/testjpg"; 
  SmbFile remoteFile = new SmbFile(remotePhotoUrl); 
  remoteFileconnect(); // Try to connect  
  // Creating a file stream  
  in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); 
  out = new ByteArrayOutputStream((int)remoteFilelength()); 
  // Read file contents  
  byte[] buffer = new byte[4096]; 
  int len = 0; // Read the length  
  while ((len = inread(buffer, 0, bufferlength)) != - 1) { 
    outwrite(buffer, 0, len); 
  } 
 
  outflush(); // Refreshes 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) {} 
} 

Related articles: