wanghc
2023-03-09 f429658e8bf3c88c4550171f61d6984033940b54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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.resp.ResponseVo;
import com.jttech.pfcs.vo.req.bill.BillApiReqVo;
import com.jttech.pfcs.vo.resp.bill.BillApiRespVo;
 
import okhttp3.*;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * 账单服务
 * @author wanghc
 * @version 1.0.0
 * @date 2023-03-08
 */
@Service
public class BillServiceImpl implements IBillService {
 
    private Logger mLogger = LoggerFactory.getLogger(getClass());
 
    private static final String contentType = "application/json;charset=utf-8";
 
    /**
     * 请求编码格式
     */
    private static final String charset = "UTF-8";
 
 
    private static final OkHttpClient client = new OkHttpClient.Builder()
            .connectionPool(new ConnectionPool(500, 5, TimeUnit.MINUTES)).connectTimeout(10000, TimeUnit.MILLISECONDS)
            .readTimeout(60000, TimeUnit.MILLISECONDS).build();
 
    /**
     * post请求
     *
     * @param reqVo
     * @return
     */
    @Override
    public ResponseVo post(BillApiReqVo reqVo) {
        try {
            mLogger.info("BillServiceImpl.post reqvo={}", reqVo);
            Request request = new Request.Builder().url(reqVo.getUrl())
                    .post(RequestBody.create(MediaType.parse(contentType), JSONObject.toJSONString(reqVo)))
                    .addHeader("Content-Type", contentType)
                    .build();
            Response resp = client.newCall(request).execute();
            String body = new String(resp.body().bytes(), charset);
            mLogger.info("BillServiceImpl.post resp={}", body);
            return new ResponseVo(JSONObject.parseObject(body, BillApiRespVo.class));
        } catch (Exception e) {
            mLogger.error("BillServiceImpl.post error={}", e);
            String errMsg = e.getMessage() == null ? "接口调用异常" : e.getMessage();
            errMsg = errMsg.substring(0, Math.min(20, errMsg.length()));
            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();
    }
}