Pre Merge pull request !493 from Rookie/addFtpconfig
This commit is contained in:
commit
54ce5cdc1a
13
pom.xml
13
pom.xml
|
|
@ -30,6 +30,8 @@
|
||||||
<commons.io.version>2.13.0</commons.io.version>
|
<commons.io.version>2.13.0</commons.io.version>
|
||||||
<poi.version>4.1.2</poi.version>
|
<poi.version>4.1.2</poi.version>
|
||||||
<velocity.version>2.3</velocity.version>
|
<velocity.version>2.3</velocity.version>
|
||||||
|
<commons-net.version>3.10.0</commons-net.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<!-- 依赖声明 -->
|
<!-- 依赖声明 -->
|
||||||
|
|
@ -193,6 +195,17 @@
|
||||||
<version>${ruoyi.version}</version>
|
<version>${ruoyi.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- ftp工具 -->
|
||||||
|
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-net</groupId>
|
||||||
|
<artifactId>commons-net</artifactId>
|
||||||
|
<version>${commons-net.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -140,3 +140,11 @@ xss:
|
||||||
swagger:
|
swagger:
|
||||||
# 是否开启swagger
|
# 是否开启swagger
|
||||||
enabled: true
|
enabled: true
|
||||||
|
|
||||||
|
|
||||||
|
# ftp配置
|
||||||
|
ftp:
|
||||||
|
server: ftp.example.com
|
||||||
|
port: 21
|
||||||
|
user: ftpusername
|
||||||
|
password: ftppassword
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.ruoyi.common.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "ftp")
|
||||||
|
public class FTPConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务地址
|
||||||
|
*/
|
||||||
|
private static String serverUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 端口
|
||||||
|
*/
|
||||||
|
private static int port;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private static String userName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private static String password;
|
||||||
|
|
||||||
|
|
||||||
|
public static String getServerUrl() {
|
||||||
|
return serverUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServerUrl(String serverUrl) {
|
||||||
|
this.serverUrl = serverUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.ruoyi.common.utils.file;
|
||||||
|
|
||||||
|
import com.ruoyi.common.config.FTPConfig;
|
||||||
|
import org.apache.commons.net.ftp.FTP;
|
||||||
|
import org.apache.commons.net.ftp.FTPClient;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class FTPUtils {
|
||||||
|
|
||||||
|
|
||||||
|
public void uploadFile(String remotePath, MultipartFile file) throws IOException {
|
||||||
|
|
||||||
|
FTPClient ftpClient = this.getConnection();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
try (InputStream inputStream = file.getInputStream()) {
|
||||||
|
boolean done = ftpClient.storeFile(remotePath, inputStream);
|
||||||
|
if (done) {
|
||||||
|
System.out.println("File " + file.getOriginalFilename() + " has been uploaded successfully.");
|
||||||
|
} else {
|
||||||
|
throw new IOException("Failed to upload file " + file.getOriginalFilename());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (ftpClient.isConnected()) {
|
||||||
|
ftpClient.logout();
|
||||||
|
ftpClient.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 建立连接
|
||||||
|
*/
|
||||||
|
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