添加ftp相关配置类,以及添加fpt工具类
This commit is contained in:
parent
084c683805
commit
7338b6b7b0
|
|
@ -7,21 +7,36 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@ConfigurationProperties(prefix = "ftp")
|
@ConfigurationProperties(prefix = "ftp")
|
||||||
public class FTPConfig {
|
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) {
|
public void setServerUrl(String serverUrl) {
|
||||||
this.server = server;
|
this.serverUrl = serverUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
public static int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -29,15 +44,15 @@ public class FTPConfig {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUser() {
|
public static String getUserName() {
|
||||||
return user;
|
return userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUser(String user) {
|
public void setUserName(String userName) {
|
||||||
this.user = user;
|
this.userName = userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public static String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,12 @@ import java.io.InputStream;
|
||||||
|
|
||||||
public class FTPUtils {
|
public class FTPUtils {
|
||||||
|
|
||||||
|
|
||||||
public void uploadFile(String remotePath, MultipartFile file) throws IOException {
|
public void uploadFile(String remotePath, MultipartFile file) throws IOException {
|
||||||
FTPClient ftpClient = new FTPClient();
|
|
||||||
FTPConfig ftpConfig = new FTPConfig();
|
FTPClient ftpClient = this.getConnection();
|
||||||
|
|
||||||
try {
|
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()) {
|
try (InputStream inputStream = file.getInputStream()) {
|
||||||
boolean done = ftpClient.storeFile(remotePath, inputStream);
|
boolean done = ftpClient.storeFile(remotePath, inputStream);
|
||||||
|
|
@ -27,6 +25,7 @@ public class FTPUtils {
|
||||||
throw new IOException("Failed to upload file " + file.getOriginalFilename());
|
throw new IOException("Failed to upload file " + file.getOriginalFilename());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (ftpClient.isConnected()) {
|
if (ftpClient.isConnected()) {
|
||||||
ftpClient.logout();
|
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