edtftpj.jar包 FTP中下载数据

import java.io.IOException;

import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPTransferType;

public class TestFTPDownload {
 
 private String hostName = "198.98.251.94";//FTP IP
 
 private String userName = "root";//连接FTP的账号
 
 private String password = "root";//连接FTP的密码
 
 private int remotePort = 21;//端口号,默认为21
 
 private String localDirName = "I:/data/20160320/";//本地文件夹
 
 private String remoteDirName = "/data/20160320";//FTP中的文件夹
 
 /**
  * 建立FTP连接
  * @throws FTPException
  * @throws IOException
  */
 public FTPClient connectFTP() throws IOException, FTPException{
  FTPClient client = new FTPClient();
  client.setRemoteHost(hostName);
  client.setRemotePort(remotePort);
  client.setControlEncoding("GB2312");
  client.connect();
  client.setTimeout(12000);//设置超时时间
  client.login(userName, password);
  client.setType(FTPTransferType.BINARY);//二进制
  client.setConnectMode(FTPConnectMode.PASV);//以波动模式链接
  return client;
 }
 /**
  * 改变远程虚拟目录路径
  * @param client
  * @return
  */
 public boolean changeRemoteDir(FTPClient client){
  String[] dirName = remoteDirName.split("/");
  for(int i=0;i<dirName.length;i++){
   try {
    //逐层进入文件夹
    System.out.println(dirName[i]);
    client.chdir(dirName[i]);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
   } catch (FTPException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
   }
  }
  return true;
 }
 /**
  * 下载文件
  * @param client
  * @return
  */
 public boolean downloadFile(FTPClient client){
  try {
   String[] files = client.dir();
   for(int i=0;i<files.length;i++){
    client.get(localDirName+files[i],files[i]);
   }
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return false;
  } catch (FTPException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return false;
  }
  return true;
 }
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  TestFTPDownload test = new TestFTPDownload();
  try {
   FTPClient client = test.connectFTP();
   if(test.changeRemoteDir(client)){
    test.downloadFile(client);
   }
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FTPException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

转载于:https://www.cnblogs.com/jokin0520/p/5341263.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
edtftpj下载:http://www.enterprisedt.com/products/edtftpj/download.html<br><br>需求描述:把WEB服务器上一个目录所有的文件上传到指定ftp服务器的某个目录。<br>分析:<br> 在edtftpj有个FTPClient类,此类可以实现ftp的上传。用到的几个函数说明如下:<br> 1、构造函数 FTPClient(ftp服务器,ftp端口)<br> 2、login(ftp登录账号,ftp密码) //登录ftp服务器<br> 3、chdir("文件夹名") //进入ftp上的某个目录,类似DOS的cd命令<br> 4、setType(FTPTransferType.BINARY) //设置传输类型<br> 5、mkdir("文件夹名") //在ftp当前目录下新建一个目录<br>问题:<br> 利用此组件在测试过程,发现当上传含有文的文件名时会失败(FTPClient认为它不是一个文件,奇怪)!<br>完整程序:<br>package yhp.test.util;<br>import java.io.File;<br>import com.enterprisedt.net.ftp.FTPClient;<br>import com.enterprisedt.net.ftp.FTPTransferType;<br>/**<br> * @author Administrator<br> *<br> * TODO To change the template for this generated type comment go to Window -<br> * Preferences - Java - Code Style - Code Templates<br> */<br>public class FTPUpload {<br> private String ftpServer;<br> private String ftpPort;<br> private String ftpUserName;<br> private String ftpPassword;<br> private FTPClient ftpClient;<br> private boolean isLogin = false;<br> public FTPUpload(String pFtpServer, String pFtpPort, String pFtpUserName,<br> String pFtpPassword) throws Exception {<br> this.ftpServer = pFtpServer;<br> if(pFtpPort.trim().equals(""))<br> this.ftpPort="21";<br> else<br> this.ftpPort = pFtpPort;<br> if(pFtpUserName.trim().equals(""))<br> this.ftpUserName ="Anonymous";<br> else<br> this.ftpUserName = pFtpUserName;<br> this.ftpPassword = pFtpPassword;<br> try {<br> ftpClient = new FTPClient(ftpServer, Integer.parseInt(ftpPort)); <br> ftpClient.login(ftpUserName, ftpPassword);<br> ftpClient.chdir("\\");//在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了<br> isLogin = true;<br> } catch (Exception e) {<br> throw new Exception(e.getMessage());<br> }<br> }<br> //上传指定文件夹到ftp服务器上<br> public String uploadFolder(String folderName,String ftpPath)throws Exception{<br> if (isLogin) {<br> String strMsg="";<br> try{<br> File file=new File(folderName);<br> if(file.isDirectory()){<br> ftpClient.chdir("\\");<br> ftpClient.setType(FTPTransferType.BINARY);<br> if (checkFolderIsExist(ftpPath)) {<br> ftpClient.chdir(ftpPath);<br> } else {<br> createFolder(ftpPath);<br> }<br> if(!checkFolderIsExist(file.getName())){<br> ftpClient.mkdir(file.getName());<br> }<br> ftpClient.chdir(file.getName());<br> ftpPath=ftpPath+"\\"+file.getName();<br> File[] files=file.listFiles();<br> for(int i=0;i<files.length;i++){<br> if(files[i].isDirectory()){<br> uploadFolder(files[i].getPath(),ftpPath);<br> }else{<br> if(files[i].isFile()){<br> try{<br> ftpClient.put(files[i].getPath(),files[i].getName());<br> }catch(Exception ee){<br> strMsg+="upload file<<:"+files[i].getPath()+">> error!Message:"+ee.getMessage()+"\r\n";<br> }<br> }<br> }<br> }<br> if(!strMsg.equals("")){<br> throw new Exception(strMsg);<br> }<br> }else{<br> throw new Exception(folderName+" is not a folder'name!");<br> }<br> }catch(Exception e){<br> strMsg+=e.getMessage()+"\r\n";<br> }<br> return strMsg;<br> } else {<br> throw new Exception("you didnot login remote ftp server!");<br> }<br> }<br> <br><br>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值