解决FTP传文件有时候路径没有创建的问题

CigaretteSH
Flamingo 9 months ago
parent cf45f5c8a1
commit 1ed252b5ff

@ -52,6 +52,8 @@ ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent)
readyToSendZip = false;
listToZip.clear();
tmpListToZip.clear();
hftp = NULL;
hint = NULL;
}
@ -62,8 +64,8 @@ void ExportDataThread::init(string ip_, int port_, string username_, string user
port = port_;
username = username_;
userpwd = userpwd_;
cout << "333333ftp ip =" << ip << "| prot =" << port << endl;
cout << "333333username =" << username << "| pwd =" << userpwd << endl;
cout << "ftp ip =" << ip << "| prot =" << port << endl;
cout << "username =" << username << "| pwd =" << userpwd << endl;
b_quit = false;
flag = false;
@ -86,8 +88,8 @@ void ExportDataThread::init(string ip_, int port_, string username_, string user
pDocument[index]->InsertFirstChild(declaration);
}
}
//init(ip, port, username, userpwd);
cout << "11111111111111";
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (hint == NULL) {
return;
@ -111,6 +113,23 @@ void ExportDataThread::stop()
//InternetCloseHandle(hint);
}
bool ExportDataThread::FtpConnectionTest () {
int retryCount = 3;
if ((hftp == NULL) || (!FtpSetCurrentDirectoryA(hftp, "/"))) {
while (!ConnectFtp(&ip, &port, &username, &userpwd)) {
if (retryCount-- == 0) {
break;
}
QTime delayTime = QTime::currentTime().addMSecs(100); //100ms
while (QTime::currentTime() < delayTime) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
}
}
return (retryCount > 0);
}
bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_) {
//// FTP地址
@ -121,20 +140,12 @@ bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, st
//string userName = "FTP2";
///* 密码 */
//string pwd = "123";
cout << "222ftp ip =" << *ip_ << "| prot =" << *port_ << endl;
cout << "222username =" << *username_ << "| pwd =" << *userpwd_ << endl;
if (hftp != NULL) {
InternetCloseHandle(hftp);
hftp = NULL;
}
if (hint == NULL) {
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (hint == NULL) {
qDebug() << "Faile to open internet";
return false;
}
}
// 创建ftp连接
// hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0);
hftp = InternetConnectA(hint, ip_->c_str(), *port_, username_->c_str(), userpwd_->c_str(), INTERNET_SERVICE_FTP, 0, 0);
@ -385,7 +396,7 @@ void ExportDataThread::run()
//QString out = QString::fromLocal8Bit(process.readAllStandardOutput());
//qDebug() << out;
remotePath = zipFilePaths[i].mid(zipFilePaths[i].indexOf("image") - 1);
qDebug() << remotePath;
//qDebug() << remotePath;
ConnectServer(zipFilePaths[i], remotePath);
QFile file(zipFilePaths[i]);
file.remove();
@ -401,10 +412,14 @@ void ExportDataThread::run()
pDocument[index]->~XMLDocument();
}
if (hftp != NULL) {
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
hftp = NULL;
}
if (hftp != NULL) {
InternetCloseHandle(hint);
hint = NULL;
}
}
//*****************************************已经调试好,没有理清逻辑前不要动**********************
@ -448,17 +463,22 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
size = files.size();
}
if (!FtpConnectionTest()) {
qDebug() << "Failed to connect FTP server";
return;
}
// 上传文件源为一个文件
if (size == 0) {
int pos = remotePath.find_last_of('/');
string destFileName = remotePath.substr(pos + 1);
string tempPath = remotePath.substr(0, pos + 1);
check_save_dir(tempPath);
if (!InternetGetConnectedState(NULL, 0)) {
qDebug() << "计算机未连接到互联网";
}
else {
check_save_dir(tempPath);
while (!FtpPutFileA(hftp, filePath.c_str(), remotePath.c_str(), FTP_TRANSFER_TYPE_BINARY, 0)) {
qDebug() << "ftp put file " << filePath.c_str() << "failed because" << GetLastError();
if (!ConnectFtp(&ip, &port, &username, &userpwd))
@ -481,54 +501,6 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
}
}
/*
bool compression_zip_file(const QString& selectFile2DirPath, const QString& savePath)
{
if (selectFile2DirPath.isEmpty() || savePath.isEmpty())
{
return false;
}
if (!QFile::exists(selectFile2DirPath) || !QFileInfo(savePath).isDir())
{
return false;
}
if (QFileInfo(selectFile2DirPath).isFile())///压缩的是一个文件
{
QString fileName = QFileInfo(selectFile2DirPath).baseName();
QString writerFilePath = savePath + "/" + fileName + ".zip";
QFile selectFile(selectFile2DirPath);
qint64 size = selectFile.size() / 1024 / 1024;
if (!selectFile.open(QIODevice::ReadOnly) || size > FILE_MAX_SIZE)
{
///打开文件失败或者大于1GB导致无法压缩的文件
return false;
}
QString addFileName = QFileInfo(selectFile2DirPath).fileName();
QZipWriter writer(writerFilePath);
writer.addFile(addFileName, selectFile.readAll());
selectFile.close();
return true;
}
else///压缩的是一个文件夹
{
QString zipRootFolder = selectFile2DirPath.mid(selectFile2DirPath.lastIndexOf("/") + 1);
QString selectDirUpDir = selectFile2DirPath.left(selectFile2DirPath.lastIndexOf("/"));
QString saveFilePath = savePath + "/" + zipRootFolder + ".zip";
QZipWriter writer(saveFilePath);
writer.addDirectory(zipRootFolder);
QFileInfoList fileList = ergodic_compression_file(&writer, selectDirUpDir, selectFile2DirPath);
writer.close();
if (0 == fileList.size())
return true;
return false;
}
}
*/
void ExportDataThread::GetDataFromSaveThread(QString filePath) {
QString remotePath;

@ -127,6 +127,7 @@ public:
//int insertXMLNode(const char* xmlPath, _ExportDataInfo& data);
int insertXMLNode(const char* xmlPath, _XMLExportDataInfo& data);
bool ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_);
bool FtpConnectionTest();
void ConnectServer(QString srcPath, QString destPath);
void GetFiles(string path, std::vector<string>& files);

Loading…
Cancel
Save