In the same LAN, use Java to copy files from the server shared folder to local.

Source: Internet
Author: User

1 referencing Jar packages
<Dependency>    <groupId>Org.samba.jcifs</groupId>    <Artifactid>Jcifs</Artifactid>    <version>1.3.14-kohsuke-1</version></Dependency>
2 uploading files from local to server shared folders
ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJcifs.smb.SmbFile;ImportJcifs.smb.SmbFileOutputStream;/*** * <b> class name:</b>copyfiletolan<br/> * <b> class description:</b> local file written to LAN shared folder <br/> * <B&G t; version:</b>v1.0<br/>*/ Public classCopyfiletolan { Public Static voidMain (string[] args) {InputStream in=NULL; OutputStream out=NULL; Try {            //test FileFile LocalFile =NewFile ("d:\\ document \\test.txt"); String Host= "192.168.1.151";//the address of the remote serverString username = "admin";//User nameString Password = "admin";//PasswordString Path = "/share/";//Remote server shared folder nameString Remoteurl= "smb://" + Username + ":" + password + "@" + Host + path + (Path.endswith ("/")? "": "/"); Smbfile RemoteFile=NewSmbfile (Remoteurl + "/" +localfile.getname ());            Remotefile.connect (); Inch=NewBufferedinputstream (NewFileInputStream (LocalFile)); out=NewBufferedoutputstream (NewSmbfileoutputstream (remotefile)); byte[] buffer =New byte[4096]; intLen = 0;  while(len = in.read (buffer, 0, buffer.length))! =-1) {out.write (buffer,0, Len);        } out.flush (); }        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) {} }}}
3 downloading files from a server shared folder to a local
ImportJcifs.smb.SmbFile;ImportJcifs.smb.SmbFileInputStream;ImportJava.io.*;/*** <b> class name:</b>copylanfiletolocal<br/> * <b> class description:</b> read LAN shared folder file to local folder <br/ >*/ Public classcopylanfiletolocal { Public Static voidMain (string[] args) {InputStream in=NULL; OutputStream out=NULL; Try {            //Target file nameString fileName = "1.jpg"; //Local FileString LocalPath = "d:/"; String Host= "192.168.1.151";//the address of the remote serverString username = "admin";//User nameString Password = "admin";//PasswordString Path = "/share/";//Remote server shared folder nameString Remoteurl= "smb://" + Username + ":" + password + "@" + Host + path + (Path.endswith ("/")? "": "/"); //To create a remote file objectSmbfile RemoteFile =NewSmbfile (Remoteurl + "/" +fileName);            Remotefile.connect (); //Create a file streamin =NewBufferedinputstream (NewSmbfileinputstream (remotefile)); out=NewBufferedoutputstream (NewFileOutputStream (NewFile (LocalPath +fileName)); //Read File contents            byte[] buffer =New byte[4096]; intLen = 0;  while(len = in.read (buffer, 0, buffer.length))! =-1) {out.write (buffer,0, Len);        } out.flush (); } 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) {} }}}

Smbfile and file operation of the files are basically consistent, when the file is copied, I want to use the Commons-io fileutils.copyfile (final file srcfile, final file destfile); and Java 1.7 Files.copy (path source, path target, copyoption ... options), but because Smbfile and File do not belong to a type, resulting in a failure, you can only choose how to use the stream.

In the same LAN, use Java to copy files from the server shared folder to local.

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.