Excel解析上传入库【POI 和 EasyPoi】

本文介绍了如何使用POI和EasyPOI处理前端上传的Excel文件,将其上传到FastDFS或FTP服务器,并解析后存入数据库。内容包括FastDFS与FTP的配置、依赖引入、DTO创建、文件上传及解析入库的方法。
摘要由CSDN通过智能技术生成

目录

一、上传Excel至FastDFS

二、上传文件至FTP使用EasyPOI

三、上传文件至FTP使用POI。

四:补充一下FTP以及FastDFS配置


目前的需求:

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);
                }
            }
        }
    }

    /**
     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值