ftp下载,解析txt文件入库
2017-11-07 10:17
639 查看
1、ftp下载
/**
* 下载文件
*
* @param localPath
* @param remotePath
* @param remoteFile
*/
public void download(String localPath, String remotePath, String remoteFile) {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
createDirectory(remotePath, ftpClient);
// 获取远程路径文件列表
FTPFile[] remoteFiles = ftpClient.listFiles();
// 检查远程文件是否存在
if (remoteFiles.length < 1) {
log.error("远程文件夹:"+remotePath+"中文件为空或不存在!");
throw new RuntimeException("远程文件夹:"+remotePath+"中文件为空或不存在!");
}
log.info("开始下载远程文件: " + remotePath + remoteFile + " 到本地路径: "
+ localPath);
boolean isDownload = false;
for (FTPFile file : remoteFiles) {
if (file.getName().equals(remoteFile)) {
File localFile = new File(localPath + "/" + remoteFile);
createDirectory(localFile, localPath);
OutputStream out = new FileOutputStream(localFile);
isDownload = ftpClient.retrieveFile(remoteFile, out);
log.info("远程文件: " + remotePath + remoteFile + " 下载成功!");
out.close();
}
}
if (!isDownload) {
log.error("远程文件: " + remotePath + remoteFile + "下载失败或不存在!");
System.out.println();
throw new RuntimeException("远程文件: " + remotePath + remoteFile
+ "下载失败或不存在!");
}
} catch (Exception e) {
log.error(e);
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
2、解析txt文件
private List<String> readEkeyFile(String filePath){
List<String> list = new ArrayList<String>();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
list.add(lineTxt.trim());
// System.out.println(lineTxt);
}
read.close();
file.delete();
log.info("读取ekey文件内容成功");
// System.out.println(list.get(0).split(",")[0]);
} else {
log.info("找不到指定的文件");
throw new Exception("文件不存在!");
}
} catch (Exception e) {
log.info("读取文件内容出错"+e.toString());
throw new RuntimeException();
}
return list;
}
/**
* 下载文件
*
* @param localPath
* @param remotePath
* @param remoteFile
*/
public void download(String localPath, String remotePath, String remoteFile) {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
createDirectory(remotePath, ftpClient);
// 获取远程路径文件列表
FTPFile[] remoteFiles = ftpClient.listFiles();
// 检查远程文件是否存在
if (remoteFiles.length < 1) {
log.error("远程文件夹:"+remotePath+"中文件为空或不存在!");
throw new RuntimeException("远程文件夹:"+remotePath+"中文件为空或不存在!");
}
log.info("开始下载远程文件: " + remotePath + remoteFile + " 到本地路径: "
+ localPath);
boolean isDownload = false;
for (FTPFile file : remoteFiles) {
if (file.getName().equals(remoteFile)) {
File localFile = new File(localPath + "/" + remoteFile);
createDirectory(localFile, localPath);
OutputStream out = new FileOutputStream(localFile);
isDownload = ftpClient.retrieveFile(remoteFile, out);
log.info("远程文件: " + remotePath + remoteFile + " 下载成功!");
out.close();
}
}
if (!isDownload) {
log.error("远程文件: " + remotePath + remoteFile + "下载失败或不存在!");
System.out.println();
throw new RuntimeException("远程文件: " + remotePath + remoteFile
+ "下载失败或不存在!");
}
} catch (Exception e) {
log.error(e);
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
2、解析txt文件
private List<String> readEkeyFile(String filePath){
List<String> list = new ArrayList<String>();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
list.add(lineTxt.trim());
// System.out.println(lineTxt);
}
read.close();
file.delete();
log.info("读取ekey文件内容成功");
// System.out.println(list.get(0).split(",")[0]);
} else {
log.info("找不到指定的文件");
throw new Exception("文件不存在!");
}
} catch (Exception e) {
log.info("读取文件内容出错"+e.toString());
throw new RuntimeException();
}
return list;
}
相关文章推荐
- 通过Http地址返回JSON数据,进行解析;通过ftp返回.txt文件数据,进行解析;
- java通过ftp方式读取文件,并解析入库
- java通过ftp方式读取文件,并解析入库
- 定时从ftp上下载txt文件到数据库中
- 如何用Java解析位于FTP中的txt文件
- ftp下载 入库 调存储过程处理 备份文件
- java 往ftp上上传跟下载txt文件
- ftp下载,TXT文件操作相关
- 将网页上word、pdf、txt文件下载下来,解析成文本内容
- 渗透通过ftp -s:filename.txt下载文件
- java通过ftp方式读取文件,并解析入库
- 使用ftpClient下载ftp上所有文件解析
- 解析.txt文件入库
- Poco之ftp获取文件列表以及下载文件
- C# 利用FTP自动下载xml文件后利用 FileSystemWatcher 监控目录下文件变化并自动更新数据库
- [DOS] ftp批量上传,下载文件 windows_linux_mput_mget.bat
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- ftp 给定路径 下载改路径下的所有文件
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表...
- java做的比较完善的FTP连接上传下载文件