java操作远程共享目录
一.前言
根据客户反馈,在进行文件下载的时候,新增远程共享目录,下载对应的文件到远程共享目录,采用常用的IO操作模式,提示下载成功,但是客户去远程共享目录查看对应的下载文件,反馈说没有找到对应的文件。要求系统需要支持上传远程共享目录,为什么有一个这样的需求?由于下载的文件涉及到了支付文件,里面的金额不允许进行修改,如果放在本地路径有可能会不会出现人为的修改,一般涉及到钱的问题,客户都是比较谨慎的,刚好没有接触过操作远程共享目录的,就google了一下看有没有对应的操作说明,下面简单总结一下。
二.远程共享目录操作
1、需要下载对应的jcifs-1.3.18.jar,本例子采用3.18版本的,下载链接:https://jcifs.samba.org/
2、涉及的主要类是 SmbFile(远程文件操作类) ,还有就是进行登录验证,验证对应的远程目录的合法性的操作,其他操作就普通的IO流的操作。
3、从远程共享目录下载文件
/**
* 方法说明:从远程共享目录下载文件
* @param localDir 本地临时路径
* @param removeDir 远程共享路径
* @param _fileName 远程共享文件名
* @param removeIp 远程共享目录IP
* @param removeLoginUser 远程共享目录用户名
* @param removeLoginPass 远程共享目录密码
* @return
* @throws Exception
*/
public static int smbDownload(String localDir, String removeDir,
String _fileName, String removeIp, String removeLoginUser,
String removeLoginPass) throws Exception {
InputStream in = null;
OutputStream out = null;
try {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
removeIp, removeLoginUser, removeLoginPass);
SmbFile remoteFile = new SmbFile(removeDir + _fileName, auth);
if (!remoteFile.exists()) {
return 0;
} File dir = new File(localDir);
if (!dir.exists()) {
dir.mkdirs();
} String fileName = _fileName.substring(_fileName.lastIndexOf("\\")+1, _fileName.length());
File localFile = new File(localDir + fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(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();
}
}
}
}
return 1;
}
4、上传文件都远程共享目录
/**
* 方法说明:上传文件到远程共享目录
* @param localDir 本地临时路径(A:/测试/测试.xls)
* @param removeDir 远程共享路径(smb://10.169.2.xx/测试/,特殊路径只能用/)
* @param removeIp 远程共享目录IP(10.169.2.xx)
* @param removeLoginUser 远程共享目录用户名(user)
* @param removeLoginPass 远程共享目录密码(password)
* @return
* @throws Exception 0成功/-1失败
*/
public static int smbUploading(String localDir, String removeDir,
String removeIp, String removeLoginUser, String removeLoginPass) throws Exception {
NtlmPasswordAuthentication auth = null;
OutputStream out = null;
int retVal = 0;
try {
File dir = new File(localDir);
if (!dir.exists()) {
dir.mkdirs();
} InetAddress ip = InetAddress.getByName(removeIp);
UniAddress address = new UniAddress(ip);
// 权限验证
auth = new NtlmPasswordAuthentication(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(new SmbFileOutputStream(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();
}
}
} return retVal;
} /**
* Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
*
* @param file 文件
* @return 字节数组
* @throws IOException IO异常信息
*/
@SuppressWarnings("resource")
public static byte[] toByteArray(File file) throws IOException {
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());
}
return result;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java操作远程共享目录的更多相关文章
- 远程映射错误 “发生系统错误 1312 指定的登录会话不存在。可能已被终止 IIS 访问 远程共享目录”
最近和其他公司做接口,需要将数据上传给对方. 我们发送程序部署在前置机上,文件在内网数据中.需要映射到文件服务器后上传数据.本机vs开发是可以映射成功,但是部署到远程的IIS中,就不能成功. 报错: ...
- Linux 中 java 访问 windows共享目录
有两个方案: 1.将windows共享目录,挂载到linux系统下,通过使用本地目录访问windows共享目录 2.通过samba的java实现包,不过需要开个windows共享目录的账户 http ...
- JAVA 操作远程mysql数据库实现单表增删改查操作
package MysqlTest; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.C ...
- 在Jenkins上配置批处理删除远程共享目录7天以上的文件
net use * /del /yes NET USE X: \\10.29.48.12\shares\Test password /user:DOMAIN1\account set AutoPath ...
- [BAT] 以当前时间为名创建文件夹,将本地文件夹里的文件拷贝到远程共享目录,而且保证本地和Jenkins上运行都成功
@echo off rem connect to szotpc801 net use * /del /yes NET USE X: \\10.66.234.95\d$ Autotest123 /use ...
- java安全编码指南之:文件和共享目录的安全性
目录 简介 linux下的文件基本权限 linux文件的特殊权限 Set UID 和 Set GID Sticky Bit SUID/SGID/SBIT权限设置 文件隐藏属性 特殊文件 java中在共 ...
- java基本权限指南之:文件和共享目录的基本权限
简介 java程序是跨平台的,可以运行在windows也可以运行在linux.但是平台不同,平台中的文件权限也是不同的.windows大家经常使用,并且是可视化的权限管理,这里就不多讲了. 本文主要讲 ...
- Msftables之Linux NFS共享目录配置漏洞
实验目的 1.了解Metasploitables靶机系统漏洞: 2.学习使用Metasploit. 实验原理 msftables之利用Linux NFS共享目录配置漏洞渗透. 实验内容 msftabl ...
- 在java程序中访问windows有用户名和密码保护的共享目录
在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0) 编辑 收藏 --> Jav ...
随机推荐
- Django haystack+solr搜索引擎部署的坑.
跟着<<Django by Example>> 一路做下来,到了搭建搜索引擎的步骤 默认的思路是用 obj.objects.filter(body__icontains='fr ...
- 登录服务器失败 IP 统计和处理方法
一.登录ssh失败次数统计 1)错误的打开方式 awk '/Failed password/ {print $(NF-3)}' secure |sort -n |uniq -c|sort -n |ta ...
- python中,类方法和静态方法区别。
面相对象程序设计中,类方法和静态方法是经常用到的两个术语. 逻辑上讲:类方法是只能由类名调用:静态方法可以由类名或对象名进行调用. 在C++中,静态方法与类方法逻辑上是等价的,只有一个概念,不会混淆. ...
- ModelAndView对象
ModelAndView属性中两个最重要的属性是model和view. view即视图,保存的是视图信息. model即模型,以<K,V>形式保存模型数据,上图可以看到是MdelMap类型 ...
- Jsp+Struts2+JavaBean+DAO开发模式(1)
DAO模式就实现了把数据库表的操作转化对Java类的操作,从而提高程序的可读性,并实现更改数据库的方便性.其架构图如下图. 一共分为五个组件(component) jsp提交页面(一下四其中的一个例子 ...
- .NET Core微服务之路:让我们对上一个Demo通讯进行修改,完成RPC通讯
最近一段时间有些事情耽搁了更新,抱歉各位了. 上一篇我们简单的介绍了DotNetty通信框架,并简单的介绍了基于DotNetty实现了回路(Echo)通信过程. 我们来回忆一下上一个项目的整个流程: ...
- 源设置导致Docker镜像构建失败
编写了一个Dockerfile,主要目的是构建一个镜像,镜像默认安装了openjdk-1.8-jre,还有另外一些包(这些包里面有dev版本的,也有release版本的),Dockerfile的内容大 ...
- JQuery Mobile - input 属性为 number,maxlength不起作用如何解决?
<input type="text" maxlength="5" /> 效果ok,当 <input type="number& ...
- sudo rm -rf iTunes.app Operation not permitted
https://www.howtogeek.com/230424/how-to-disable-system-integrity-protection-on-a-mac-and-why-you-sho ...
- MongoDB 数据库
数据库: 关系型数据库 mysql 收费 速度快 字段类型 非关系型数据库 MongoDB 不收费 速度慢一些 存储数据都是字符串 ...