Java Remote Access shared directory

Source: Internet
Author: User

1.2 Jcifs

Jcifs <script src= "http://hi.images.csdn.net tutorials/js/blog/tiny_mce/themes/advanced/langs/zh.js" type= "text/ JavaScript tutorial "></script><script src=" http://hi.images.111cn.net/js/blog/tiny_mce/plugins/syntaxhl/ Langs/zh.js "type=" Text/javascript ></script> pan> is a Java-developed SMB client library that uses Jcifs to manipulate Windows shared files and to get domain users, Realize single sign-on, the latest version is: 1.3.12, official website: http://jcifs.samba.org/

2. Code implementation

Package UPLOADSMB;

Import Java.io.BufferedInputStream;

Import Java.io.BufferedOutputStream;

Import Java.io.File;

Import Java.io.FileInputStream;

Import Java.io.FileOutputStream;

Import java.io.IOException;

Import Java.io.InputStream;

Import Java.io.OutputStream;

Import Jcifs.smb.SmbFile;

Import Jcifs.smb.SmbFileInputStream;

Import Jcifs.smb.SmbFileOutputStream;

public class Uploaddownloadutil {

/**

* Description: Copy files from shared directory to local

* @Version1.0 Sep, 2009 3:48:38 PM

* @param the file path on the Remoteurl shared directory

* @param localdir Local Directory

*/

public void Smbget (String remoteurl,string localdir) {

InputStream in = null;

OutputStream out = null;

try {

Smbfile remotefile = new Smbfile (Remoteurl);

if (remotefile==null) {

SYSTEM.OUT.PRINTLN ("Shared file does not exist");

Return

}

String fileName = Remotefile.getname ();

File LocalFile = new file (localdir+file.separator+filename);

in = new Bufferedinputstream (new Smbfileinputstream (RemoteFile));

out = new Bufferedoutputstream (new FileOutputStream (LocalFile));

byte[] buffer = new byte[1024];

while (in.read (buffer)!=-1) {

Out.write (buffer);

Buffer = new byte[1024];

}
  
catch (Exception e) {

E.printstacktrace ();

finally {

try {

Out.close ();

In.close ();

catch (IOException e) {

E.printstacktrace ();

}

}

}

/**

* Description: Uploading files from local to shared directory

* @Version1.0 Sep, 2009 3:49:00 PM

* @param remoteurl Shared Files directory

* @param localfilepath Local File absolute path

*/

public void Smbput (String remoteurl,string localfilepath) {

InputStream in = null;

OutputStream out = null;

try {

File LocalFile = new file (Localfilepath);

String fileName = Localfile.getname ();

Smbfile remotefile = new Smbfile (remoteurl+ "/" +filename);

in = new Bufferedinputstream (new FileInputStream (LocalFile));

out = new Bufferedoutputstream (new Smbfileoutputstream (RemoteFile));

byte[] buffer = new byte[1024];

while (in.read (buffer)!=-1) {

Out.write (buffer);

Buffer = new byte[1024];

}

catch (Exception e) {

E.printstacktrace ();

finally {

try {

Out.close ();

In.close ();

catch (IOException e) {

E.printstacktrace ();

}

}

}

public static void Main (string[] args) {

Uploaddownloadutil test = new Uploaddownloadutil ();

SMB: Domain name: password @ destination ip/folder/filename. xxx

Test.smbget ("Smb://szpcg;jiang.t:xxx@192.168.193.13/jake/test.txt", "c://");

Test.smbput ("Smb://szpcg;jiang.t:xxx@192.168.193.13/jake", "c://test.txt");

}

}

2.3 Remoteurl Description

Remoteurl How to fill in is worth noting

In the case of a shared password-free share, the following format is similar:

Smb://ip/sharefolder (for example: Smb://192.168.0.77/test)

If a username or password is required, it is similar to the following format:

Smb://username:password@ip/sharefolder (for example: Smb://chb:123456@192.168.0.1/test)

SMB: Domain name: password @ destination ip/folder/filename. xxx

Test.smbget ("Smb://szpcg;jiang.t:xxx@192.168.193.13/jake/test.txt", "c://");

Test.smbput ("Smb://szpcg;jiang.t:xxx@192.168.193.13/jake", "c://test.txt");

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.