Java Operation Remote shared directory

Source: Internet
Author: User

I. Preface

According to customer feedback, in the file download, add a remote shared directory, download the corresponding file to a remote shared directory, the use of commonly used IO operation mode, prompting the successful download, but the customer went to the remote shared directory to view the corresponding download file, feedback said no corresponding files found. Requires the system needs to support uploading remote shared directory, why is there a requirement? Since the downloaded file involves the payment file, the amount in the content is not allowed to be modified, if it is put on the local path there will be no human modification, generally involving money problems, the customer is more cautious, Just did not contact the operation of remote shared directory, Google a bit to see if there is a corresponding operating instructions, the following simple summary.

Two. Remote shared directory operation

1, need to download the corresponding Jcifs-1.3.18.jar, this example uses 3.18 version, download link: https://jcifs.samba.org/

2, the main class involved is smbfile (remote file Operation Class) , there is a login to verify, verify the corresponding remote directory of the legality of the operation, other operations on the normal IO stream operation.

3. Downloading files from a remote shared directory

/*** Method Description: Download files from remote shared directory *@paramLocaldir Local Temporary path *@paramremovedir Remote Shared path *@param_filename Remote Share file name *@paramREMOVEIP Remote shared directory IP *@paramremoveloginuser Remote shared directory user name *@paramremoveloginpass Remote shared directory password *@return     * @throwsException*/     Public Static intsmbdownload (String localdir, String RemoveDir, String _filename, String Removeip, String removeloginuser, String Removeloginpass)throwsException {InputStream in=NULL; OutputStream out=NULL; Try{ntlmpasswordauthentication auth=Newntlmpasswordauthentication (Removeip, Removeloginuser, Removeloginpass); Smbfile RemoteFile=NewSmbfile (RemoveDir +_filename, auth); if(!remotefile.exists ()) {                return0; } File dir=NewFile (Localdir); if(!dir.exists ())            {Dir.mkdirs (); } String fileName= _filename.substring (_filename.lastindexof ("\ \") +1, _filename.length ()); File LocalFile=NewFile (Localdir +fileName); Inch=NewBufferedinputstream (NewSmbfileinputstream (remotefile)); out=NewBufferedoutputstream (NewFileOutputStream (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 {                if(NULL!=Out )                {Out.close (); }            } Catch(IOException e) {e.printstacktrace (); } finally {                if(NULL!=In ) {                    Try{in.close (); } Catch(IOException e) {e.printstacktrace (); }                }            }        }        return1; }

4, upload files are shared remotely directory

/*** Method Description: Upload file to remote shared directory *@paramLocaldir Local temporary path (a:/test/test. xls) *@paramremovedir Remote shared path (smb://10.169.2.xx/test/, special path can only be used/) *@paramREMOVEIP Remote shared directory IP (10.169.2.xx) *@paramremoveloginuser Remote shared directory username (user) *@paramremoveloginpass Remote shared directory password (password) *@return     * @throwsException 0 Successes/-1 failures*/     Public Static intsmbuploading (String localdir, String RemoveDir, String Removeip, String removeloginuser, String Removelogi Npass)throwsException {ntlmpasswordauthentication auth=NULL; OutputStream out=NULL; intRetVal = 0; Try{File dir=NewFile (Localdir); if(!dir.exists ())            {Dir.mkdirs (); } inetaddress IP=Inetaddress.getbyname (REMOVEIP); Uniaddress Address=Newuniaddress (IP); //Permission ValidationAuth =Newntlmpasswordauthentication (Removeip, Removeloginuser, Removeloginpass);                         Smbsession.logon (Address,auth); //remote path Determines whether file file path is legalSmbfile RemoteFile =NewSmbfile (RemoveDir +dir.getname (), auth);                   Remotefile.connect (); if(Remotefile.isdirectory ()) {RetVal=-1; }                    //writing files to a remote shared directoryout =NewBufferedoutputstream (NewSmbfileoutputstream (remotefile));        Out.write (Tobytearray (dir)); } Catch(unknownhostexception e) {retVal=-1;        E.printstacktrace (); } Catch(malformedurlexception e) {retVal=-1;        E.printstacktrace (); } Catch(smbexception e) {retVal=-1;        E.printstacktrace (); } Catch(IOException e) {retVal=-1;        E.printstacktrace (); } finally{            if(Out! =NULL) {                Try{out.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }                returnRetVal; }        /*** Mapped file Mappedbytebuffer can improve performance when working with large files * *@paramFile Files *@returnbyte array *@throwsIOException IO exception information*/@SuppressWarnings ("Resource")     Public Static byte[] Tobytearray (file file)throwsIOException {FileChannel FC=NULL; Try{FC=NewRandomaccessfile (file, "R"). Getchannel (); Mappedbytebuffer Bytebuffer= Fc.map (mapmode.read_only, 0, Fc.size ()). Load (); byte[] result =New byte[(int) fc.size ()]; if(bytebuffer.remaining () > 0) {Bytebuffer.get (result,0, bytebuffer.remaining ()); }            returnresult; } Catch(IOException e) {e.printstacktrace (); Throwe; } finally {            Try{fc.close (); } Catch(IOException e) {e.printstacktrace (); }        }    }

Java Operation Remote shared directory

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.