| | |
| | | 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; |
| | | |
| | | /** |
| | | * 账单服务 |
| | |
| | | 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(); |
| | | } |
| | | } |