poco ftp客户端操作演示

// 000.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <vector>
#include <assert.h>
#include <fstream>

#include "Poco/Net/MailMessage.h"
#include "Poco/Net/MailRecipient.h"
#include "Poco/Net/SMTPClientSession.h"
#include "Poco/Net/StringPartSource.h"
#include "Poco/Path.h"
#include "Poco/Exception.h"
#include <iostream>
#include "poco/net/StreamSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/POP3ClientSession.h"
#include "Poco/Net/NetException.h"
using Poco::Net::MailMessage;
using Poco::Net::MailRecipient;
using Poco::Net::SMTPClientSession;
using Poco::Net::StringPartSource;
using Poco::Path;
using Poco::Exception;
using Poco::Net::StreamSocket;
using Poco::Net::SocketAddress;

using Poco::Net::POP3ClientSession;
using Poco::Net::MessageHeader;
using Poco::Net::MailMessage;
using Poco::Net::POP3Exception;

#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/Exception.h"
#include <iostream>


using Poco::Net::StreamSocket;
using Poco::Net::SocketStream;
using Poco::Net::SocketAddress;
using Poco::StreamCopier;
using Poco::Path;
using Poco::Exception;

#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include <iostream>


#include "Poco/Net/FTPClientSession.h"
using Poco::Net::FTPClientSession;
using Poco::Net::FTPException;
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;

#include "Poco/ActiveMethod.h"
#include "Poco/StreamCopier.h"
#include <sstream>
using Poco::ActiveMethod;
using Poco::ActiveResult;
using Poco::StreamCopier;
#include "Poco/FileStream.h"
#include "Poco/File.h"
class ActiveDownloader
{
public:
    ActiveDownloader(FTPClientSession& session):
      download(this, &ActiveDownloader::downloadImp),
          _session(session)
      {
      }

      ActiveMethod<std::string, std::string, ActiveDownloader> download;

protected:
    std::string downloadImp(const std::string& path)
    {
        std::istream& istr = _session.beginDownload(path);
        std::ostringstream ostr;
        StreamCopier::copyStream(istr, ostr);
        _session.endDownload();
        return ostr.str();
    }

private:
    FTPClientSession& _session;
};

int main(int argc, char** argv)
{
    FTPClientSession session("ftp.hhhw64.budget-gecko-network.com", 21);
    try
    {
        session.login("aaa", "bbb");
        std::string type = session.systemType(); // 获取服务器类型
        std::string cwd = session.getWorkingDirectory(); // 获取当前工作目录
        // 工作目录ftp://ftp.hhhw64.budget-gecko-network.com/public_html/test/
        session.setWorkingDirectory("public_html//test"); // 设置工作目录
        cwd = session.getWorkingDirectory();
        // session.rename("old.txt", "new.txt"); // 更改文件名
        // session.cdup(); // cd..
        // cwd = session.getWorkingDirectory();

        // session.remove("xxx.txt"); // 删除文件
        // session.createDirectory("foo"); // 创建目录
        // session.removeDirectory("foo"); // 删除目录

        // 下载文本文件,使用ActiveMethod
//         session.setPassive(false);
//         ActiveDownloader dl(session);
//         ActiveResult<std::string> result = dl.download("1.txt");
//         result.wait();
//         std::string received = result.data();
//         cout<<received;

        // 下载文本文件,同一线程内,文件大会卡
//         std::istream& istr = session.beginDownload("1.txt");
//         std::ostringstream dataStr;
//         StreamCopier::copyStream(istr, dataStr);
//         session.endDownload();
//         std::string s(dataStr.str());
//         cout<<s;


        // 上传二进制文件
//         ifstream my_ifstream("123.jpg",ios::binary);
//         std::ostream& ostr = session.beginUpload("123.jpg");
//           ostr << my_ifstream.rdbuf();
//           session.endUpload();
//         my_ifstream.close();

        // 下载二进制文件
        ofstream outf("xxx.jpg", ios::binary);
        std::istream& istr = session.beginDownload("123.jpg");
        StreamCopier::copyStream(istr, outf);
        session.endDownload();
        outf.close();

        // 获取文件列表
//         std::istream& istr = session.beginList();
//         std::ostringstream dataStr;
//         StreamCopier::copyStream(istr, dataStr);
//         session.endList();
//         std::string s(dataStr.str());
//         cout<<s;

        session.close();
    }
    catch (FTPException& e)
    {
        cout<<e.displayText();
    }


    system("pause");
    return 0;
}

 

posted @ 2013-01-07 13:43  魏桐  阅读(1642)  评论(0编辑  收藏  举报