1、文件上传模块集成ftp功能并测试实现
This commit is contained in:
parent
2e07b49209
commit
0158b62f88
1
pom.xml
1
pom.xml
|
|
@ -46,6 +46,7 @@
|
|||
<knife4j.version>3.0.3</knife4j.version>
|
||||
<hutool.version>5.7.17</hutool.version>
|
||||
<mybatisplus.version>3.4.3.4</mybatisplus.version>
|
||||
<commonsNet.version>3.6</commonsNet.version>
|
||||
|
||||
</properties>
|
||||
|
||||
|
|
|
|||
|
|
@ -125,11 +125,20 @@
|
|||
<artifactId>swagger-annotations</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--xjs-->
|
||||
|
||||
<!--hutool工具类-->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<!--lombok简化实体类-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
|||
|
|
@ -16,56 +16,63 @@
|
|||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- FastDFS -->
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Minio -->
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>${minio.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--FTP-->
|
||||
<dependency>
|
||||
<groupId>commons-net</groupId>
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>${commonsNet.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- RuoYi Api System -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-api-system</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- RuoYi Common Swagger -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-swagger</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
@ -84,5 +91,5 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package com.ruoyi.file.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* ftp配置信息
|
||||
* @author xiejs
|
||||
* @since 2022-01-24
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "ftp")
|
||||
@Data
|
||||
public class FtpProperties {
|
||||
|
||||
/**
|
||||
* 端口
|
||||
*/
|
||||
private String port;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String user;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
|
||||
/**
|
||||
* ftp服务ip
|
||||
*/
|
||||
private String serverIp;
|
||||
|
||||
/**
|
||||
* ftp文件路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 项目地址
|
||||
*/
|
||||
private String domain;
|
||||
|
||||
/**
|
||||
* 前缀
|
||||
*/
|
||||
private String prefix;
|
||||
}
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
package com.ruoyi.file.config;
|
||||
|
||||
import java.io.File;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 通用映射配置
|
||||
*
|
||||
|
|
@ -27,12 +28,29 @@ public class ResourcesConfig implements WebMvcConfigurer
|
|||
@Value("${file.prefix}")
|
||||
public String localFilePrefix;
|
||||
|
||||
|
||||
/**
|
||||
* ftp文件路径
|
||||
*/
|
||||
@Value("${ftp.path}")
|
||||
private String ftpPath;
|
||||
|
||||
/**
|
||||
* ftp资源映射路径 前缀
|
||||
*/
|
||||
@Value("${ftp.prefix}")
|
||||
public String ftpFilePrefix;
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry)
|
||||
{
|
||||
registry.addResourceHandler(ftpFilePrefix + "/**")
|
||||
.addResourceLocations("file:" + ftpPath + File.separator);
|
||||
/** 本地文件上传路径 */
|
||||
registry.addResourceHandler(localFilePrefix + "/**")
|
||||
.addResourceLocations("file:" + localFilePath + File.separator);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package com.ruoyi.file.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ruoyi.file.config.FtpProperties;
|
||||
import com.ruoyi.file.utils.FileUploadUtils;
|
||||
import com.ruoyi.file.utils.FtpUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* FTP 文件存储
|
||||
* @author xiejs
|
||||
* @since 2022-01-24
|
||||
*/
|
||||
@Service
|
||||
@Primary
|
||||
public class FtpSysFileServiceImpl implements ISysFileService{
|
||||
|
||||
@Autowired
|
||||
private FtpUtils ftpUtils;
|
||||
@Autowired
|
||||
private FtpProperties ftpProperties;
|
||||
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file) throws Exception {
|
||||
Assert.notNull(file, "file is null");
|
||||
String extension = FileUploadUtils.getExtension(file);
|
||||
String url = "";
|
||||
String fileName = UUID.randomUUID() + "." + extension;
|
||||
boolean uploadResult = ftpUtils.uploadFile(String.valueOf(DateUtil.thisYear()),fileName , file.getInputStream());
|
||||
if (uploadResult) {
|
||||
url = ftpProperties.getDomain() + ftpProperties.getPrefix() +"/"+DateUtil.thisYear()+"/"+ fileName;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,15 @@
|
|||
package com.ruoyi.file.service;
|
||||
|
||||
import com.ruoyi.file.utils.FileUploadUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.ruoyi.file.utils.FileUploadUtils;
|
||||
|
||||
/**
|
||||
* 本地文件存储
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Primary
|
||||
@Service
|
||||
public class LocalSysFileServiceImpl implements ISysFileService
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,118 @@
|
|||
package com.ruoyi.file.utils;
|
||||
|
||||
import cn.hutool.extra.ftp.Ftp;
|
||||
import com.ruoyi.file.config.FtpProperties;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ftp工具类
|
||||
* @author xjs
|
||||
* @since 2022-01-24 16:49:40
|
||||
*/
|
||||
@Component
|
||||
public class FtpUtils {
|
||||
|
||||
private Ftp ftp = null;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FtpUtils.class);
|
||||
public static String LOCAL_CHARSET = "GBK";
|
||||
|
||||
@Autowired
|
||||
private FtpProperties ftpProperties;
|
||||
|
||||
public Ftp instance() {
|
||||
if (ftp != null) {
|
||||
ftp.reconnectIfTimeout();
|
||||
} else {
|
||||
String ftpServerIp = ftpProperties.getServerIp();
|
||||
String ftpServerAccount = ftpProperties.getUser();
|
||||
String ftpServerPassword = ftpProperties.getPassword();
|
||||
String ftpServerPort = ftpProperties.getPort();
|
||||
synchronized (this) {
|
||||
if (StringUtils.isBlank(ftpServerPassword) || StringUtils.isBlank(ftpServerAccount)) {
|
||||
ftp = new Ftp(ftpServerIp, Integer.parseInt(ftpServerPort));
|
||||
} else {
|
||||
ftp = new Ftp(ftpServerIp, Integer.parseInt(ftpServerPort), ftpServerAccount, ftpServerPassword);
|
||||
}
|
||||
ftp.setBackToPwd(true);
|
||||
logger.info("login ftp server with {}:{}@{}:{}", ftpServerIp, Integer.parseInt(ftpServerPort), ftpServerAccount, ftpServerPassword);
|
||||
setClientCharacter();
|
||||
}
|
||||
}
|
||||
return ftp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void setClientCharacter() {
|
||||
FTPClient ftpClient = ftp.getClient();
|
||||
ftpClient.setControlKeepAliveTimeout(60);
|
||||
try {
|
||||
int command = ftpClient.sendCommand("OPTS UTF8", "ON");
|
||||
if (FTPReply.isPositiveCompletion(command)) {
|
||||
LOCAL_CHARSET = "UTF-8";
|
||||
}
|
||||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||||
} catch (Exception exception) {
|
||||
throw new RuntimeException("获取ftp服务编码格式异常:", exception.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean existFile(String filePath) {
|
||||
return instance().existFile(filePath);
|
||||
}
|
||||
|
||||
public boolean delFile(String filePath) {
|
||||
return instance().delFile(filePath);
|
||||
}
|
||||
|
||||
public boolean delDir(String dirPath) {
|
||||
return instance().delDir(dirPath);
|
||||
}
|
||||
|
||||
public boolean mkDir(String dirPath) {
|
||||
return instance().mkdir(dirPath);
|
||||
}
|
||||
|
||||
public List<FTPFile> lsFiles(String dirPath) {
|
||||
FTPFile[] ftpFiles = instance().lsFiles(dirPath);
|
||||
return Arrays.asList(ftpFiles);
|
||||
}
|
||||
|
||||
public boolean uploadFile(String path, String fileName, File file) {
|
||||
return instance().upload(path, fileName, file);
|
||||
}
|
||||
|
||||
public boolean uploadFile(String path, String fileName, InputStream inputStream) {
|
||||
return instance().upload(path, fileName, inputStream);
|
||||
}
|
||||
|
||||
public void download(String path, String fileName, OutputStream outputStream) {
|
||||
instance().download(path, fileName, outputStream);
|
||||
}
|
||||
|
||||
public boolean close() {
|
||||
try {
|
||||
if (ftp != null) {
|
||||
ftp.close();
|
||||
}
|
||||
return true;
|
||||
} catch (Exception exception) {
|
||||
logger.error("error close ftp: " + exception.getCause());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
<version>3.3.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<name>业务模块-爬虫模块</name>
|
||||
|
||||
<artifactId>xjs-business-webmagic</artifactId>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package com.xjs.handler;
|
||||
|
||||
import us.codecraft.webmagic.Page;
|
||||
import us.codecraft.webmagic.Site;
|
||||
import us.codecraft.webmagic.processor.PageProcessor;
|
||||
|
||||
/**
|
||||
* 米哈游社区爬取资源
|
||||
* @author xiejs
|
||||
* @since 2022-01-24
|
||||
*/
|
||||
public class MiHoYoRepoPageProcessor implements PageProcessor {
|
||||
|
||||
|
||||
private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
|
||||
|
||||
|
||||
@Override
|
||||
public void process(Page page) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Site getSite() {
|
||||
return site;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue