wanghc
2023-03-09 f429658e8bf3c88c4550171f61d6984033940b54
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/services/impl/BillServiceImpl.java
@@ -1,18 +1,25 @@
package com.jttech.pfcs.services.impl;
import java.io.File;
import java.util.concurrent.TimeUnit;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import com.jttech.pfcs.util.SftpUtil;
import com.jttech.pfcs.vo.req.bill.FtpServerParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.jttech.pfcs.services.IBillService;
import com.jttech.pfcs.vo.ResponseVo;
import com.jttech.pfcs.vo.resp.ResponseVo;
import com.jttech.pfcs.vo.req.bill.BillApiReqVo;
import com.jttech.pfcs.vo.req.bill.BillApiRespVo;
import com.jttech.pfcs.vo.resp.bill.BillApiRespVo;
import okhttp3.*;
import org.springframework.web.multipart.MultipartFile;
/**
 * 账单服务
@@ -62,4 +69,47 @@
            return ResponseVo.fail(-999, errMsg);
        }
    }
    /**
     * 文件上传
     *
     * @param files
     * @param ftpServer
     * @return
     */
    @Override
    public ResponseVo fileUpload(MultipartFile[] files, FtpServerParam ftpServer) {
        Session session = null;
        ChannelSftp sftpChannel = null;
        try {
            session = SftpUtil.getSession(ftpServer.getIp(), ftpServer.getUser(), ftpServer.getPwd(), ftpServer.getPort());
            sftpChannel = SftpUtil.openChannel(session);
            //文件目录没有就需要创建
            String saveDirPath = ftpServer.getSaveDirPath();
            File targetDirFile = new File(saveDirPath);
            if (!targetDirFile.exists()) {
                if (!targetDirFile.mkdirs()) {
                    // 创建处理目录失败
                    throw new Exception("创建文件目录失败");
                }
            }
            SftpUtil.cd(sftpChannel, saveDirPath);
            for (MultipartFile file : files) {
                String fileName = file.getOriginalFilename();
                //放入文件
                SftpUtil.put(sftpChannel, file.getBytes(), fileName);
            }
        } catch (Exception e) {
            mLogger.error("BillServiceImpl.fileUpload err={} server={}", e, ftpServer);
            throw new RuntimeException(e.getMessage());
        } finally {
            if (null != sftpChannel) {
                sftpChannel.disconnect();
            }
            if (null != session) {
                session.disconnect();
            }
        }
        return ResponseVo.ok();
    }
}