导入maven包

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>smbj</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

在yml文件中建立配置

1
2
3
4
5
6
7
8
9
10
11
windows:
host: 192.168.1.105
port: 445
username: test
password: test
//备份目录
backupPath: backup_path
//升级目录
upgradingPath: upgrading_path
//共享文件夹目录
remoteUrl: dfsss

建立对应的 windows 映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.lab.common.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "windows")
public class WindowsConfig {

private static String host;
private static int port;

private static String username;

private static String password;

private static String backupPath;

private static String upgradingPath;

public static String getHost() {
return host;
}

public void setHost(String host) {
WindowsConfig.host = host;
}

public static int getPort() {
return port;
}

public void setPort(int port) {
WindowsConfig.port = port;
}

public static String getUsername() {
return username;
}

public void setUsername(String username) {
WindowsConfig.username = username;
}

public static String getPassword() {
return password;
}

public void setPassword(String password) {
WindowsConfig.password = password;
}

public static String getBackupPath() {
return backupPath;
}

public void setBackupPath(String backupPath) {
WindowsConfig.backupPath = backupPath;
}

public static String getUpgradingPath() {
return upgradingPath;
}

public void setUpgradingPath(String upgradingPath) {
WindowsConfig.upgradingPath = upgradingPath;
}
}

建立对应使用的 smbUtils 使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.lab.common.utils.smb;

import com.hierynomus.msdtyp.AccessMask;
import com.hierynomus.msfscc.FileAttributes;
import com.hierynomus.mssmb2.SMB2CreateDisposition;
import com.hierynomus.mssmb2.SMB2CreateOptions;
import com.hierynomus.mssmb2.SMB2ShareAccess;
import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskShare;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

public class SmbUtils {

private static final Logger log = LoggerFactory.getLogger(SmbUtils.class);
public static boolean uploadFileToServer(String host, String port, String username,
String password, List<String> pathList, String localStore,String remoteUrl){
boolean flag = true;
OutputStream shareOs = null;
SMBClient client = null;
Session session = null;
DiskShare diskShare = null;
Connection connection = null;
AuthenticationContext authenticationContext = null;
try {
client = new SMBClient();
connection = client.connect(host);
// 创建连接会话.
authenticationContext = new AuthenticationContext(username, password.toCharArray(), "");
session = connection.authenticate(authenticationContext);
// 远程开启文件
diskShare = (DiskShare) session.connectShare(remoteUrl);
for (String path:pathList) {
fileUpload(localStore, path, diskShare);
}
} catch (IOException e) {
log.error("机台同步项目文件报错"+e);
return false;
} finally {
try {
if (null != shareOs) {
shareOs.close();
}
if (null != diskShare) {
diskShare.close();
}
if (null != session) {
session.close();
}
if (null != client) {
client.close();
}
} catch (IOException ex) {
log.error("机台同步项目文件报错"+ex);
return false;
}
return flag;
}
}

public static void fileUpload(String localStore,String path, DiskShare diskShare) throws IOException {
createRemoteDir(diskShare,path);

// 获取文件流.上传文件.
com.hierynomus.smbj.share.File shareFile = diskShare.openFile(path, EnumSet.of(AccessMask.GENERIC_WRITE),
EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL), SMB2ShareAccess.ALL,
SMB2CreateDisposition.FILE_OPEN_IF, EnumSet.noneOf(SMB2CreateOptions.class));
OutputStream shareOs = shareFile.getOutputStream();
File file = new File(localStore);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len;
while((len = fileInputStream.read(bytes))!=-1){
shareOs.write(bytes,0,len);
}
// shareOs.write();
}

/**
* 创建远程目录
*
* @param share
* @param dirName
*/
private static void createRemoteDir(DiskShare share, String dirName) {
// 是否已存在
if (share.folderExists(dirName)) {
return;
}

// 目录不存在,逐层创建
String[] fileNameArr = dirName.split("/");
String tempDirName = ""; // 逐层目录

for (int i = 0; i < fileNameArr.length - 1; i++) {
tempDirName += fileNameArr[i];// 下一层目录
if (share.folderExists(tempDirName)) {
tempDirName += "/";
continue;
}
tempDirName += "/";
System.out.println("目录: " + tempDirName + " 不存在,即将创建");
share.mkdir(tempDirName);
}
}

}