一.前言

根据客户反馈,在进行文件下载的时候,新增远程共享目录,下载对应的文件到远程共享目录,采用常用的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操作远程共享目录的更多相关文章

  1. 远程映射错误 “发生系统错误 1312 指定的登录会话不存在。可能已被终止 IIS 访问 远程共享目录”

    最近和其他公司做接口,需要将数据上传给对方. 我们发送程序部署在前置机上,文件在内网数据中.需要映射到文件服务器后上传数据.本机vs开发是可以映射成功,但是部署到远程的IIS中,就不能成功. 报错:  ...

  2. Linux 中 java 访问 windows共享目录

    有两个方案: 1.将windows共享目录,挂载到linux系统下,通过使用本地目录访问windows共享目录 2.通过samba的java实现包,不过需要开个windows共享目录的账户  http ...

  3. JAVA 操作远程mysql数据库实现单表增删改查操作

    package MysqlTest; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.C ...

  4. 在Jenkins上配置批处理删除远程共享目录7天以上的文件

    net use * /del /yes NET USE X: \\10.29.48.12\shares\Test password /user:DOMAIN1\account set AutoPath ...

  5. [BAT] 以当前时间为名创建文件夹,将本地文件夹里的文件拷贝到远程共享目录,而且保证本地和Jenkins上运行都成功

    @echo off rem connect to szotpc801 net use * /del /yes NET USE X: \\10.66.234.95\d$ Autotest123 /use ...

  6. java安全编码指南之:文件和共享目录的安全性

    目录 简介 linux下的文件基本权限 linux文件的特殊权限 Set UID 和 Set GID Sticky Bit SUID/SGID/SBIT权限设置 文件隐藏属性 特殊文件 java中在共 ...

  7. java基本权限指南之:文件和共享目录的基本权限

    简介 java程序是跨平台的,可以运行在windows也可以运行在linux.但是平台不同,平台中的文件权限也是不同的.windows大家经常使用,并且是可视化的权限管理,这里就不多讲了. 本文主要讲 ...

  8. Msftables之Linux NFS共享目录配置漏洞

    实验目的 1.了解Metasploitables靶机系统漏洞: 2.学习使用Metasploit. 实验原理 msftables之利用Linux NFS共享目录配置漏洞渗透. 实验内容 msftabl ...

  9. 在java程序中访问windows有用户名和密码保护的共享目录

    在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0)  编辑  收藏 --> Jav ...

随机推荐

  1. frp使用笔记

    参考文档: https://github.com/fatedier/frp/blob/master/README_zh.md#%E9%80%9A%E8%BF%87-frpc-%E6%89%80%E5% ...

  2. selenium+java+chrome 自动化测试环境搭建

    安装jdk    (jdk 配置环境变量)    eclipse(可用免安装的) 安装谷歌浏览器 下载chorme driver (chorme driver 也要配置环境变量,将chormedriv ...

  3. 在excel中如何利用vba通过网址读取网页title(网址是https的)?

    昨天在百度知道上提了这个问题,我保存了些百度知道我回答的网址,想利用excel直接读取出网址的title,请问vba代码怎么写?(要支持https的) excel大神帮我回答了,在这记录下: Func ...

  4. 基于OpenCV的图书扫描识别程序开发

    1.AndroidStudio环境配置 https://www.cnblogs.com/little-monkey/p/7162340.html

  5. noip第19课资料

  6. 《mysql必知必会》学习_第15章_20180806_欢

    第15章:联结表 P98 外键:外键为某个表的一列A,同时这一列包含另一个表的主键值B(B属于A,等于或者小于的关系) P99 select vend_name,prod_name,prod_pric ...

  7. shell 命令 zip unzip

    工作当中,经常要用到zip压缩. zip 将文件夹打包: 如文件夹名 xxx zip -r xxx.zip xxx unzip unzip -o xxx.zip -o 覆盖原有的文件夹 查询更多参数: ...

  8. 使用netstat命令查看端口的使用情况

    Windows如何查看端口占用情况操作步骤如下: 开始--运行--cmd 进入命令提示符,输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管 ...

  9. Vue学习笔记八:v-for,v-if,v-show指令

    目录 v-for指令:遍历 HTML和效果图 v-for讲解 v-if和v-show:创建,删除,显示,隐藏 HTML和效果图 v-if和v-show的原理 v-for指令:遍历 HTML和效果图 有 ...

  10. document.domain实现不同域名跨域

    利用document.domain 实现跨域:前提条件:这两个域名必须属于同一个基础域名!而且所用的协议,端口都要一致,否则无法利用document.domain进行跨域. 两个子域名:aaa.xxx ...