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