这篇文章将为大家详细讲解有关利用Java怎么对文件目录进行远程共享,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
.远程共享目录操作
1、需要下载对应的jcifs-1.3.18.jar
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怎么对文件目录进行远程共享就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。