读取FTP文本并入库
来源:互联网 发布:手机淘宝店铺怎么登录 编辑:程序博客网 时间:2024/05/01 02:25
两个jar包
commons-net-1.4.1.jar
jakarta-oro-2.0.8.jar
代码
package net.sxjr.an.renbao;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import net.sxjr.an.utils.MyUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class ApiUtilsRenbao {
public static void reportDispose(){
//ftp服务器登录凭证
String host="61.178.21.80";
int port=21;
String user="xxx";
String password="xxxx";
String txtFileDir="/";
String dateString=MyUtils.parseDate(MyUtils.changeDateByDay(new Date(), -1));
String cbString ="新承保清单"+dateString+".txt";
boolean success = false;
FTPClient ftp = new FTPClient();
//设置中文编码
ftp.setControlEncoding("GBK");
try {
int reply;
ftp.connect(host, port);
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(user, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return;
}
System.out.println("aaa");
ftp.changeWorkingDirectory(dateString);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
System.out.println("line======" + ff.getName());
//遍历所有文件根据文件名匹配
if(ff.getName().equals(cbString)){
System.out.println(new Date()+" ftpDownload定时器读取成功"+ff.getName());
String result =readFile(ftp,ff.getName());
//BufferedReader br = new BufferedReader(new InputStreamReader(is));
// String line = null;
// while ((line = br.readLine()) != null) {
// System.out.println("line======" + line);
// }
// br.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
}
/**
* 返回一个文件流
* @param fileName
* @return
*/
public static String readFile(FTPClient ftp,String fileName){
String result = "";
InputStream ins = null;
try {
ins = ftp.retrieveFileStream(fileName);
// byte []b = new byte[ins.available()];
// ins.read(b);
//BufferedReader reader=new BufferedReader(new InputStreamReader(ins));
InputStreamReader isr = new InputStreamReader(ins, "GBK");
BufferedReader reader = new BufferedReader(isr);
String inLine = reader.readLine();
while (inLine != null) {
System.out.println(inLine);
result += (inLine + System.getProperty("line.separator"));
inLine = reader.readLine();
}
reader.close();
if(ins != null){
ins.close();
}
// 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题
ftp.getReply();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
reportDispose();
}
}
- 读取FTP文本并入库
- C#读取文本并入库解决方法
- java通过ftp方式读取文件,并解析入库
- java通过ftp方式读取文件,并解析入库
- java通过ftp方式读取文件,并解析入库
- 通过ftp读取文件入库
- python读取access文件并入库
- FTP文件 BCP入库
- FTP数据入库Hive
- 批量文本入库
- JAVA读取FTP文件并转为字符串
- java读取文件入库
- kettle读取文本文件入库
- 对象序列化到文本并读取
- 读取文件中的文本并返回字符串
- shell 读取并处理文本信息
- [csh] 逐行读取并处理文本
- MATLAB:读取文本数据并排序
- localStorage的存储数组对象的方式
- 原生 ajax 封装
- SOA (面向服务的架构)
- php成员变量获取速度排序时怎么样的?
- 关于HTTPS证书的理解
- 读取FTP文本并入库
- 查询oracle刚刚删除的数据,有时间限制
- Centos 配置mailx使用外部smtp发送邮件
- 基础练习 特殊回文数
- Eclipse中运行Maven打包编译时出现:Perhaps you are running on a JRE rather than a JDK?
- BloomFilter算法概述
- 综合素质评价系统-北上广深必须上的教育项目
- c语言fopen函数
- oracle-the password has expired