目录
目前的需求:
1、将前端上传的Excel文件上传至FTP 服务器。【也可以上传至FastDFS】
2、解析Excel文件并存入数据库。【需要统计文件大小以及内部总金额】
3、入库后定时根据数据库中的表数据进行支付打款。【可以通过分布式定时任务或者MQ】
我们这里主要说Excel的解析上传及入库。
一、上传Excel至FastDFS
由于是SpringBoot项目,我们要在yml文件中添加如下配置:
fdfs:
confPath: /src/main/resources/fdfs/dev/fdfs_client.conf
minPoolSize: 5
maxPoolSize: 10
waitTimes: 20
fileServerHost: 10.1.1.1
pom.xml文件中添加:
<!-- FastDFS-->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>
FastDFS连接池类:
/**
* @author xuyang
* @date 2019/07/18
*/
public class FastDFSConnectionPool {
private static final Logger log = LoggerFactory.getLogger(FastDFSConnectionPool.class);
/**
* 空闲的连接池
*/
private LinkedBlockingQueue<TrackerServer> idleFastDFSConnectionPool = null;
/**
* 连接池默认最小连接数
*/
private long minPoolSize;
/**
* 连接池默认最大连接数
*/
private long maxPoolSize;
/**
* 默认等待时间(单位:秒)
*/
private long waitTimes;
/**
* fastdfs客户端创建连接默认1次
*/
private static final int COUNT = 3;
/**
* 重试次数
*/
private static final int RE_TRIE_TIME = 5;
/**
* 配置文件的路径
*/
private String confPath;
/**
* 当前创建的连接数
*/
private volatile long nowPoolSize = 0;
/**
* 默认构造方法
*/
public FastDFSConnectionPool(long minPoolSize, long maxPoolSize, long waitTimes) {
}
public FastDFSConnectionPool(){
}
/**
* * @Description: 连接池初始化 (在加载当前FastDFSConnectionPool时执行) 1).加载配置文件 * 2).空闲连接池初始化; 3).创建最小连接数的连接,并放入到空闲连接池; *
*/
public void poolInit() {
try {
/*加载配置文件 */
initClientGlobal();
/* 初始化空闲连接池 */
idleFastDFSConnectionPool = new LinkedBlockingQueue<TrackerServer>();
/* 往线程池中添加默认大小的线程 */
for (int i = 0; i < minPoolSize; i++) {
createTrackerServer(COUNT);
}
log.info("[线程池初始化方法(FastDFSConnectionPool)][" + "][默认参数:minPoolSize=" + minPoolSize + ",maxPoolSize=" + maxPoolSize + ",waitTimes=" + waitTimes + "]成功");
} catch (Exception e) {
log.error("[线程池初始化方法(FastDFSConnectionPool)][" + "][默认参数:minPoolSize=" + minPoolSize + ",maxPoolSize=" + maxPoolSize + ",waitTimes=" + waitTimes + "]失败",e);
}
}
/**
* * @Description: 创建TrackerServer,并放入空闲连接池 *
*/
protected void createTrackerServer(int flag) {
TrackerServer trackerServer = null;
try {
TrackerClient trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
while (trackerServer == null && flag < RE_TRIE_TIME ) {
log.info("[创建TrackerServer(createTrackerServer)][第" + flag + "次重建]");
flag++;
initClientGlobal();
trackerServer = trackerClient.getConnection();
}
// assert trackerServer != null;
if(trackerServer!=null){
org.csource.fastdfs.ProtoCommon.activeTest(trackerServer.getSocket());
idleFastDFSConnectionPool.add(trackerServer);
// 同一时间只允许一个线程对nowPoolSize操作
synchronized (this) {
nowPoolSize++;
}
}else{
throw new NullPointerException("trackerServer为空");
}
} catch (Exception e) {
log.error("[创建TrackerServer(createTrackerServer)][异常:{}]", e);
} finally {
if (trackerServer != null) {
try {
trackerServer.close();
} catch (Exception e) {
log.error("[创建TrackerServer(createTrackerServer)--关闭trackerServer异常][异常:{}]", e);
}
}
}
}
/**