Commit ce3e7046 by xmh

部分开发

1 parent 87ad1849
......@@ -11,7 +11,7 @@
<groupId>com.viontech</groupId>
<artifactId>recv_data_longhua</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<packaging>jar</packaging>
<name>recv_data_longhua</name>
<description>recv_data_longhua</description>
......@@ -24,7 +24,22 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.viontech.keliu</groupId>
<artifactId>keliu-util</artifactId>
<version>6.0.7-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>maven-scm-provider-integrity</artifactId>
<groupId>org.apache.maven.scm</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
......@@ -32,23 +47,23 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<finalName>recv_data_longhua</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
......
......@@ -4,7 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@SpringBootApplication(exclude = org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class)
@Slf4j
public class Application {
......@@ -15,5 +15,4 @@ public class Application {
log.error("error", e);
}
}
}
package com.viontech.configuration;
import com.viontech.ftp.FTPClientFactory;
import com.viontech.ftp.FTPClientHelper;
import com.viontech.ftp.FTPClientPool;
import com.viontech.ftp.FtpPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FtpConfiguration {
@Value("${ftp.host:127.0.0.1}")
private String host;//主机名
@Value("${ftp.port:21}")
private int port;//端口
@Value("${ftp.username:}")
private String username;//用户名
@Value("${ftp.password:}")
private String password;//密码
@Value("${ftp.tempPath:}")
private String tempPath;
@Bean("ftpPoolConfig")
@ConditionalOnProperty(name = "ftp.host")
public FtpPoolConfig ftpPoolConfig() {
FtpPoolConfig ftpPoolConfig = new FtpPoolConfig();
ftpPoolConfig.setHost(host);
ftpPoolConfig.setPort(port);
ftpPoolConfig.setUsername(username);
ftpPoolConfig.setPassword(password);
//ftpPoolConfig.setMaxWaitMillis(1000);
//ftpPoolConfig.setMaxIdle(2);
return ftpPoolConfig;
}
@Bean("ftpClientFactory")
@ConditionalOnBean(FtpPoolConfig.class)
public FTPClientFactory ftpClientFactory(FtpPoolConfig ftpPoolConfig) {
FTPClientFactory ftpClientFactory = new FTPClientFactory();
ftpClientFactory.setFtpPoolConfig(ftpPoolConfig);
return ftpClientFactory;
}
@Bean("ftpClientPool")
@ConditionalOnBean(FTPClientFactory.class)
public FTPClientPool ftpClientPool(FTPClientFactory ftpClientFactory) {
FTPClientPool ftpClientPool = new FTPClientPool(ftpClientFactory);
return ftpClientPool;
}
@Bean("ftpClientHelper")
@ConditionalOnBean(FTPClientPool.class)
public FTPClientHelper ftpClientHelper(FTPClientPool ftpClientPool) {
FTPClientHelper ftpClientHelper = new FTPClientHelper();
ftpClientHelper.setFtpClientPool(ftpClientPool);
if (!tempPath.trim().isEmpty())
ftpClientHelper.setTempPath(tempPath);
return ftpClientHelper;
}
}
package com.viontech.controller;
import org.springframework.stereotype.Controller;
import com.viontech.process.Process;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
......@@ -12,5 +14,12 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class DataController {
@PostMapping("/data")
public Object receiveData(@RequestBody String dataStr) {
Process.process(dataStr);
return null;
}
}
package com.viontech.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import java.io.IOException;
/**
* ftpclient 工厂
*/
@Slf4j
public class FTPClientFactory extends BasePooledObjectFactory<FTPClient> {
private FtpPoolConfig ftpPoolConfig;
public FtpPoolConfig getFtpPoolConfig() {
return ftpPoolConfig;
}
public void setFtpPoolConfig(FtpPoolConfig ftpPoolConfig) {
this.ftpPoolConfig = ftpPoolConfig;
}
/**
* 新建对象
*/
@Override
public FTPClient create() throws Exception {
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(ftpPoolConfig.getConnectTimeOut());
try {
log.info("连接ftp服务器:" + ftpPoolConfig.getHost() + ":" + ftpPoolConfig.getPort());
ftpClient.connect(ftpPoolConfig.getHost(), ftpPoolConfig.getPort());
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
log.error("FTPServer 拒绝连接");
return null;
}
boolean result = ftpClient.login(ftpPoolConfig.getUsername(), ftpPoolConfig.getPassword());
if (!result) {
throw new Exception("ftpClient登录失败! userName:" + ftpPoolConfig.getUsername() + ", password:"
+ ftpPoolConfig.getPassword());
}
ftpClient.setControlEncoding(ftpPoolConfig.getControlEncoding());
ftpClient.setBufferSize(ftpPoolConfig.getBufferSize());
ftpClient.setFileType(ftpPoolConfig.getFileType());
ftpClient.setDataTimeout(ftpPoolConfig.getDataTimeout());
if (ftpPoolConfig.isPassiveMode()) {
log.info("进入ftp被动模式");
ftpClient.enterLocalPassiveMode();//进入被动模式
}
} catch (IOException e) {
log.error("FTP连接失败:", e);
}
return ftpClient;
}
@Override
public PooledObject<FTPClient> wrap(FTPClient ftpClient) {
return new DefaultPooledObject<FTPClient>(ftpClient);
}
/**
* 销毁对象
*/
@Override
public void destroyObject(PooledObject<FTPClient> p) throws Exception {
FTPClient ftpClient = p.getObject();
try {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();
}
} catch (IOException io) {
io.printStackTrace();
} finally {
// 注意,一定要在finally代码中断开连接,否则会导致占用ftp连接情况
try {
ftpClient.disconnect();
} catch (IOException io) {
io.printStackTrace();
}
}
}
/**
* 验证对象
*/
@Override
public boolean validateObject(PooledObject<FTPClient> p) {
FTPClient ftpClient = p.getObject();
boolean connect = false;
try {
connect = ftpClient.sendNoOp();
} catch (IOException e) {
e.printStackTrace();
}
return connect;
}
/**
* No-op.
*
* @param p ignored
*/
@Override
public void activateObject(PooledObject<FTPClient> p) throws Exception {
FTPClient ftpClient = p.getObject();
if (!validateObject(p)) {
ftpClient.reinitialize();
}
if (!validateObject(p)) {
throw new RuntimeException("ftp已经失效");
}
}
/**
* No-op.
*
* @param p ignored
*/
@Override
public void passivateObject(PooledObject<FTPClient> p) throws Exception {
}
}
\ No newline at end of file
package com.viontech.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import java.io.*;
/**
* ftp客户端辅助bean
*/
@Slf4j
public class FTPClientHelper {
private FTPClientPool ftpClientPool;
private String tempPath;
private int retryCount = 3;
public void setFtpClientPool(FTPClientPool ftpClientPool) {
this.ftpClientPool = ftpClientPool;
}
/**
* 下载 remote文件流
*
* @param remote 远程文件
*
* @return 字节数据
*/
public byte[] retrieveFileStream(String remote) {
FTPClient client = null;
ByteArrayOutputStream out = null;
try {
log.info("开始获取FTPClient对象");
client = ftpClientPool.borrowObject();
log.info("结束获取FTPClient对象");
out = new ByteArrayOutputStream();
log.info("开始获取文件");
boolean result = client.retrieveFile(remote, out);
log.info("结束获取文件" + result);
if (result) {
return out.toByteArray();
}
} catch (Exception e) {
e.printStackTrace();
log.error("下载文件失败", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClientPool.returnObject(client);
}
return null;
}
/**
* 创建目录 单个不可递归
*
* @param pathname 目录名称
*
* @return
*/
public boolean makeDirectory(String pathname) throws Exception {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
return client.makeDirectory(pathname);
} finally {
ftpClientPool.returnObject(client);
}
}
/**
* 删除目录,单个不可递归
*
* @param pathname
*
* @return
*/
public boolean removeDirectory(String pathname) throws Exception {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
return client.removeDirectory(pathname);
} finally {
ftpClientPool.returnObject(client);
}
}
/**
* 删除文件 单个 ,不可递归
*
* @param pathname
*
* @return
*/
public boolean deleteFile(String pathname) throws Exception {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
return client.deleteFile(pathname);
} finally {
ftpClientPool.returnObject(client);
}
}
/**
* 上传文件
*
* @param remote
* @param local
*
* @return
*/
public boolean storeFile(String remote, InputStream local) {
Exception throwException = null;
for (int i = 0; i < retryCount; i++) { // 尝试三次 如果三次不成功那么存储到本地
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
boolean result = client.storeFile(remote, local);
if (result) {
return result;
}
} catch (Exception e) {
e.printStackTrace();
throwException = e;
} finally {
if (local != null) {
try {
local.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClientPool.returnObject(client);
}
}
log.error("通过ftp存储文件" + remote + "时发生异常 ,开始尝试存储到本地路径" + tempPath + "下", throwException);
return storeTemp(remote, local);
}
public boolean isExist(String remote) {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
int count = client.list(remote);
if (count == 0) {
return false;
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ftpClientPool.returnObject(client);
}
return false;
}
public boolean storeFile(String remote, byte[] content) {
Exception throwException = null;
for (int i = 0; i < retryCount; i++) { // 尝试三次 如果三次不成功那么存储到本地
Long startTime = System.currentTimeMillis();
FTPClient client = null;
ByteArrayInputStream bis = null;
try {
client = ftpClientPool.borrowObject();
String parentPath = getParentPath(remote);
boolean makeDirectoryResult = client.makeDirectory(parentPath);
if (!makeDirectoryResult) {//如果创建失败 可能是不支持一次创建多级目录
makeDirectorys(client, parentPath);
}
bis = new ByteArrayInputStream(content);
boolean result = client.storeFile(remote, bis);
if (result) {
return result;
} else {
log.error("通过ftp存储文件" + remote + "时未发生异常 ,但存储失败");
return result;
}
} catch (Exception e) {
e.printStackTrace();
throwException = e;
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClientPool.returnObject(client);
Long endTime = System.currentTimeMillis();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
log.error("通过ftp存储文件" + remote + "时发生异常 ,开始尝试存储到本地路径" + tempPath + "下", throwException);
return storeTemp(remote, content);
}
private boolean makeDirectorys(FTPClient ftpclient, String remote) throws Exception {
File file = new File(remote);
if (file.getPath().indexOf(File.separator) == -1) { //是根路径
ftpclient.makeDirectory(file.getPath());
} else {
makeDirectorys(ftpclient, file.getParent());
ftpclient.makeDirectory(file.getPath());
}
return true;
}
private boolean storeTemp(String remote, InputStream local) {
FileOutputStream fos = null;
try {
File file = new File(tempPath, remote);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
int bytesWritten = 0;
int byteCount = 0;
byte[] bytes = new byte[1024];
while ((byteCount = local.read(bytes)) != -1) {
fos.write(bytes, bytesWritten, byteCount);
bytesWritten += byteCount;
}
return true;
} catch (Exception e) {
log.error("存储到本地路径" + tempPath + "下失败", e);
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (local != null) {
local.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private boolean storeTemp(String remote, byte[] content) {
FileOutputStream fos = null;
try {
File file = new File(tempPath, remote);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
fos.write(content);
fos.flush();
return true;
} catch (Exception e) {
log.error("存储到本地路径" + tempPath + "下失败", e);
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public InputStream getInputStream(FileInputStream fileInput) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = -1;
InputStream inputStream = null;
try {
while ((n = fileInput.read(buffer)) != -1) {
baos.write(buffer, 0, n);
}
byte[] byteArray = baos.toByteArray();
inputStream = new ByteArrayInputStream(byteArray);
return inputStream;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String getParentPath(String remote) {
File f = new File(remote);
return f.getParent();
}
public String getTempPath() {
return tempPath;
}
public void setTempPath(String tempPath) {
this.tempPath = tempPath;
}
public boolean deleteItem(String remote) {
FTPClient client = null;
try {
client = ftpClientPool.borrowObject();
} catch (Exception e) {
e.printStackTrace();
}
boolean isDelete = false;
try {
isDelete = client.deleteFile(remote);
} catch (IOException e) {
e.printStackTrace();
}
ftpClientPool.returnObject(client);
return isDelete;
}
}
\ No newline at end of file
package com.viontech.ftp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;
import java.io.IOException;
/**
* FTP 客户端连接池
*/
@Slf4j
public class FTPClientPool {
/**
* ftp客户端连接池
*/
private GenericObjectPool<FTPClient> pool;
/**
* ftp客户端工厂
*/
private FTPClientFactory clientFactory;
/**
* 构造函数中 注入一个bean
*
* @param clientFactory
*/
public FTPClientPool(FTPClientFactory clientFactory) {
this.clientFactory = clientFactory;
pool = new GenericObjectPool<FTPClient>(clientFactory, clientFactory.getFtpPoolConfig());
}
public FTPClientFactory getClientFactory() {
return clientFactory;
}
public GenericObjectPool<FTPClient> getPool() {
return pool;
}
/**
* 借 获取一个连接对象
*
* @return
*/
public FTPClient borrowObject() throws Exception {
FTPClient client = pool.borrowObject();
boolean valid = true;
try {
if (client.isConnected()) {
valid = client.sendNoOp();
} else {
valid = false;
}
} catch (IOException e) {
log.error("什么情况 刚刚获取的对象就不可用,", e);
e.printStackTrace();
valid = false;
}
if (!valid) {
//使池中的对象无效
try {
client.logout();
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
pool.invalidateObject(client);
}
return client;
}
/**
* 还 归还一个连接对象
*
* @param ftpClient
*/
public void returnObject(FTPClient ftpClient) {
if (ftpClient != null) {
try {
pool.returnObject(ftpClient);
} catch (Exception e) {
log.error("将FTP归还到FTP池中时发生异常", e);
}
}
}
}
\ No newline at end of file
package com.viontech.ftp;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
/**
* ftp配置参数对象 继承自GenericObjectPoolConfig
*/
@Getter
@Setter
public class FtpPoolConfig extends GenericObjectPoolConfig {
private String host;
private int port;
private String username;
private String password;
/** ftp 连接超时时间 毫秒 */
private int connectTimeOut = 6000000;
private String controlEncoding = "utf-8";
/** 缓冲区大小 */
private int bufferSize = 1024;
/** 传输数据格式 2表binary二进制数据 */
private int fileType = 2;
private int dataTimeout = 120000;
private boolean useEPSVwithIPv4 = false;
/** 是否启用被动模式 */
private boolean passiveMode = true;
private String tempPath = System.getProperty("java.io.tmpdir");
}
package com.viontech.model;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
/**
* .
*
* @author 谢明辉
* @date 2020/8/20
*/
@Getter
@Setter
public abstract class BaseModel {
protected Long empty;
protected Long length;
protected Long firstInt;
protected Long secondInt;
protected byte[] data;
/**
* 将byteBuf 转换为 model
*
* @param byteBuf byteBuf
*
* @return model
*/
abstract public BaseModel decode(ByteBuf byteBuf);
/**
* 将 model 转换为 byteBuf
*
* @return byteBuf
*/
abstract public ByteBuf encode();
}
package com.viontech.model;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
/**
* .
*
* @author 谢明辉
* @date 2020/8/20
*/
@Getter
@Setter
public class LoginData extends BaseModel {
private String username;
private String password;
@Override
public BaseModel decode(ByteBuf byteBuf) {
return null;
}
@Override
public ByteBuf encode() {
return null;
}
}
package com.viontech.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* .
*
* @author 谢明辉
* @date 2020/8/18
*/
@Component
@Slf4j
public class NettyServer implements CommandLineRunner {
@Value("${netty.port:30001}")
private int port;
@Override
public void run(String[] args) {
EventLoopGroup workerGroup = new NioEventLoopGroup(30);
EventLoopGroup bossGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b = b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024 * 1000, 4, 4, -16, 0));
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
log.info("通道构建完毕");
b.bind(port).addListener(future -> {
if (future.isSuccess()) {
log.info("端口[{}]绑定成功", port);
} else {
log.info("端口[{}]绑定失败", port);
}
});
} catch (Exception e) {
log.error("", e);
}
}
}
package com.viontech.process;
/**
* .
*
* @author 谢明辉
* @date 2020/8/20
*/
public class Process {
public static void process(String jsonStr) {
}
}
netty.port=30001
ftp.host=
ftp.port=
ftp.username=
ftp.password=
\ No newline at end of file
spring:
profiles:
active: config
application:
name: revc_data_longhua
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
server:
port: 30000
\ No newline at end of file
package com.viontech;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RecvDataLonghuaApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!