-
Notifications
You must be signed in to change notification settings - Fork 4
/
SftpImpl.java
207 lines (180 loc) · 6.54 KB
/
SftpImpl.java
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.project.ttotw.lib;
import com.jcraft.jsch.*;
import com.project.ttotw.entity.File;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.UUID;
import java.util.Vector;
@Slf4j
@Component
public class SftpImpl implements FtpUtils{
@Value("${ttotw.sftp.server}")
private String server;
@Value("${ttotw.sftp.port}")
private int port;
@Value("${ttotw.sftp.username}")
private String username;
@Value(("${ttotw.sftp.privateKey}"))
private String privateKey;
@Value("${ttotw.sftp.documentRoot}")
private String fileServerDocumentRoot;
private final String SEPARATOR = "/";
private final String PERIOD = ".";
private JSch jSch;
private Session jSchSession;
private Channel channel;
private ChannelSftp channelSftp;
@Override
public void open() {
//JSch 객체를 생성
jSch = new JSch();
try {
//privateKey 인증
jSch.addIdentity(privateKey);
//JSchSession 객체를 생성 (사용자 이름, 접속할 호스트, 포트 전달)
jSchSession = jSch.getSession(username, server, port);
//기타 설정 적용
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
jSchSession.setConfig(config);
//접속
jSchSession.connect();
//sftp 채널 열기 및 접속
channel = jSchSession.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
log.error("SFTP:: server connect failed.");
}
//채널을 FTP 용 채널 객체로 캐스팅
channelSftp = (ChannelSftp) channel;
}
@Override
public void close() {
if (jSchSession.isConnected()) {
channelSftp.disconnect();
jSchSession.disconnect();
}
}
@Override
public File upload(String firstDirectory, MultipartFile file) {
open();
String originName = StringUtils.getFilename(file.getOriginalFilename());
String savedName = "";
String savedPath2 = "";
String extension = "";
try (InputStream inputStream = file.getInputStream()){
//directory to store files for today's date
String yyyyMMdd = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
//first directory path
String firstDirectoryPath = fileServerDocumentRoot + SEPARATOR + firstDirectory;
//upload directory path
String uploadDirectoryPath = fileServerDocumentRoot + SEPARATOR + firstDirectory + SEPARATOR + yyyyMMdd;
savedPath2 = SEPARATOR + firstDirectory + SEPARATOR + yyyyMMdd;
//create directory
createDirectory(firstDirectoryPath, uploadDirectoryPath);
//create uuid file name
String uuidFileName = UUID.randomUUID().toString();
savedName = uuidFileName;
//file extension
String fileExtension = StringUtils.getFilenameExtension(file.getOriginalFilename());
extension = fileExtension;
channelSftp.put(inputStream, uploadDirectoryPath + SEPARATOR + uuidFileName + PERIOD + fileExtension);
} catch (IOException | SftpException e) {
log.error("SFTP:: file upload failed.");
e.printStackTrace();
return null;
} finally {
close();
}
return File.builder()
.originName(originName)
.savedName(savedName)
.savedHost(server)
.savedPath1(fileServerDocumentRoot)
.savedPath2(savedPath2)
.fileExt(extension)
.useAt(true)
.build();
}
@Override
public void delete(String path) {
open();
try {
if (exists(path)) {
channelSftp.rm(path);
}
} catch (SftpException e) {
log.error("SFTP:: file delete failed.");
e.printStackTrace();
} finally {
close();
}
}
private void createDirectory(String firstDirectoryPath, String uploadPath) throws IOException {
try {
//check document root
if (!this.exists(fileServerDocumentRoot)) {
log.error("SFTP:: server doesn't exists root directory");
throw new IOException();
}
//directory check, if not exists create
if (!this.exists(firstDirectoryPath)) {
channelSftp.mkdir(firstDirectoryPath);
}
if (!this.exists(uploadPath)) {
channelSftp.mkdir(uploadPath);
}
} catch (SftpException e) {
log.error("SFTP:: create directory failed.");
e.printStackTrace();
}
}
//경로가 존재하는지 확인
private boolean exists(String path) {
Vector res = null;
try {
res = channelSftp.ls(path);
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
return false;
} else {
e.printStackTrace();
}
}
return res != null && !res.isEmpty();
}
@Override
public void getImage(String path, ServletOutputStream servletOutputStream) {
open();
try {
if (exists(path)) {
try (InputStream inputStream = channelSftp.get(path); OutputStream outputStream = servletOutputStream) {
int length;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
} catch (SftpException e) {
log.error("SFTP:: get image failed.");
e.printStackTrace();
} catch (IOException e) {
log.error("SFTP:: get image failed.");
e.printStackTrace();
}
}
} finally {
close();
}
}
}