工作需求要遠程控制公共盤,百度了一圈發現能用jcifs實現
添加依賴
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version>
</dependency>
這邊有個問題,網上的教程一般用的版本都是1.3.3,我用1.3.3的話會報錯,改成1.3.17一切正常,原因未知
jcifs.util.transport.TransportException Connection reset
springboot 版本是2.5.2
public class demo2 {
private static String USER_DOMAIN = ""; //域賬號,沒有可以不填
private static String USER_ACCOUNT = "test"; //賬號
private static String USER_PWS = "test"; //密碼
public static void main(String[] args) throws Exception {
String shareDir = "smb://xxx.xxx.xx.xx/sharemovie";
copyFiles(shareDir);
}
public static void copyFiles(String shareDirectory) throws IOException {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN,USER_ACCOUNT, USER_PWS);
SmbFile remoteFile = new SmbFile(shareDirectory+"/aa.txt", auth);
SmbFile targetFile = new SmbFile(shareDirectory+"/a/aa.txt", auth);
if (remoteFile.exists()) {
System.out.println("yes");
remoteFile.copyTo(targetFile);
}else{
System.out.println("no");
}
}
}
其他問題:
1.windows10兩邊都需要開啟SMB,否則,也是
Connection reset的錯誤,和局域網防火牆是沒有什么關系
2.github上找到的版本都是以2開頭,官網上只有1開頭的
兩種版本方法調用不同
<dependency>
<groupId>org.codelibs</groupId> <artifactId>jcifs</artifactId> <version>2.1.19</version> </dependency>