Pre Merge pull request !493 from Rookie/addFtpconfig
This commit is contained in:
commit
54ce5cdc1a
19
pom.xml
19
pom.xml
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
<name>ruoyi</name>
|
||||
<url>http://www.ruoyi.vip</url>
|
||||
<description>若依管理系统</description>
|
||||
|
||||
|
||||
<properties>
|
||||
<ruoyi.version>4.7.9</ruoyi.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
|
@ -30,6 +30,8 @@
|
|||
<commons.io.version>2.13.0</commons.io.version>
|
||||
<poi.version>4.1.2</poi.version>
|
||||
<velocity.version>2.3</velocity.version>
|
||||
<commons-net.version>3.10.0</commons-net.version>
|
||||
|
||||
</properties>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
|
|
@ -60,7 +62,7 @@
|
|||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 验证码 -->
|
||||
<dependency>
|
||||
<groupId>pro.fessional</groupId>
|
||||
|
|
@ -193,6 +195,17 @@
|
|||
<version>${ruoyi.version}</version>
|
||||
</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>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ server:
|
|||
max: 800
|
||||
# Tomcat启动初始化的线程数,默认值10
|
||||
min-spare: 100
|
||||
|
||||
|
||||
# 日志配置
|
||||
logging:
|
||||
level:
|
||||
|
|
@ -140,3 +140,11 @@ xss:
|
|||
swagger:
|
||||
# 是否开启swagger
|
||||
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