Maven 使用 jcifs 1.3.17 版本
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version>
</dependency>
JAVA 代码如下
package com.xxx.common;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.xxx.UIFrame;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @Date: 2022/10/25
*/
public class GainRemoteFile {
private static String REMOTEURL = "//192.168.10.202/";
private static String REMOTEUSERNAME = "user";
private static String REMOTEPASSWORD = "psd";
/**
* 判断是否需要更新
*/
public static boolean checkNeedUpdate() {
String remoteVersion = "";
try {
remoteVersion = getRemoteFileLike(REMOTEURL, "查询工具");
} catch (Exception e) {
e.printStackTrace();
}
return !UIFrame.VERSION.startsWith(remoteVersion);
}
/**
* 获取远程文件
*
* @param remoteFilepath 远程文件地址,该参数需以IP打头,如'192.168.8.2/aa/bb.java'或者'192.168.8.2/aa/',如'192.168.8.2/aa'是不对的
* @return SmbFile 获取SmbFile文件
* @throws Exception
*/
private static SmbFile getRemoteSmbFilePath(String remoteFilepath) {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, REMOTEUSERNAME, REMOTEPASSWORD);
SmbFile smbFile = null;
try {
smbFile = new SmbFile("smb:" + remoteFilepath, auth);
if (!smbFile.exists()) {
Console.log("no such folder");
}
} catch (Exception e) {
e.printStackTrace();
}
return smbFile;
}
/**
* 获取远程文件列表
*/
public static List<SmbFile> getRemoteFileList(String remoteFilepath) throws Exception {
SmbFile smbFile = getRemoteSmbFilePath(remoteFilepath);
return smbFile == null ? Collections.EMPTY_LIST : Arrays.asList(smbFile.listFiles());
}
/**
* 获取 远程文件名称
*/
public static String getRemoteFileLike(String remoteFilepath, String remoteFileNameLike) throws Exception {
if (StrUtil.isBlank(remoteFileNameLike)) {
return null;
}
for (SmbFile smbFile : getRemoteFileList(remoteFilepath)) {
String name = smbFile.getName();
if (name.contains(remoteFileNameLike)) {
return name;
}
}
return null;
}
/**
* 判断远程文件是否存在
*/
public static boolean remoteFileExist(String remoteFilepath, String remoteFileNameLike) throws Exception {
if (StrUtil.isNotBlank(remoteFileNameLike)) {
return StrUtil.isNotBlank(getRemoteFileLike(remoteFilepath, remoteFileNameLike));
}
return false;
}
/**
* 拷贝远程文件到本地目录
*/
public static boolean copyRemoteFile(String remoteFilepath, String remoteFileNameLike, String localDirectory) throws Exception {
for (SmbFile smbFile : getRemoteFileList(remoteFilepath)) {
String name = smbFile.getName();
if (!name.contains(remoteFileNameLike)) {
continue;
}
File file = FileUtil.file(localDirectory, name);
try {
if (file.exists()) {
file.delete();
} else if (smbFile.exists()) {
BufferedInputStream bis = new BufferedInputStream(new SmbFileInputStream(smbFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
int len = 0;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
}
以下内容可以忽略
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】
发文助手会检测您的文章标题、错别字、内容质量,助您提升文章质量。【创作规范】