[英]Poco::Net::FTPClientSession uploading blank, 0-byte copy of the actual target file
I am currently writing a class which handles a variety of FTP requests and I'm using Poco's FTPClientSession class.我目前正在编写一个 class 来处理各种 FTP 请求,并且我正在使用 Poco 的 FTPClientSession class。 I've managed to get most of the stuff I needed to work, however I'm facing an issue regarding uploading files to the server.
我已经设法获得了工作所需的大部分内容,但是在将文件上传到服务器时遇到了问题。
int __fastcall upload(String sLocalPath,
String sLocalFile, // String can be substituted by std::string here, basically the same thing
String sRemotePath,
String sRemoteFile,
String& sErr,
int iMode,
bool bRemoveFile)
{
try
{
// replace backslashes with forward slashes in the filepath strings,
// append one if necessary
std::string sLocalFilepath = sLocalPath.c_str();
std::replace(sLocalFilepath.begin(), sLocalFilepath.end(), '\\', '/');
if (sLocalFilepath[sLocalFilepath.size() - 1] != '/')
sLocalFilepath += "/";
sLocalFilepath += sLocalFile.c_str();
std::string sRemoteFilepath = sRemotePath.c_str();
std::replace(sRemoteFilepath.begin(), sRemoteFilepath.end(), '\\', '/');
// traverses and/or creates directories in the server (this definitely works)
FailsafeDirectoryCycler(sRemoteFilepath, "/");
// upload the file
m_Session.beginUpload(sLocalFilepath);
m_Session.endUpload();
// change the name if necessary
if (sLocalFile != sRemoteFile)
{
std::string oldName = sLocalFile.c_str();
std::string newName = sRemoteFile.c_str();
m_Session.rename(oldName, newName);
}
// delete the local file if specified
if (bRemoveFile)
DeleteFileA((sLocalPath + sLocalFile).c_str());
m_Session.setWorkingDirectory("/");
return 0;
}
catch (Poco::Exception& e)
{
std::cout << e.displayText() << std::endl;
return -1;
}
}
The above function also changes the file transfer mode ( TYPE_TEXT
or TYPE_BINARY
), however I excluded it for clarity, as I am certain it works as intended.上面的 function 也改变了文件传输模式(
TYPE_TEXT
或TYPE_BINARY
),但是为了清楚起见,我排除了它,因为我确信它可以按预期工作。 The call to this function looks as follows:对此 function 的调用如下所示:
f.upload(".", "filename123.txt", "testdir1\\testdir2\\abc", "filenamenew.txt", err, 0, true);
The arguments indicate that the file to be transfered is ./filename.txt
, and it will end up as /testdir1/testdir2/abc/filenamenew.txt
, which is exactly what happens (the rest of the arguments doesn't matter in this case). arguments 表示要传输的文件是
./filename.txt
,最终会变成/testdir1/testdir2/abc/filenamenew.txt
,这正是发生的情况(ZDBC11CAA5BDA5C6800AAD896F888B2A62AFCZ 在这个78829D7F中没有问题案子)。 However, my issue is, the local file contains a short string: abcdef
.但是,我的问题是,本地文件包含一个短字符串:
abcdef
。 The file, which gets uploaded to the server, does not contain a single byte;上传到服务器的文件不包含单个字节; it is blank.
它是空白的。
I couldn't find an answer to this question other than there is insufficient space on the server, which is definitely not the issue in this case.除了服务器上的空间不足之外,我找不到这个问题的答案,这绝对不是这种情况下的问题。 I shall add that the server is hosted locally using XLight FTP Server.
我要补充一点,服务器是使用 XLight FTP 服务器在本地托管的。 Has anyone encountered this kind of problem before?
有没有人遇到过这种问题?
Turns out, Poco::Net::FTPClientSession::beginUpload() doesn't read the file on its own.事实证明, Poco::Net::FTPClientSession::beginUpload() 不会自行读取文件。 It returns a reference to an std::ostream, to which you need to load the contents of the file yourself (eg using std::ifstream):
它返回对 std::ostream 的引用,您需要自己加载文件的内容(例如使用 std::ifstream):
std::ifstream hFile(sLocalFilepath, std::ios::in);
std::string line;
std::ostream& os = m_Session.beginUpload(sRemoteFile.c_str());
while (std::getline(hFile, line))
os << line;
hFile.close();
m_Session.endUpload();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.