mirror of
https://gitee.com/nutz/nutzboot.git
synced 2024-11-30 02:38:28 +08:00
add: nutzboot-starter-ftp FTP客户端
This commit is contained in:
parent
2c6542ba87
commit
c04ef24733
58
nutzboot-starter/nutzboot-starter-ftp/pom.xml
Normal file
58
nutzboot-starter/nutzboot-starter-ftp/pom.xml
Normal file
@ -0,0 +1,58 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>nutzboot-starter</artifactId>
|
||||
<groupId>org.nutz</groupId>
|
||||
<version>2.3.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>nutzboot-starter-ftp</artifactId>
|
||||
<description>NutzBoot, micoservice base on Nutz</description>
|
||||
<url>https://nutzam.com</url>
|
||||
<issueManagement>
|
||||
<system>Github Issue</system>
|
||||
<url>https://github.com/nutzam/nutzboot/issues</url>
|
||||
</issueManagement>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>https://apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
</license>
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer>
|
||||
<id>wizzercn</id>
|
||||
<name>Wizzer</name>
|
||||
<email>wizzer.cn@gmail.com</email>
|
||||
<url>https://wizzer.cn</url>
|
||||
</developer>
|
||||
</developers>
|
||||
<scm>
|
||||
<connection>scm:git:git://github.com/nutzam/nutzboot.git</connection>
|
||||
<developerConnection>scm:git:git://github.com/nutzam/nutzboot.git</developerConnection>
|
||||
<url>git://github.com/nutzam/nutzboot.git</url>
|
||||
</scm>
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>nutzcn-snapshots</id>
|
||||
<name>NutzCN snapshot repository</name>
|
||||
<url>https://jfrog.nutz.cn/artifactory/snapshots</url>
|
||||
</snapshotRepository>
|
||||
|
||||
<repository>
|
||||
<id>sonatype-release-staging</id>
|
||||
<name>Sonatype Nexus release repository</name>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-net</groupId>
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>${commons.net.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,206 @@
|
||||
package org.nutz.boot.starter.ftp;
|
||||
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.log.Log;
|
||||
import org.nutz.log.Logs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@IocBean
|
||||
public class FtpService {
|
||||
private static final Log log = Logs.get();
|
||||
private static String host;
|
||||
private static int port;
|
||||
private static String username;
|
||||
private static String password;
|
||||
private static int timeout;
|
||||
private static String LOCAL_CHARSET = "UTF-8";
|
||||
// FTP协议里面,规定文件名编码为iso-8859-1
|
||||
private static String SERVER_CHARSET = "ISO-8859-1";
|
||||
|
||||
public FTPClient connect() {
|
||||
FTPClient ftpClient = null;
|
||||
try {
|
||||
ftpClient = new FTPClient();
|
||||
ftpClient.connect(host, port);
|
||||
//登录服务器
|
||||
ftpClient.login(username, password);
|
||||
//判断返回码是否合法
|
||||
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
|
||||
//不合法时断开连接
|
||||
ftpClient.disconnect();
|
||||
log.info("[FtpService] FTP用户名或密码错误");
|
||||
return null;
|
||||
} else {
|
||||
log.info("[FtpService] FTP连接成功");
|
||||
}
|
||||
//设置被动模式
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
//设置文件类型
|
||||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
//设置超时时间
|
||||
ftpClient.setDefaultTimeout(timeout * 1000);
|
||||
//设置缓冲区大小
|
||||
ftpClient.setBufferSize(3072);
|
||||
//设置字符编码
|
||||
ftpClient.sendCommand("OPTS UTF8", "ON");
|
||||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||||
} catch (IOException e) {
|
||||
log.error("[FtpService] FTP配置错误,请检查配置");
|
||||
}
|
||||
return ftpClient;
|
||||
}
|
||||
|
||||
public boolean upload(String filePath, String fileName, InputStream input) {
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = null;
|
||||
try {
|
||||
ftpClient = connect();
|
||||
if (ftpClient == null) {
|
||||
return false;
|
||||
}
|
||||
// 切换到上传目录
|
||||
if (!ftpClient.changeWorkingDirectory(filePath)) {
|
||||
// 如果目录不存在创建目录
|
||||
String[] dirs = filePath.split("/");
|
||||
String tempPath = "";
|
||||
for (String dir : dirs) {
|
||||
if (null == dir || "".equals(dir))
|
||||
continue;
|
||||
tempPath += "/" + dir;
|
||||
if (!ftpClient.changeWorkingDirectory(tempPath)) {
|
||||
if (!ftpClient.makeDirectory(tempPath)) {
|
||||
return result;
|
||||
} else {
|
||||
ftpClient.changeWorkingDirectory(tempPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 上传文件
|
||||
fileName = new String(fileName.getBytes(LOCAL_CHARSET), SERVER_CHARSET);
|
||||
if (!ftpClient.storeFile(fileName, input)) {
|
||||
return result;
|
||||
}
|
||||
result = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (null != input) {
|
||||
try {
|
||||
// 关闭输入流
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
//退出登录
|
||||
ftpClient.logout();
|
||||
//关闭连接
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean delete(String filePath, String fileName) {
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = null;
|
||||
try {
|
||||
ftpClient = connect();
|
||||
if (ftpClient == null) {
|
||||
return false;
|
||||
}
|
||||
fileName = new String(fileName.getBytes(LOCAL_CHARSET), SERVER_CHARSET);
|
||||
ftpClient.changeWorkingDirectory(filePath);
|
||||
ftpClient.dele(fileName);
|
||||
result = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
//退出登录
|
||||
ftpClient.logout();
|
||||
//关闭连接
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public InputStream download(String fileNameHasPath) {
|
||||
FTPClient ftpClient = null;
|
||||
try {
|
||||
ftpClient = connect();
|
||||
if (ftpClient == null) {
|
||||
return null;
|
||||
}
|
||||
fileNameHasPath = new String(fileNameHasPath.getBytes(LOCAL_CHARSET), SERVER_CHARSET);
|
||||
InputStream inputStream = ftpClient.retrieveFileStream(fileNameHasPath);
|
||||
ftpClient.completePendingCommand();
|
||||
return inputStream;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
//退出登录
|
||||
ftpClient.logout();
|
||||
//关闭连接
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public static int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public static void setTimeout(int timeout) {
|
||||
FtpService.timeout = timeout;
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package org.nutz.boot.starter.ftp;
|
||||
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.nutz.boot.annotation.PropDoc;
|
||||
import org.nutz.boot.starter.ServerFace;
|
||||
import org.nutz.ioc.impl.PropertiesProxy;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.log.Log;
|
||||
import org.nutz.log.Logs;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@IocBean
|
||||
public class FtpStarter implements ServerFace {
|
||||
private static final Log log = Logs.get();
|
||||
private static final String PRE = "ftp.";
|
||||
@Inject
|
||||
protected PropertiesProxy conf;
|
||||
@PropDoc(value = "是否启动FTP客户端", defaultValue = "false", type = "boolean")
|
||||
public static final String PROP_ENABLED = PRE + "enabled";
|
||||
|
||||
@PropDoc(value = "FTP地址", defaultValue = "", type = "string")
|
||||
public static final String PROP_SERVER_HOST = PRE + "host";
|
||||
|
||||
@PropDoc(value = "FTP端口", defaultValue = "21", type = "int")
|
||||
public static final String PROP_SERVER_PORT = PRE + "port";
|
||||
|
||||
@PropDoc(value = "FTP用户名", defaultValue = "", type = "string")
|
||||
public static final String PROP_SERVER_USERNAME = PRE + "username";
|
||||
|
||||
@PropDoc(value = "FTP用户密码", defaultValue = "", type = "string")
|
||||
public static final String PROP_SERVER_PASSWORD = PRE + "password";
|
||||
|
||||
@PropDoc(value = "FTP超时时间", defaultValue = "30", type = "int")
|
||||
public static final String PROP_SERVER_TIMEOUT = PRE + "timeout";
|
||||
|
||||
@Inject
|
||||
private FtpService ftpService;
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
if (conf.getBoolean(PROP_ENABLED, false)) {
|
||||
ftpService.setHost(conf.get(PROP_SERVER_HOST, ""));
|
||||
ftpService.setPort(conf.getInt(PROP_SERVER_PORT, 21));
|
||||
ftpService.setUsername(conf.get(PROP_SERVER_USERNAME, ""));
|
||||
ftpService.setPassword(conf.get(PROP_SERVER_PASSWORD, ""));
|
||||
ftpService.setTimeout(conf.getInt(PROP_SERVER_TIMEOUT, 30));
|
||||
//连接一下测试是否配置正确
|
||||
FTPClient ftpClient = ftpService.connect();
|
||||
if (ftpClient != null) {
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
//退出登录
|
||||
ftpClient.logout();
|
||||
//关闭连接
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return conf.getBoolean(PROP_ENABLED, false);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.nutz.boot.starter.ftp.FtpStarter
|
@ -72,7 +72,8 @@
|
||||
<module>nutzboot-starter-logback-exts</module>
|
||||
<module>nutzboot-starter-sentinel-annotation</module>
|
||||
<module>nutzboot-starter-tio-websocket</module>
|
||||
<module>nutzboot-starter-nacos-config-client</module>
|
||||
<module>nutzboot-starter-nacos-config-client</module>
|
||||
<module>nutzboot-starter-ftp</module>
|
||||
</modules>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
1
pom.xml
1
pom.xml
@ -42,6 +42,7 @@
|
||||
<quartz.version>2.3.0</quartz.version>
|
||||
<elasticsearch.version>6.3.2</elasticsearch.version>
|
||||
<commons.email.version>1.4</commons.email.version>
|
||||
<commons.net.version>3.3</commons.net.version>
|
||||
<javassist.version>3.24.0-GA</javassist.version>
|
||||
</properties>
|
||||
<description>NutzBoot, micoservice base on Nutz</description>
|
||||
|
Loading…
Reference in New Issue
Block a user