缘来似你 发表于 2015-11-6 10:57:19

java 代码之FTP服务器下载文件

  下面介绍下之前工作中用过的从FTP服务器下载文件并解析,相对比较完整的代码,已经经过测试。。。
  

/**
*
* 从ftp上下载文件,对其内容进行处理。
*
*/
@Component
public class BatchProcessingPort {
int    port= xxx;      //端口
String name= xxx;      //用户名
String password=xxx;    //密码
String upaddress = xxx//下载的地址;
String downName = xxx;//下载的文件名
downFile(url,port,name,password,upaddress,downName,"D:\\");//地址
String readName = "xxx"; //本地下载地址+文件名
ArrayList<String> readList=readtxt(readName);
/**
* ==============================================================================
*/
/*
* 读取分析.txt文档里的内容
*/
for(int i= 0;i< readList.size();i++){
if(readList.size() >0){
if(!readList.get(i).equals(&quot;&quot;)){
if(!readList.get(i).substring(0,1).equals(&quot;#&quot;)){
String code=readList.get(i).substring(0, readList.get(i).length()-3);
String status=readList.get(i).substring(readList.get(i).length()-2, readList.get(i).length()-1);
System.out.println(&quot;+++++++++++++++++++&quot;+code+&quot;---------------------&quot;+status);
String state = dbprofileservice.getSatus(code).getSTATUS();
log.info(&quot;-------warrant_code:&quot;+code+&quot;----------status:&quot;+status+&quot;,-----------the status of this database is&quot;+ state);
}
}
}
}
return rbpm;
}
/**
* Description: 从FTP服务器下载文件
* @param url FTP服务器hostname
* @param port   FTP服务器端口
* @param username FTP登录账号
* @param password   FTP登录密码
* @param remotePath   FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
// 初始表示下载失败
boolean success = false;
// 创建FTPClient对象
FTPClient ftp = new FTPClient();
try {
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url, port);
reply = ftp.getReplyCode();
/*
* 判断是否连接成功
*/
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
} else{   
// 登录ftp
if(ftp.login(username, password)){
// 转到指定下载目录
ftp.changeWorkingDirectory(remotePath);
// 列出该目录下所有文件
FTPFile[] fs = ftp.listFiles();
// 遍历所有文件,找到指定的文件
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
// 根据绝对路径初始化文件
File localFile = new File(localPath + &quot;/&quot; + ff.getName());
// 输出流
OutputStream is = new FileOutputStream(localFile);
// 下载文件
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
// 退出ftp
ftp.logout();
// 下载成功
success = true;
}
}
} catch (IOException e) {
e.printStackTrace();
<div style=&quot;text-align: center;&quot;></div>       } finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* 读取本地TXT
* @param filepath txt文件目录即文件名
*/
publicArrayList<String> readtxt(String filepath){
ArrayList<String> readList = new ArrayList<String>();
ArrayList retList = new ArrayList();   
try{   
String temp = null;   
File f = new File(filepath);   
String adn=&quot;&quot;;   
//指定读取编码用于读取中文   
InputStreamReader read = new InputStreamReader(new FileInputStream(f),&quot;UTF-8&quot;);         
BufferedReader reader=new BufferedReader(read);   
//bufReader = new BufferedReader(new FileReader(filepath));   
do{
temp=reader.readLine();
readList.add(temp);
}
while(temp!=null);
read.close();         
}catch (Exception e) {   
// TODO: handle exception         
e.printStackTrace();            
}
return readList;   
}
}


  

至于上传ftp,我这里提供下
/**
* 处理Ftp文件的上传和下载
*
* @author zhaofeng
*
*/
public class FtpTool {
/**
* Description: 向FTP服务器上传文件
*
* @param url
*            FTP服务器hostname
* @param username
*            FTP登录账号
* @param password
*            FTP登录密码
* @param path
*            FTP服务器保存目录
* @param filename
*            上传到FTP服务器上的文件名
* @param input
*            输入流
* @return 成功返回true,否则返回false
*/
public boolean uploadFile(String url, String username, String password,
String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
// 转到指定上传目录
ftp.changeWorkingDirectory(path);
ftp.setFileType(FTP.BINARY_FILE_TYPE); // 设置为2进制上传
// 将上传文件存储到指定目录
ftp.storeFile(filename, input);
ftp.logout();
input.close();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
上文的ftp上传部分代码转载自点击打开链接




  

版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: java 代码之FTP服务器下载文件