Remote connection sftp, download the csv file, read into the database

sftp tools:

package com.examstack.portal.util;

import com.jcraft.jsch.*;

import java.io.*;
import java.util.*;

public class SftpUtil {
    private ChannelSftp sftp = null;
    private Session sshSession = null;
    private String host;
    private String username;
    private String password;
    private int port;
    private String keyFilePath;
    private String passphrase;

    publicSftpUtil (Host String, String username, String password, int Port, KeyFilePath String, String a passphrase) {
         the this .host = Host;
         the this .username = username;
         the this .password = password;
         the this .port = Port;
         the this .keyFilePath = KeyFilePath ;
         the this .passphrase = a passphrase; 
    } 

    / ** 
     * sftp connection server 
     password connecting 
     * 
     * @return ChannelSftp sftp connection instance
      * / 
    public ChannelSftp connect () { 
        by JSch JSch =new JSch();
        try {
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties properties = new Properties();
            properties.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(properties);
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp =(ChannelSftp) Channel;
            info ( "FTP Connected to" Host + + ":" + Port); 
        } the catch (JSchException E) {
             the throw  new new a RuntimeException ( "sftp connection failed" , E); 
        } 
        return sftp; 
    } 

    / ** 
     * server connected sftp 
     * Yue-tight connection 
     * 
     * @return ChannelSftp SFTP connection instance
      * / 
    public ChannelSftp connectKey () { 
        by JSch JSch = new new by JSch (); 
        the Session the session = null ; 
        ChannelSftp Channel= Null ;
         the try { 
            JSch = new new by JSch ();
             IF (KeyFilePath =! Null ) {
                 IF (a passphrase =! Null ) { 
                    jsch.addIdentity (KeyFilePath, a passphrase); // set private 
                } the else { 
                    jsch.addIdentity (KeyFilePath ); // set private 
                } 
                System.out.println ( "connection sftp, the private key file path:" + KeyFilePath); 
            } 
            System.out.println ( "the SFTP the Host:" Host + + "; UserName:" + username);
            session = jsch.getSession(username, host, port);
            System.out.println("Session 已建立.");
            if (password != null) {
                session.setPassword(password);
            }
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            session.setConfig(sshConfig);
            session.setConfig("kex", "diffie-hellman-group1-sha1");
            session.connect();
            System.out.println ( "Session 已连接.");
            Channel = (ChannelSftp) session.openChannel ( "SFTP" ); 
            channel.connect (); 
            SFTP = (ChannelSftp) Channel; 
            System.out.println ( "successfully connected to the SFTP .host:" + Host ); 
        } the catch (Exception E) { 
            System.out.println ( "SFTP connection failed:" + E); 
        } 
        return SFTP; 
    } 

    / ** 
     * Download single file, if the specified file name, the file name or downloaded to the holding the original file name 
     * 
     * @param remoteFilePath remote file path || xxx.txt.zip /tmp/xxx.txt 
     * @paramlocalFilePath local file path such as D: \\ xxx.txt
     * @Return downloaded files
      * / 
    public File the downloadFile (remoteFilePath String, String localFilePath) { 
        connectKey (); 
        String remoteFileName = "" ;
         // distal determined directory / directory format as 
        String rFileSeparator = "/" ;
         int rDirNameSepIndex = remoteFilePath.lastIndexOf (rFileSeparator) +. 1 ; 
        String Rdir = remoteFilePath.substring (0 , rDirNameSepIndex); 
        remoteFileName = remoteFilePath.substring (rDirNameSepIndex);
         / * IF (localFilePath.endsWith (the File.separator)) {
            localFilePath = localFilePath + (localFilePath.endsWith(File.separator) ? remoteFileName : "/" + remoteFileName);
        }*/
        File file = null;
        File fileLocalPath = null;
        OutputStream output = null;
        try {
            fileLocalPath = new File(localFilePath);
            if (!fileLocalPath.exists()) {
                fileLocalPath.mkdirs();
            }
            file = new File(localFilePath + "/" + remoteFileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            sftp.cd(rDir);
            output = new FileOutputStream(file);
            sftp.get(remoteFileName, output);
            info("===DownloadFile:" + remoteFileName + " success from sftp.");
        } catch (SftpException e) {
            error("ftp下载文件失败", e);
        } catch (FileNotFoundException e) {
            error("Local directory abnormal, please check "+file.getPath (), E); 
        } the catch (IOException E) { 
            error ( "failed to create a local file" + file.getPath (), E); 
        } the finally {
             IF ! (Output = null ) {
                 the try { 
                    output.close (); 
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                } 
            } 
            the disconnect (); 
        } 

        info ( ">>>>>>>>> the downloadFile - FTP download file ends >>>>>>>> >>>>> " );
        return file;
    }

    /**
     * Upload a single file, if the correct name is used to download files, or retain the original file name 
     * 
     * @param remoteFilePath remote file path || xxx.txt.zip /tmp/xxx.txt 
     * @param file uploadFilePath to upload such as: D: \\ \\ xxx.txt Test
      * / 
    public  void the uploadFile (remoteFilePath String, String uploadFilePath) { 
        info ( "the begin from the uploadFile:" + uploadFilePath + 
                ", to:" + remoteFilePath); 
        the FileInputStream in = null ; 
        Connect ( ); 
        String remoteFileName = "" ; 
        String remotedir =  remoteFilePath;
        String LocalFileName = "";
        // 远端目录确定以 / 作为目录格式
        String rFileSeparator = "/";
        if (remoteFilePath.endsWith(rFileSeparator)) {
            localFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf(File.separator) + 1);
            remoteFileName = localFileName;
        } else {
            int fileNameDirSep = remoteFilePath.lastIndexOf(rFileSeparator) + 1;
            remoteDir = remoteFilePath.substring(0, fileNameDirSep);
            remoteFileName =remoteFilePath.substring (fileNameDirSep); 
        }
         {the try { 
            sftp.cd (remotedir); 
        } the catch (SftpException E) {
             the try { 
                sftp.mkdir (remotedir); 
                sftp.cd (remotedir); 
            } the catch (SftpException E1) { 
                error ( "FTP create file path fails, the path is "+ remotedir);
                 the throw  new new RuntimeException (" the FTP failed to create a file path "+ remotedir); 
            } 
        } 
        file file = new new file (uploadFilePath);
         the try 
            in = new new FileInputStream (file);
            sftp.put(in, remoteFileName);
        } catch (FileNotFoundException e) {
            error("文件不存在-->" + uploadFilePath);
        } catch (SftpException e) {
            error("sftp异常-->", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    info("Close stream error." + e.getMessage());
                }
            }
            the disconnect (); 
        } 
        info ( ">>>>>>>>> the uploadFile - FTP upload end >>>>>>>>>>>>>" ); 
    } 

    / ** 
     * close connection 
     * / 
    public  void the disconnect () {
         IF ( the this .sftp =! null ) {
             IF ( the this .sftp.isConnected ()) {
                 the this .sftp.disconnect ();
                 the this .sftp = null ; 
                info ( "SFTP connection was closed!" ) ;
            }
        }
        if (this.sshSession != null) {
            if (this.sshSession.isConnected()) {
                this.sshSession.disconnect();
                this.sshSession = null;
                info("sshSession 连接已关闭!");
            }
        }
    }

    private void info(String msg) {
        System.out.println("info: " + msg);
    }

    private void error(String msg) {
        error(msg, null);
    }

    private void error(String msg, Throwable e) {
        System.out.println("error: " + msg);
        if (e != null) {
            e.printStackTrace();
        }
    }

}
sftp Tools
 

controller:

UploadTest = SftpUtil new new SftpUtil ( "IP", "name", "password" Port "key address", null ); 
 uploadTest.downloadFile ( "file path SFTP", "Download Path");
controller

Csv file reads:

public List<OrderPayCSV> readOrderCSV(String path) {
        List<OrderPayCSV> csvList = new ArrayList<OrderPayCSV>();
        try {
            //这里要统一编码
            InputStreamReader read = new InputStreamReader(new FileInputStream(path), "UTF-8");
            BufferedReader reader = new BufferedReader(read);
            String line;
            int num = 0;
            while ((line = reader.readLine()) != null) {
                num++;
                if (num == 1) {
                    continue;
                }
                String info[] = line.split(",");
                OrderPayCSV orderPayCSV = new OrderPayCSV();
                orderPayCSV.setOrderNo(info[0]);
                orderPayCSV.setSerialNumber(info[1]);
                csvList.add(orderPayCSV);
            }
        } catch (FileNotFoundException ex) {
            System.out.println("没找到文件!");
        } catch(IOException EX) { 
            System.out.println ( "Error reading and writing files!" ); 
        } 
        Return csvList; 
    }
csv reading class

controller

public List<OrderPayCSV> readOrderCSV(String path) {
        List<OrderPayCSV> csvList = new ArrayList<OrderPayCSV>();
        try {
            //这里要统一编码
            InputStreamReader read = new InputStreamReader(new FileInputStream(path), "UTF-8");
            BufferedReader reader = new BufferedReader(read);
            String line;
            int num = 0;
            while ((line = reader.readLine()) != null) {
                num++;
                if (num == 1) {
                    continue;
                }
                String info[] = line.split(",");
                OrderPayCSV orderPayCSV = new OrderPayCSV();
                orderPayCSV.setOrderNo(info[0]);
                orderPayCSV.setSerialNumber(info[1]);
                csvList.add(orderPayCSV);
            }
        } catch (FileNotFoundException ex) {
            System.out.println("没找到文件!");
        } catch(IOException EX) { 
            System.out.println ( "Error reading and writing files!" ); 
        } 
        Return csvList; 
    }
csv reading
 
<insert id="insertList" parameterType="java.util.List">
        INSERT INTO examstack.et_order_pay_csv
        (order_no,serial_number,order_amount,order_currency,create_time)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.orderNo},#{item.serialNumber},#{item.orderAmount},#{item.orderCurrency},now())
        </foreach>
    </insert>
Bulk Insert database

Reconciliation: Reconciliation tableA in acquiring, on tableB data does not exist, bulk insert table b

 SELECT *
from tableA a
WHERE NOT EXISTS (SELECT * FROM tableB b WHERE b.id = a.id)

Contrast update:

    UPDATE tableA a,tableB b
        SET b.order_status = a.order_status,
            b.order_amount = a.order_amount,
            b.real_bank_serial_number = a.refund_number,
        WHERE b.order_no = a.order_no    

Guess you like

Origin www.cnblogs.com/xueyicanfei/p/12216691.html