文件读写操作(工具类)
1、核心功能
文件上传:
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"