java 共享目录_java操作远程共享目录

/*** 方法说明:上传文件到远程共享目录

*@paramlocalDir 本地临时路径(A:/测试/测试.xls)

*@paramremoveDir 远程共享路径(smb://10.169.2.xx/测试/,特殊路径只能用/)

*@paramremoveIp 远程共享目录IP(10.169.2.xx)

*@paramremoveLoginUser 远程共享目录用户名(user)

*@paramremoveLoginPass 远程共享目录密码(password)

*@return*@throwsException 0成功/-1失败*/

public static intsmbUploading(String localDir, String removeDir,

String removeIp, String removeLoginUser, String removeLoginPass)throwsException {

NtlmPasswordAuthentication auth= null;

OutputStream out= null;int retVal = 0;try{

File dir= newFile(localDir);if (!dir.exists()) {

dir.mkdirs();

}

InetAddress ip=InetAddress.getByName(removeIp);

UniAddress address= newUniAddress(ip);//权限验证

auth = newNtlmPasswordAuthentication(removeIp, removeLoginUser, removeLoginPass);

SmbSession.logon(address,auth);//远程路径判断文件文件路径是否合法

SmbFile remoteFile = new SmbFile(removeDir +dir.getName(), auth);

remoteFile.connect();if(remoteFile.isDirectory()){

retVal= -1;

}//向远程共享目录写入文件

out = new BufferedOutputStream(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 way MappedByteBuffer 可以在处理大文件时,提升性能

*

*@paramfile 文件

*@return字节数组

*@throwsIOException IO异常信息*/@SuppressWarnings("resource")public static byte[] toByteArray(File file) throwsIOException {

FileChannel fc= null;try{

fc= new RandomAccessFile(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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值