添加ftp相关配置类,以及添加fpt工具类
This commit is contained in:
parent
084c683805
commit
7338b6b7b0
|
|
@ -7,21 +7,36 @@ import org.springframework.context.annotation.Configuration;
|
|||
@ConfigurationProperties(prefix = "ftp")
|
||||
public class FTPConfig {
|
||||
|
||||
private String server;
|
||||
private int port;
|
||||
private String user;
|
||||
private String password;
|
||||
/**
|
||||
* 服务地址
|
||||
*/
|
||||
private static String serverUrl;
|
||||
|
||||
// Getters and Setters
|
||||
public String getServer() {
|
||||
return server;
|
||||
/**
|
||||
* 端口
|
||||
*/
|
||||
private static int port;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private static String userName;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private static String password;
|
||||
|
||||
|
||||
public static String getServerUrl() {
|
||||
return serverUrl;
|
||||
}
|
||||
|
||||
public void setServer(String server) {
|
||||
this.server = server;
|
||||
public void setServerUrl(String serverUrl) {
|
||||
this.serverUrl = serverUrl;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
public static int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
|
|
@ -29,15 +44,15 @@ public class FTPConfig {
|
|||
this.port = port;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
public static String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
public static String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,14 +10,12 @@ import java.io.InputStream;
|
|||
|
||||
public class FTPUtils {
|
||||
|
||||
|
||||
public void uploadFile(String remotePath, MultipartFile file) throws IOException {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPConfig ftpConfig = new FTPConfig();
|
||||
|
||||
FTPClient ftpClient = this.getConnection();
|
||||
|
||||
try {
|
||||
ftpClient.connect(ftpConfig.getServer(), ftpConfig.getPort());
|
||||
ftpClient.login(ftpConfig.getUser(), ftpConfig.getPassword());
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
boolean done = ftpClient.storeFile(remotePath, inputStream);
|
||||
|
|
@ -27,6 +25,7 @@ public class FTPUtils {
|
|||
throw new IOException("Failed to upload file " + file.getOriginalFilename());
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (ftpClient.isConnected()) {
|
||||
ftpClient.logout();
|
||||
|
|
@ -34,4 +33,21 @@ public class FTPUtils {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 建立连接
|
||||
*/
|
||||
private FTPClient getConnection() throws IOException {
|
||||
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
|
||||
ftpClient.connect(FTPConfig.getServerUrl(), FTPConfig.getPort());
|
||||
ftpClient.login(FTPConfig.getUserName(), FTPConfig.getPassword());
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
|
||||
return ftpClient;
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue