讀寫文件+壓縮文件+上傳下載sftp服務器(工具類)

核心功能

文件上傳:
1、 將文件壓縮爲gz格式,且上傳到sftp服務器
2、向文件中寫入String類型的數據,並壓縮爲gz格式,且上傳到sftp服務器
3、向文件中寫入list類型的數據,每條分行,並壓縮爲gz格式,上傳到sftp服務器

文件下載: 從sftp服務器下載壓縮文件gz,且讀取壓縮文件內容的數據,獲取到list類型的數據

2 文件讀寫核心操作工具類 ExchangeFileAndListUtils

文件上傳3種 uploadDataFileToSftp() ,文件下載1種 testdownloadFileFromSftp()。

package com.zkbc.tools.file.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.junit.Test;

import com.zkbc.tools.filedeal.util.AnnotationDealUtil;
import com.zkbc.tools.filedeal.util.StringUtil;
testuploadsftp
public class ExchangeFileAndListUtils {
    /**在sftp服務器上傳test工具方法使用說明 如下*/
    @Test
    public void testuploadsftp() {

        /**1 sftp服務器   將 D:/temp/aa.del 文件,壓縮爲gz格式,同時上傳到sftp服務器上*/
        uploadFileToSftpBydelFile("aa.del", "D:/temp/", "172.30.10.37", "/XXX/2020/04/22", "root", "st********T", 22,
                true);

        /**2 向文件D:/temp/a/hello.txt 中寫入內容data,同時壓縮上傳到linux服務器【hello.txt可以不存在;寫入數據內容可以通過數據庫獲取到】*/
        uploadDataFileToSftp("傲嬌仙女", "hello.txt", "D:/temp/a/", "172.30.10.37", "/XXX/2020/04/22", "root",
                "st********T", 22, true);

        /**3 向文件D:/temp/cc/aa.del 中寫入list數據,同時壓縮上傳到linux服務器【hello.txt可以不存在;寫入數據內容可以通過數據庫獲取到】*/
        uploadFileToSftp(downloadFileFromSftp(), null, "aa.del", "D:/temp/cc/", "172.30.10.37", "/XXX/2020/04/22",
                "root", "st********T", 22, true);

    }

    /**1 下載test工具方法使用說明 :下載服務器文件/XXX/2020/04/22/reportSynData007.del.gz到本地D:/temp/b/裏,解壓縮獲取文件內容,獲取數據類型爲list
     * 注意:reportSynData007.del.gz文件存在
     * */
    @Test
    public void testdownloadFileFromSftp() {

        Map<String, Object> re = downloadFileFromSftp("reportSynData007.del.gz", "/XXX/2020/04/22", "172.30.10.37",
                "root", "st********T", 22, "D:/temp/b/", ReportSynData.class);
        //downloadFileFromBankSettingSftp
        System.out.println(re.toString());

        if (re == null || !(boolean) re.get("status")) {
            System.out.println("---SFTP服務器上獲取文件下載失敗---");
            return;
        }

        if (re != null && (boolean) re.get("status")) {
            List<Object> list = (List<Object>) re.get("list");
            System.out.println(list);
        }
    }

    /**
    * 將data數據,壓縮成gzip格式,上傳至sftp服務器
    * @param List<CustomerBalanceCheck>,sequenceNum序號
    * @return Map<String, Object> status(boolean)狀態、message(String)操作描述
    */
    @SuppressWarnings("resource")
    public static Map<String, Object> uploadDataFileToSftp(String data, String fileName, String localPath,
            String sftpIp, String remotePath, String sftpUsername, String sftpPassword, int port, boolean isZip) {
        Map<String, Object> map = new HashMap<String, Object>();
        OutputStreamWriter out = null;
        SFTPUtil _ftp = null;
        FileInputStream fis = null;
        FileInputStream fin = null;
        checkFilePath(localPath);
        try {
            out = new OutputStreamWriter(new FileOutputStream(localPath + fileName), "GB18030");
            out.write(data);
            out.flush();
            out.close();
            fis = new FileInputStream(localPath + fileName);
            fis.close();
            _ftp = new SFTPUtil(sftpIp, sftpUsername, sftpPassword, port);
            _ftp.connect();
            boolean _boo = false;
            if (isZip) {
                // 將數據壓縮
                String tmpZipFile = fileName + ".gz";
                SFTPUtil.gzipfile(localPath + fileName, localPath + tmpZipFile);
                fin = new FileInputStream(localPath + tmpZipFile);
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                // 設定讀入緩衝區尺寸
                byte[] buf = new byte[1024];
                while (fin.read(buf) != -1) {
                    bout.write(buf);
                }
                fin.close();
                _boo = _ftp.uploadFile(remotePath, tmpZipFile, localPath, tmpZipFile);
            } else {
                _boo = _ftp.uploadFile(remotePath, fileName, localPath, fileName);
            }
            if (!_boo) {
                map.put("status", false);
                map.put("message", "文件上傳失敗");
            } else {
                map.put("status", true);
                map.put("message", "文件上傳成功");
            }
            _ftp.disconnect();
        } catch (Exception e) {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (_ftp != null) {
                _ftp.disconnect();
            }
            map.put("status", false);
            map.put("message", "上傳文件發生異常");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 將list轉換成TXT文件,壓縮成gzip格式,上傳至sftp服務器
     * @param List<CustomerBalanceCheck>,sequenceNum序號
     * @return Map<String, Object> status(boolean)狀態、message(String)操作描述
     */
    @SuppressWarnings("resource")
    public static Map<String, Object> uploadFileToSftp(List list, String fileHead, String fileName, String localPath,
            String sftpIp, String remotePath, String sftpUsername, String sftpPassword, int port, boolean isZip) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (list == null & list.size() < 1) {
            map.put("status", false);
            map.put("message", "上傳list爲null");
            return map;
        }
        OutputStreamWriter out = null;
        SFTPUtil _ftp = null;
        FileInputStream fis = null;
        FileInputStream fin = null;
        checkFilePath(localPath);
        try {
            out = new OutputStreamWriter(new FileOutputStream(localPath + fileName), "GB18030");
            Iterator<Object> it = list.iterator();
            if (!StringUtil.isEmptyAndNotNull(fileHead)) {
                out.write(fileHead);
                out.flush();
            }

            while (it.hasNext()) {
                Object info = it.next();
                if (info == null) {
                    map.put("status", false);
                    map.put("message", "list實體對象爲null");
                    return map;
                }
                Map<String, Object> validate = AnnotationDealUtil.validate(info);
                if (validate == null || !validate.containsKey("result") || !(boolean) validate.get("result")) {
                    map.put("status", false);
                    map.put("message", validate.get("message"));
                    return map;
                }
                out.write(info.toString());
                out.flush();
            }
            out.close();
            fis = new FileInputStream(localPath + fileName);
            fis.close();
            _ftp = new SFTPUtil(sftpIp, sftpUsername, sftpPassword, port);
            _ftp.connect();
            boolean _boo = false;
            if (isZip) {
                // 將數據壓縮
                String tmpZipFile = fileName + ".gz";
                SFTPUtil.gzipfile(localPath + fileName, localPath + tmpZipFile);
                fin = new FileInputStream(localPath + tmpZipFile);
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                //設定讀入緩衝區尺寸   
                byte[] buf = new byte[1024];
                while (fin.read(buf) != -1) {
                    bout.write(buf);
                }
                fin.close();
                _boo = _ftp.uploadFile(remotePath, tmpZipFile, localPath, tmpZipFile);
            } else {
                _boo = _ftp.uploadFile(remotePath, fileName, localPath, fileName);
            }
            if (!_boo) {
                map.put("status", false);
                map.put("message", "文件上傳失敗");
            } else {
                map.put("status", true);
                map.put("message", "文件上傳成功");
            }
            _ftp.disconnect();
        } catch (Exception e) {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (_ftp != null) {
                _ftp.disconnect();
            }
            map.put("status", false);
            map.put("message", "上傳文件發生異常");
            e.printStackTrace();
        }
        return map;
    }

    public static Map<String, Object> downloadFileFromSftp(String fileName, String remotePath, String sftpIp,
            String sftpUsername, String sftpPassword, int port, String localPath, Class<?> classType) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Object> list = new ArrayList<Object>();
        SFTPUtil _ftp = null;
        InputStreamReader read = null;
        checkFilePath(localPath);
        try {
            _ftp = new SFTPUtil(sftpIp, sftpUsername, sftpPassword, port);
            _ftp.connect();
            boolean downloadFile = _ftp.downloadFile(remotePath, fileName, localPath, fileName);
            if (downloadFile) {
                File file = new File(localPath + fileName);
                if (file.isFile() && file.exists()) { //判斷文件是否存在
                    read = new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), "GB18030");
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while ((lineTxt = bufferedReader.readLine()) != null) {
                        String[] split = lineTxt.split(",", -1);//保留lineTxt中的空值
                        Object o = arrayToObject(split, classType);
                        list.add(o);
                    }
                    read.close();
                    map.put("list", list);
                    map.put("status", true);
                    map.put("message", "操作成功");
                } else {
                    map.put("status", false);
                    map.put("message", "找不到指定的文件");
                }
                //file.delete();
                _ftp.disconnect();
            } else {
                map.put("status", false);
                map.put("message", "下載文件出錯");
            }
        } catch (Exception e) {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (_ftp != null) {
                _ftp.disconnect();
            }
            e.printStackTrace();
            map.put("status", false);
            map.put("message", "下載文件發生異常");
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (_ftp != null) {
                _ftp.disconnect();
            }

        }
        return map;
    }

    //檢驗解壓的數據解析到對象裏是否正確要把所有列放在壓縮文件裏,是否一一對應一目瞭然
    public static Object arrayToObject(Object[] obj, Class<?> classType) {

        Object stu1 = null;
        try {
            stu1 = classType.newInstance();
            //assetRegistContractNo,duedate,dueTime,custAccno,toBeCollectedPrincipal,contractFlag
            for (int i = 0; i < classType.getDeclaredFields().length; i++) {

                String setMethodName = "set" + classType.getDeclaredFields()[i].getName().substring(0, 1).toUpperCase()
                        + classType.getDeclaredFields()[i].getName().substring(1);
                Method setMethod = classType.getDeclaredMethod(setMethodName,
                        new Class[] { classType.getDeclaredFields()[i].getType() });
                setMethod.invoke(stu1, obj[i]);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return stu1;
    }

    private static void checkFilePath(String path) {
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
    }

    /**
     * 將del文件壓縮成gzip上傳至sftp服務器
     */
    public static Map<String, Object> uploadFileToSftpBydelFile(String fileName, String localPath, String sftpIp,
            String remotePath, String sftpUsername, String sftpPassword, int port, boolean isZip) {
        Map<String, Object> map = new HashMap<String, Object>();
        SFTPUtil _ftp = null;
        FileInputStream fin = null;
        checkFilePath(localPath);
        try {
            _ftp = new SFTPUtil(sftpIp, sftpUsername, sftpPassword, port);
            _ftp.connect();
            boolean _boo = false;
            if (isZip) {
                // 將數據壓縮
                String tmpZipFile = fileName + ".gz";
                SFTPUtil.gzipfile(localPath + fileName, localPath + tmpZipFile);
                fin = new FileInputStream(localPath + tmpZipFile);
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                //設定讀入緩衝區尺寸   
                byte[] buf = new byte[1024];
                while (fin.read(buf) != -1) {
                    bout.write(buf);
                }
                fin.close();
                _boo = _ftp.uploadFile(remotePath, tmpZipFile, localPath, tmpZipFile);
            } else {
                _boo = _ftp.uploadFile(remotePath, fileName, localPath, fileName);
            }
            if (!_boo) {
                map.put("status", false);
                map.put("message", "文件上傳失敗");
            } else {
                map.put("status", true);
                map.put("message", "文件上傳成功");
            }
            _ftp.disconnect();
        } catch (Exception e) {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (_ftp != null) {
                _ftp.disconnect();
            }
            map.put("status", false);
            map.put("message", "上傳文件發生異常");
            e.printStackTrace();
        }
        return map;
    }

    public List<ReportSynData> downloadFileFromSftp() {

        Map<String, Object> re = downloadFileFromSftp("reportSynData007.del.gz", "/XXX/2020/04/22", "172.30.10.37",
                "root", "st********T", 22, "D:/temp/b/", ReportSynData.class);
        //downloadFileFromBankSettingSftp
        System.out.println(re.toString());

        if (re == null || !(boolean) re.get("status")) {
            System.out.println("---SFTP服務器上獲取文件下載失敗---");
            return null;
        }

        if (re != null && (boolean) re.get("status")) {
            List<ReportSynData> list = (List<ReportSynData>) re.get("list");
            System.out.println(list);
            return list;
        }
        return null;
    }

}

4、解析出的list每個元素對應的對象:ReportSynData

package com.zkbc.tools.file.util;

/**業務實體類*/
public class ReportSynData {
    private String assetRegistContractNo;

    private String duedate;

    private String dueTime;

    private String custAccno;

    private String toBeCollectedPrincipal;

    private String contractFlag;

    public String getAssetRegistContractNo() {
        return assetRegistContractNo;
    }

    public void setAssetRegistContractNo(String assetRegistContractNo) {
        this.assetRegistContractNo = assetRegistContractNo;
    }

    public String getDuedate() {
        return duedate;
    }

    public void setDuedate(String duedate) {
        this.duedate = duedate;
    }

    public String getDueTime() {
        return dueTime;
    }

    public void setDueTime(String dueTime) {
        this.dueTime = dueTime;
    }

    public String getCustAccno() {
        return custAccno;
    }

    public void setCustAccno(String custAccno) {
        this.custAccno = custAccno;
    }

    public String getToBeCollectedPrincipal() {
        return toBeCollectedPrincipal;
    }

    public void setToBeCollectedPrincipal(String toBeCollectedPrincipal) {
        this.toBeCollectedPrincipal = toBeCollectedPrincipal;
    }

    public String getContractFlag() {
        return contractFlag;
    }

    public void setContractFlag(String contractFlag) {
        this.contractFlag = contractFlag;
    }

    @Override
    public String toString() {
        return assetRegistContractNo + "," + duedate + "," + dueTime + "," + custAccno + "," + toBeCollectedPrincipal
                + "," + contractFlag + "\r\n";
    }

}

5 測試壓縮文件用的數據格式:

aa.del
assetRegistContractNo,duedate,dueTime,custAccno,toBeCollectedPrincipal,contractFlag
"adfg2019122617234283580","20200127","000000","0010QETTEWE661","345001","0"
"adfg2019122623457835800","20200127","000000","0010QETTEWE036","11399","0"
"adfg2019122612345285819","20200127","000000","0010QETTEWE165","1278067","0"
"adfg2019122618242335870","20200127","000000","0010QETTEWE190","1133333","0"
"adfg2019122623412835870","20200127","000000","0010QETTEWE910","132734","0"
"adfg2019122618523234279","20200127","000000","0010QETTEWE977","209866","0"
"adfg2019122618483445702","20200127","000000","0010QETTEWE988","155400","0"
"adfg20191226184845835902","20200127","000000","0010QETTEWE164","54466","0"
"adfg20191226185722355923","20200127","000000","0010QETTEWE168","378000","0"
"adfg20191226196788835678","20200127","000000","0010QETTEWE441","450000","0"
"adfg20191226190167886935","20200127","000000","0010QETTEWE085","7650","0"
"adfg20191226192604867866","20200127","000000","0010QETTEWE604","879999","0"
"adfg20191226192606786986","20200127","000000","0010QETTEWE154","101933","0"
"adfg20191226193519560054","20200127","000000","0010QETTEWE244","106401","0"
"adfg20191226193417835681","20200127","000000","0010QETTEWE020","453332","0"
"adfg20191226193417856831","20200127","000000","0010QETTEWE213","74668","0"
"adfg20191226193615786761","20200127","000000","0010QETTEWE231","152266","0"
"adfg20191226193656778091","20200127","000000","0010QETTEWE568","477734","0"
"adfg20191226789997466086","20200127","000000","0010QETTEWE222","786667","0"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章