wanghc
2023-03-09 f429658e8bf3c88c4550171f61d6984033940b54
浦发分行系统文件上传
3 files added
6 files modified
347 ■■■■■ changed files
cmci-pfcs-gateway/pom.xml 6 ●●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/conf/GlobalExceptionHandler.java 29 ●●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/controller/BillController.java 21 ●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/services/IBillService.java 10 ●●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/services/impl/BillServiceImpl.java 50 ●●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/util/SftpUtil.java 115 ●●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/vo/req/bill/FtpServerParam.java 106 ●●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/vo/resp/ResponseVo.java 4 ●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/src/main/resources/application.properties 6 ●●●● patch | view | raw | blame | history
cmci-pfcs-gateway/pom.xml
@@ -49,6 +49,12 @@
            <artifactId>okhttp</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!-- jcraft sftp -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/conf/GlobalExceptionHandler.java
New file
@@ -0,0 +1,29 @@
package com.jttech.pfcs.conf;
import com.jttech.pfcs.vo.resp.ResponseVo;
import org.slf4j.Logger;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
 * 全域异常处理
 * @author wanghc
 * @version 1.0.0
 * @date 2023-03-09
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseVo exceptionHandler(HttpServletRequest request, Exception exception) {
        String errMsg = StringUtils.isEmpty(exception.getMessage()) ? "服务异常" : exception.getMessage();
        //截取前面20个字符
        errMsg = errMsg.substring(0,Math.min(20, errMsg.length()));
        return ResponseVo.fail(-999, errMsg);
    }
}
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/controller/BillController.java
@@ -2,15 +2,14 @@
import com.jttech.pfcs.services.IBillService;
import com.jttech.pfcs.vo.req.bill.BillApiReqVo;
import com.jttech.pfcs.vo.req.bill.FtpServerParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.jttech.pfcs.vo.resp.ResponseVo;
import org.springframework.web.multipart.MultipartFile;
/**
 * 账单
@@ -34,7 +33,19 @@
            return mBillService.post(reqVo);
        } finally {
            final long endTime = System.currentTimeMillis();
            mLogger.info("Execute heart the result is {} time spent is {} ", result, (endTime - beginTime));
            mLogger.info("Execute post the result is {} time spent is {} ", result, (endTime - beginTime));
        }
    }
    @RequestMapping(value = "/fileUpload")
    public ResponseVo fileUpload(MultipartFile[] files, FtpServerParam ftpServer) {
        final long beginTime = System.currentTimeMillis();
        ResponseVo result = new ResponseVo();
        try {
            return mBillService.fileUpload(files, ftpServer);
        } finally {
            final long endTime = System.currentTimeMillis();
            mLogger.info("Execute fileUpload the result is {} time spent is {} ", result, (endTime - beginTime));
        }
    }
}
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/services/IBillService.java
@@ -1,7 +1,9 @@
package com.jttech.pfcs.services;
import com.jttech.pfcs.vo.req.bill.FtpServerParam;
import com.jttech.pfcs.vo.resp.ResponseVo;
import com.jttech.pfcs.vo.req.bill.BillApiReqVo;
import org.springframework.web.multipart.MultipartFile;
/**
 * @author wanghc
@@ -16,4 +18,12 @@
     * @return
     */
    ResponseVo post(BillApiReqVo reqVo);
    /**
     * 文件上传
     * @param files
     * @param pFtpServerParam
     * @return
     */
    ResponseVo fileUpload(MultipartFile[] files, FtpServerParam pFtpServerParam);
}
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/services/impl/BillServiceImpl.java
@@ -1,9 +1,15 @@
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;
@@ -13,6 +19,7 @@
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();
    }
}
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/util/SftpUtil.java
New file
@@ -0,0 +1,115 @@
package com.jttech.pfcs.util;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
/**
 * sftp
 *
 * @author: yan xu
 * @version: 1.0, 2017年12月21日
 */
public class SftpUtil {
    public static Session getSession(String pIp, String pUser, String pPsw, int pPort) throws Exception {
        Session session = null;
        JSch jsch = new JSch();
        if (pPort <= 0) {
            // 连接服务器,采用默认端口
            session = jsch.getSession(pUser, pIp);
        } else {
            // 采用指定的端口连接服务器
            session = jsch.getSession(pUser, pIp, pPort);
        }
        // 设置登陆主机的密码
        session.setPassword(pPsw);// 设置密码
        // 设置第一次登陆的时候提示,可选值:(ask | yes | no)
        session.setConfig("StrictHostKeyChecking", "no");
        // 设置登陆超时时间
        session.connect(300000);
        return session;
    }
    public static ChannelSftp openChannel(Session pSession) throws Exception {
        ChannelSftp channel = (ChannelSftp) pSession.openChannel("sftp");
        channel.connect(10000000);
        return channel;
    }
    public static void cd(ChannelSftp pSftp, String pPath) throws Exception {
        String[] folders = pPath.split("/");
        boolean isFirst = true;
        for (int i = 0; i < folders.length; i++) {
            String folder = folders[i];
            if (folder == null || folder.length() == 0) {
                continue;
            }
            if (isFirst) {
                folder = "/" + folder;
                isFirst = false;
            }
            try {
                pSftp.cd(folder);
            } catch (SftpException e) {
                pSftp.mkdir(folder);
                pSftp.cd(folder);
            }
        }
    }
    public static void put(ChannelSftp pSftp, byte[] pFileBytes, String pPath) throws Exception {
        InputStream inputs = new ByteArrayInputStream(pFileBytes);
        pSftp.put(inputs, pPath);
    }
    public static void put(ChannelSftp pSftp, File pFile, String pPath) throws Exception {
        FileInputStream inputStream = new FileInputStream(pFile);
        try {
            pSftp.put(inputStream, pPath);
        } finally {
            inputStream.close();
        }
    }
    public static void get(ChannelSftp pSftp, String pDownloadFile, String pSaveDirectory) throws Exception {
        File dir = new File(pSaveDirectory);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String saveFile = pSaveDirectory + "/" + pDownloadFile;
        File file = new File(saveFile);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        pSftp.get(pDownloadFile, fileOutputStream);
        fileOutputStream.close();
    }
    public static List<String> listFiles(ChannelSftp pSftp, String pPath) throws Exception {
        List<String> result = new ArrayList<>();
        Vector ls = pSftp.ls(pPath);
        if (null != ls && ls.size() > 0) {
            for (Object l : ls) {
                ChannelSftp.LsEntry lsEntry = (ChannelSftp.LsEntry) l;
                String filename = lsEntry.getFilename();
                result.add(filename);
            }
        }
        return result;
    }
}
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/vo/req/bill/FtpServerParam.java
New file
@@ -0,0 +1,106 @@
package com.jttech.pfcs.vo.req.bill;
import java.io.Serializable;
/**
 * ftp服务参数
 *
 * @author wanghc
 * @version 1.0.0
 * @date 2023-03-09
 */
public class FtpServerParam implements Serializable {
    private static final long serialVersionUID = 9215027608162929560L;
    /**
     * ip
     */
    private String            ip;
    /**
     * 端口
     */
    private Integer           port;
    /**
     * 登录用户
     */
    private String            user;
    /**
     * 登录密码
     */
    private String            pwd;
    /**
     * 保存路径
     */
    private String            saveDirPath;
    public String getIp() {
        return ip;
    }
    public void setIp(String pIp) {
        ip = pIp;
    }
    public Integer getPort() {
        return port;
    }
    public void setPort(Integer pPort) {
        port = pPort;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String pUser) {
        user = pUser;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pPwd) {
        pwd = pPwd;
    }
    public String getSaveDirPath() {
        return saveDirPath;
    }
    public void setSaveDirPath(String pSaveDirPath) {
        saveDirPath = pSaveDirPath;
    }
    @Override
    public String toString() {
        return "FtpServerParam{" + "ip='" + ip + '\'' + ", port=" + port + ", user='" + user + '\'' + ", pwd='" + pwd
                + '\'' + ", saveDirPath='" + saveDirPath + '\'' + '}';
    }
}
cmci-pfcs-gateway/src/main/java/com/jttech/pfcs/vo/resp/ResponseVo.java
@@ -28,6 +28,10 @@
        return new ResponseVo(body);
    }
    public static ResponseVo ok() {
        return new ResponseVo();
    }
    public static ResponseVo fail(int errorCode, String errMsg) {
        return new ResponseVo(errorCode, errMsg, null);
    }
cmci-pfcs-gateway/src/main/resources/application.properties
@@ -1,8 +1,4 @@
spring.application.name=@pom.artifactId@
server.port=8400
server.servlet.context-path=/pf-api
logging.config=classpath:logback.xml
#### pf ¹ÒÏúÕËapi ############
pf.bill.trade.flow.qry.url=http://www.baidu.com
pf.bill.file.notice.url=http://www.baidu.com
logging.config=classpath:logback.xml