Java read-write method instance for Windows shared folder _java

Source: Internet
Author: User
Tags mongodb postgresql


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.


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.