Java远程共享目录的操作代码
一.前言
根据客户反馈,在进行文件下载的时候,新增远程共享目录,下载对应的文件到远程共享目录,采用常用的IO操作模式,提示下载成功,但是客户去远程共享目录查看对应的下载文件,反馈说没有找到对应的文件。要求系统需要支持上传远程共享目录,为什么有一个这样的需求?由于下载的文件涉及到了支付文件,里面的金额不允许进行修改,如果放在本地路径有可能会不会出现人为的修改,一般涉及到钱的问题,客户都是比较谨慎的,刚好没有接触过操作远程共享目录的,就google了一下看有没有对应的操作说明,下面简单总结一下。
二.远程共享目录操作
1、需要下载对应的jcifs-1.3.18.jar,本例子采用3.18版本的,下载链接:https://jcifs.samba.org/
2、涉及的主要类是 SmbFile(远程文件操作类),还有就是进行登录验证,验证对应的远程目录的合法性的操作,其他操作就普通的IO流的操作。
3、从远程共享目录下载文件
/** *方法说明:从远程共享目录下载文件 *@paramlocalDir本地临时路径 *@paramremoveDir远程共享路径 *@param_fileName远程共享文件名 *@paramremoveIp远程共享目录IP *@paramremoveLoginUser远程共享目录用户名 *@paramremoveLoginPass远程共享目录密码 *@return *@throwsException */ publicstaticintsmbDownload(StringlocalDir,StringremoveDir, String_fileName,StringremoveIp,StringremoveLoginUser, StringremoveLoginPass)throwsException{ InputStreamin=null; OutputStreamout=null; try{ NtlmPasswordAuthenticationauth=newNtlmPasswordAuthentication( removeIp,removeLoginUser,removeLoginPass); SmbFileremoteFile=newSmbFile(removeDir+_fileName,auth); if(!remoteFile.exists()){ return0; } Filedir=newFile(localDir); if(!dir.exists()){ dir.mkdirs(); } StringfileName=_fileName.substring(_fileName.lastIndexOf("\\")+1,_fileName.length()); FilelocalFile=newFile(localDir+fileName); in=newBufferedInputStream(newSmbFileInputStream(remoteFile)); out=newBufferedOutputStream(newFileOutputStream(localFile)); byte[]buffer=newbyte[1024]; while(in.read(buffer)!=-1){ out.write(buffer); buffer=newbyte[1024]; } }catch(Exceptione){ e.printStackTrace(); }finally{ try{ if(null!=out){ out.close(); } }catch(IOExceptione){ e.printStackTrace(); }finally{ if(null!=in){ try{ in.close(); }catch(IOExceptione){ e.printStackTrace(); } } } } return1; }
4、上传文件都远程共享目录
/** *方法说明:上传文件到远程共享目录 *@paramlocalDir本地临时路径(A:/测试/测试.xls) *@paramremoveDir远程共享路径(smb://10.169.2.xx/测试/,特殊路径只能用/) *@paramremoveIp远程共享目录IP(10.169.2.xx) *@paramremoveLoginUser远程共享目录用户名(user) *@paramremoveLoginPass远程共享目录密码(password) *@return *@throwsException0成功/-1失败 */ publicstaticintsmbUploading(StringlocalDir,StringremoveDir, StringremoveIp,StringremoveLoginUser,StringremoveLoginPass)throwsException{ NtlmPasswordAuthenticationauth=null; OutputStreamout=null; intretVal=0; try{ Filedir=newFile(localDir); if(!dir.exists()){ dir.mkdirs(); } InetAddressip=InetAddress.getByName(removeIp); UniAddressaddress=newUniAddress(ip); //权限验证 auth=newNtlmPasswordAuthentication(removeIp,removeLoginUser,removeLoginPass); SmbSession.logon(address,auth); //远程路径判断文件文件路径是否合法 SmbFileremoteFile=newSmbFile(removeDir+dir.getName(),auth); remoteFile.connect(); if(remoteFile.isDirectory()){ retVal=-1; } //向远程共享目录写入文件 out=newBufferedOutputStream(newSmbFileOutputStream(remoteFile)); out.write(toByteArray(dir)); }catch(UnknownHostExceptione){ retVal=-1; e.printStackTrace(); }catch(MalformedURLExceptione){ retVal=-1; e.printStackTrace(); }catch(SmbExceptione){ retVal=-1; e.printStackTrace(); }catch(IOExceptione){ retVal=-1; e.printStackTrace(); }finally{ if(out!=null){ try{ out.close(); }catch(IOExceptione){ e.printStackTrace(); } } } returnretVal; } /** *MappedFilewayMappedByteBuffer可以在处理大文件时,提升性能 * *@paramfile文件 *@return字节数组 *@throwsIOExceptionIO异常信息 */ @SuppressWarnings("resource") publicstaticbyte[]toByteArray(Filefile)throwsIOException{ FileChannelfc=null; try{ fc=newRandomAccessFile(file,"r").getChannel(); MappedByteBufferbyteBuffer=fc.map(MapMode.READ_ONLY,0, fc.size()).load(); byte[]result=newbyte[(int)fc.size()]; if(byteBuffer.remaining()>0){ byteBuffer.get(result,0,byteBuffer.remaining()); } returnresult; }catch(IOExceptione){ e.printStackTrace(); throwe; }finally{ try{ fc.close(); }catch(IOExceptione){ e.printStackTrace(); } } }
总结
以上所述是小编给大家介绍的Java远程共享目录的操作代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!