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.