1.FTP實體類
public class BaseFtp {
private String ipAddr;//ip地址
private Integer port;//端口號
private String userName;//用戶名
private String pwd;//密碼
private String path;//路徑
private String refPayPath;//機上拒付名單路徑
private String localPath;//本地路徑
private String fileName;//文件名稱
public BaseFtp() {
super();
}public BaseFtp(String ipAddr, String userName, String pwd,
String path, String localPath, String fileName) {
super();
this.ipAddr = ipAddr;
this.userName = userName;
this.pwd = pwd;
this.path = path;
this.localPath = localPath;
this.fileName = fileName;
}public BaseFtp(String ipAddr, Integer port, String userName, String pwd,
String path, String localPath, String fileName) {
super();
this.ipAddr = ipAddr;
this.port = port;
this.userName = userName;
this.pwd = pwd;
this.path = path;
this.localPath = localPath;
this.fileName = fileName;
}public String getLocalPath() {
return localPath;
}public void setLocalPath(String localPath) {
this.localPath = localPath;
}public String getFileName() {
return fileName;
}public void setFileName(String fileName) {
this.fileName = fileName;
}public String getIpAddr() {
return ipAddr;
}public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}public Integer getPort() {
return port;
}public void setPort(Integer port) {
this.port = port;
}public String getUserName() {
return userName;
}public void setUserName(String userName) {
this.userName = userName;
}public String getPwd() {
return pwd;
}public void setPwd(String pwd) {
this.pwd = pwd;
}public String getPath() {
return path;
}public void setPath(String path) {
this.path = path;
}public String getRefPayPath() {
return refPayPath;
}public void setRefPayPath(String refPayPath) {
this.refPayPath = refPayPath;
}
}
2.工具類(包含一些讀取文件內容的方法)
package com.xxl.job.executor.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;import com.csair.cbd.utils.DateUtil;
import com.mysql.fabric.xmlrpc.base.Array;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.domain.TblScmLabelPushStatus;
public class FtpUtils {
private static final Log logger = LogFactory.getLog(FtpUtils.class);private static FTPClient ftp;
private static HashMap<String,Map<String, String>> ftpFileNew;
/**
* 獲取ftp連接
* @param f
* @return
* @throws Exception
*/
public static boolean connectFtp(BaseFtp f) throws Exception{
ftp=new FTPClient();
boolean flag=false;
int reply;
try{
if (f.getPort()==null) {
ftp.connect(f.getIpAddr(),21);
}else{
ftp.connect(f.getIpAddr(),f.getPort());
}
//ftp登陸
ftp.login(f.getUserName(), f.getPwd());
//設置文件傳輸類型
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//檢查延時
reply = ftp.getReplyCode();
//如果延時不在200到300之間,就關閉連接
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.changeWorkingDirectory(f.getPath());
flag = true;
return flag;
}catch(Exception e){
throw new Exception(e.getMessage());
}
}
/**
* 關閉ftp連接
*/
public static void closeFtp(){
if (ftp!=null && ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* ftp上傳文件
* @param f
* @throws Exception
*/
public static void upload(File f) throws Exception{
if (f.isDirectory()) {
ftp.makeDirectory(f.getName());
ftp.changeWorkingDirectory(f.getName());
//返回目錄名和文件名組成的字符串數組
String[] files=f.list();
for(String fstr : files){
File file1=new File(f.getPath()+"/"+fstr);
if (file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2=new File(f.getPath()+"/"+fstr);
FileInputStream input=new FileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}
}else{
File file2=new File(f.getPath());
FileInputStream input=new FileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}
/**
* 下載鏈接配置
* @param f
* @param localBaseDir 本地目錄
* @param remoteBaseDir 遠程目錄
* @throws Exception
*/
public static boolean startDown(BaseFtp f,String localBaseDir,String remoteBaseDir ) throws Exception{
if (FtpUtils.connectFtp(f)) {
FTPFile[] files = null;
boolean changedir = ftp.changeWorkingDirectory(new String(remoteBaseDir.getBytes("GBK"),"iso-8859-1"));
if (changedir) {
ftp.setControlEncoding("GBK");
files = ftp.listFiles();
for (int i = 0; i < files.length; i++) {
downloadFile(files[i], localBaseDir, remoteBaseDir);
}
return true;
}
}else{
logger.error("鏈接失敗!");
}
return false;
}
/**
* 下載鏈接配置
* @param f
* @param localBaseDir 本地目錄
* @param remoteBaseDir 遠程目錄
* @throws Exception
*/
public static boolean startDownWithFileName(BaseFtp f,String filename,String localBaseDir,String remoteBaseDir ) throws Exception{
if (FtpUtils.connectFtp(f)) {
FTPFile[] files = null;
boolean changedir = ftp.changeWorkingDirectory(new String(remoteBaseDir.getBytes("GBK"),"iso-8859-1"));
if (changedir) {
ftp.setControlEncoding("GBK");
files = ftp.listFiles();
for (int i = 0; i < files.length; i++) {
if(filename.equals(files[i].getName())){
downloadFile(files[i], localBaseDir, remoteBaseDir);
return true;
}
}
}
}else{
logger.error("鏈接失敗!");
}
return false;
}
/**
*
* 下載FTP文件
* 當你需要下載FTP文件的時候,調用此方法
* 根據獲取的文件名,本地地址,遠程地址進行下載
*
* @param ftpFile
* @param relativeLocalPath
* @param relativeRemotePath
*/
private static void downloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) {
if (ftpFile.isFile()) {
if (ftpFile.getName().indexOf("?") == -1) {
OutputStream outputStream = null;
try {
File entryDir = new File(relativeLocalPath);
//如果文件夾路徑不存在,則創建文件夾
if (!entryDir.exists() || !entryDir.isDirectory())
{
entryDir.mkdirs();
}
// File locaFile= new File(relativeLocalPath+ ftpFile.getName());
//判斷文件是否存在,存在則返回
// if(locaFile.exists()){
// return;
// }else{
outputStream = new FileOutputStream(relativeLocalPath+ ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), outputStream);
createFtpFileNewToList(ftpFile.getName());
outputStream.flush();
outputStream.close();
// }
} catch (Exception e) {
logger.error(e);
} finally {
try {
if (outputStream != null){
outputStream.close();
}
} catch (IOException e) {
logger.error("輸出文件流異常");
}
}
}
}
}
/**
* 先配置下載鏈接,在下載文件
* @param f
* @param savepath
* @param filename
* @return
* @throws Exception
*/
public static boolean downfile(BaseFtp f,String savepath, boolean refPay) throws Exception{
try{
FtpUtils.connectFtp(f);
String path = refPay ? f.getRefPayPath() : f.getPath();
// return FtpUtils.startDown(f, filename ,savepath, path);
ftpFileNew=new HashMap<String,Map<String,String>>();
return FtpUtils.startDown(f ,savepath, path);
}catch(Exception e){
throw new Exception(e.getMessage());
}
}
/**
* 讀取zip文件內容
*//*
public static List<CbUgdTargetedList> readZip(String path) {
File fil = new File(path);
ZipInputStream zipIn = null;
CbUgdTargetedList cbUgdTargetedList=new CbUgdTargetedList();
List<CbUgdTargetedList> list=new ArrayList<CbUgdTargetedList>();
try {
zipIn = new ZipInputStream(new FileInputStream(fil));
} catch (FileNotFoundException e) {
logger.error("創建ZIP讀取流出錯!"+e.getMessage());
e.printStackTrace();
}ZipEntry zipEn = null;
ZipFile zfil = null;
try {
zfil = new ZipFile(path);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
while ((zipEn = zipIn.getNextEntry()) != null) {
if (!zipEn.isDirectory()) { // 判斷此zip項是否爲目錄
*//**
* 把是文件的zip項讀出緩存,
*//*
BufferedReader buff = new BufferedReader(new InputStreamReader(zfil.getInputStream(zipEn)));
String str;
while ((str = buff.readLine()) != null) {
cbUgdTargetedList=new CbUgdTargetedList();
String[] fileResult=str.split(",");
if(fileResult.length>0){
if(fileResult.length>1){
cbUgdTargetedList.setUid(fileResult[0]);
cbUgdTargetedList.setMemberNo(fileResult[1]);
}else{
cbUgdTargetedList.setUid(fileResult[0]);
}
list.add(cbUgdTargetedList);
}
}
buff.close();
}
zipIn.closeEntry();// 關閉當前打開的項
}
} catch (IOException e) {
logger.error("讀取ZIP出錯!"+e.getMessage());
e.printStackTrace();
} finally {
try {
zfil.close();
} catch (IOException e) {
logger.error("關閉ZIP讀取流出錯!"+e.getMessage());
e.printStackTrace();
}
}
//根據UID去重
list=duplicateRemoval(list);
return list;
}
*//**根據字段去重
* @param list
* @return
*//*
public static List<CbUgdTargetedList> duplicateRemoval(List<CbUgdTargetedList> list){
List<CbUgdTargetedList> result=new ArrayList<CbUgdTargetedList>();
Set<String> set=new HashSet<String>();
for(CbUgdTargetedList item:list){
if(item!=null){
String uid=item.getUid();
if(StringUtils.isNotBlank(uid)){
if(!set.contains(uid)){
result.add(item);
set.add(uid);
}else{
logger.info("重複UID:"+uid+",會員號:"+item.getMemberNo());
}
}
}
}
return result;
}*/
public static void main(String[] args) throws Exception {
/*ftpFileNew =new HashMap<String,Map<String,String>>();
Map<String,String> map=new HashMap<String,String>();
map.put("b1", "a");
ftpFileNew.put("a1", map);
System.out.println(ftpFileNew.get("a1").get("b1"));
map=new HashMap<String,String>();
map.put("b1", "b");
ftpFileNew.put("a1", map);
System.out.println(ftpFileNew.size());
System.out.println(ftpFileNew.get("a1").get("b1"));*/
/*BaseFtp f=new BaseFtp();
f.setIpAddr("10.79.0.115");
f.setPath("/smpms/dss/");
f.setUserName("tag_user");
f.setPwd("taguser*2016");
connectFtp(f);
upload(new File("D:\\ftp\\test\\DSS20191116_T201911161643009485_170107.txt"));*/
//upload(new File("C:\\Users\\qiu-pc\\Desktop\\數據導入\\DSS20191106_T201911061643009485_170101.txt"));
//downfile(f,"D:\\ftp\\","DSS20191014_T201910140847504513_091410.txt", false);
/*File file=new File("C:/Users/qiu-pc/Desktop/數據導入/DSS20191014_T201910140847504513_091410/DSS20191014_T201910140847504513_091410.txt");
upload(file);
/*connectFtp(f);
upload(new File("F:\\FTP\\NEGATIVE_LIST_INT_20180504.zip"));
downfile(f,"F:\\FTP\\","NEGATIVE_LIST_INT_20180404.zip");
List<CbUgdTargetedList> list =readZip("F:\\FTP\\NEGATIVE_LIST_INT_20180404.zip");
System.out.println(list.size());
System.out.println(duplicateRemoval(list).size());*/
}
/**
* 讀取txt文件
* @readFileName 讀取文件的地址
* @writeFileName 輸出文件位置
* @return
*/
public static TblScmLabelPushStatus readTxt(String readFileName,String writeFileName) {
File file = new File(readFileName);
List<String> writeList=new ArrayList<String>();
TblScmLabelPushStatus tblScmLabelPushStatus=new TblScmLabelPushStatus();
String lineStr=null;
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
String str;
while ((str = buff.readLine()) != null) {
if("[header]".equals(str.trim())){
lineStr="[header]";
continue;
}
if("[content]".equals(str)){
lineStr="[content]";
continue;
}
if("[footer]".equals(str)){
lineStr="[footer]";
continue;
}
if("[header]".equals(lineStr)){
tblScmLabelPushStatus=new TblScmLabelPushStatus();
String[] fileResult=str.split("\\|\\|");
if(fileResult.length>0){
tblScmLabelPushStatus.setID(fileResult[0]);
tblScmLabelPushStatus.setLabelId(fileResult[1]);
tblScmLabelPushStatus.setLabelName(fileResult[2]);
tblScmLabelPushStatus.setLabelPrompt(fileResult[3]);
tblScmLabelPushStatus.setPushUser(fileResult[4]);
try {
tblScmLabelPushStatus.setPushTime(DateUtil.formatDate(fileResult[5],DateUtil.RFC3339_FORMAT));
} catch (Exception e) {
XxlJobLogger.log("ST標籤文件解析字段PushTime日期轉換異常!"+e.getMessage());
}
}
lineStr=null;
}
if("[content]".equals(lineStr)){
writeList.add(str);
}
if("[footer]".equals(lineStr)){
tblScmLabelPushStatus.setPushTotal(str);
}
}
buff.close();
} catch (IOException e) {
XxlJobLogger.log("讀取txt文件出錯!"+e.getMessage());
}
if(writeList.size()>0){
XxlJobLogger.log("開始寫入BL_SCM_TARGET_CUSTOMER_LABEL數據到文件");
//File writeFile = new File(writeFileName);
wirterToFile(writeFileName,writeList);
XxlJobLogger.log("寫入BL_SCM_TARGET_CUSTOMER_LABEL數據到文件成功");
}
return tblScmLabelPushStatus;
}
/**初始化標籤值文件
* @param readFileName
* @param writeFileName
*/
public static void readTxtInit(String writeFileName) {
List<String> writeList=new ArrayList<String>();
writeList.add("------Start importing-----");
File writeFile = new File(writeFileName);
writeData(writeFile,writeList);
XxlJobLogger.log("初始化標籤值文件成功!");
}
/**
* 數據寫入文件
* @param file 要寫入的文件
* @param feedbacks 要寫的數據
*/
public static boolean writeData(File file, List<String> uids) {
boolean flag = false;
PrintWriter pw = null;
StringBuilder str = null;
try {
pw = new PrintWriter(file, "utf-8");
str = new StringBuilder();
for (String uid : uids) {
str.append(uid);
}
pw.write(str.toString());
flag = true;
pw.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 關閉資源
try {
if(pw != null){
pw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return flag;
}
/**
* 追加文件:使用FileWriter
*
* @param fileName
* @param content
*/
public static void wirterToFile(String fileName, List<String> uids) {
FileWriter writer = null;
StringBuilder str;
try {
// 打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件
writer = new FileWriter(fileName, true);
str = new StringBuilder();
str.append("\r\n");
for (String uid : uids) {
str.append(uid).append("\r\n");
}
writer.write(str.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}public static HashMap<String, Map<String, String>> getFtpFileNew() {
return ftpFileNew;
}public static void setFtpFileNew(HashMap<String, Map<String, String>> ftpFileNew) {
FtpUtils.ftpFileNew = ftpFileNew;
}
}