解决ftp传送问题

main
Jeffrey_Li 1 year ago
parent 92d931e56d
commit 2020f6a810

@ -31,42 +31,26 @@ ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent)
XMLDeclaration* declaration = pDocument[index]->NewDeclaration(); XMLDeclaration* declaration = pDocument[index]->NewDeclaration();
pDocument[index]->InsertFirstChild(declaration); pDocument[index]->InsertFirstChild(declaration);
} }
}
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (hint == NULL) {
return;
}
//if (error != XML_SUCCESS) // FTP地址
//{ string ftpServer = "192.168.1.170";
// XMLDeclaration* declaration = pDocument[index]->NewDeclaration(); /* 端口号一般为21 */
// pDocument[index]->InsertFirstChild(declaration); int port = 666;
// /* 用户名 */
// XMLElement* root = pDocument[0]->NewElement("Root"); string userName = "FTP2";
// pDocument[0]->InsertEndChild(root); /* 密码 */
// pDocument[index]->SaveFile(filePath.toLocal8Bit().constData()); string pwd = "123";
//} // 创建ftp连接
hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0);
/* if (hftp == NULL) {
_ExportDataInfo data; qDebug() << "ftp connect failed";
data.cameraId = index; return;
data.cameraTotal = 3;
data.timeCost = "3ms";
insertXMLNode(xmlPath, data);
*/
//_XMLExportDataInfo data;
//// 总的统计数据
//data.TotalCheckNum = 1000;
//data.TotalKickNum = 900;
//data.PassRate = 99.8;
//data.KickRate = 0.2;
//// 各相机数据
//data.cameraTotal = NumberOfSupportedCameras;
//data.cameraId = index;
//data.shotCounts = 3;
//data.okNum = 560;
//data.ngNum = 440;
//data.kickNum = 440;
//data.AcquisitionSpeed = "3ms";
//for (int i = 0; i < data.shotCounts; i++) data.jdNum[i] = 4;
//data.DetectSpeed = "5ms";
//insertXMLNode(filePath.toLocal8Bit().constData(), data);
} }
} }
@ -88,6 +72,8 @@ void ExportDataThread::stop()
//export_Data_Info_queue->put(data); //export_Data_Info_queue->put(data);
_XMLExportDataInfo data; _XMLExportDataInfo data;
export_XMLData_Info_queue->put(data); export_XMLData_Info_queue->put(data);
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
} }
bool _ExportDataInfo::getAverageData(map<string, float> &averageData, int index) bool _ExportDataInfo::getAverageData(map<string, float> &averageData, int index)
@ -138,45 +124,6 @@ bool _ExportDataInfo::getAverageData(map<string, float> &averageData, int index)
return true; return true;
} }
//int ExportDataThread::insertXMLNode(const char* xmlPath, _ExportDataInfo& data)
//{
// XMLElement* root = pDocument[data.cameraId]->RootElement();
//
// if (root == NULL) {
// root = pDocument[data.cameraId]->NewElement("Root");
// pDocument[data.cameraId]->InsertEndChild(root);
// pDocument[data.cameraId]->SaveFile(xmlPath);
// }
//
// XMLElement* CameraNode = pDocument[data.cameraId]->NewElement("Camera");
// CameraNode->SetAttribute("Id", data.cameraId);
// CameraNode->SetAttribute("Count ", data.cameraTotal);
// root->InsertEndChild(CameraNode);
//
// XMLElement* IsNG = pDocument[data.cameraId]->NewElement("IsNG");
// XMLText* IsNGText = pDocument[data.cameraId]->NewText(data.isNg ? "TRUE" : "FALSE");
// IsNG->InsertEndChild(IsNGText);
// CameraNode->InsertEndChild(IsNG);
//
// XMLElement* timeCost = pDocument[data.cameraId]->NewElement("TimeCost");
// timeCost->InsertEndChild(pDocument[data.cameraId]->NewText(data.timeCost.c_str()));
// CameraNode->InsertEndChild(timeCost);
//
// XMLElement* isJdExist = pDocument[data.cameraId]->NewElement("IsJdExist");
// isJdExist->InsertEndChild(pDocument[data.cameraId]->NewText(data.isJdExist[0] ? "TRUE" : "FALSE"));
// CameraNode->InsertEndChild(isJdExist);
//
// // XMLElement* jdInterval = pDocument[data.cameraId]->NewElement("JdInterval");
// // jdInterval->InsertEndChild(pDocument[data.cameraId]->NewText((const char*)&data.jdInterval));
// // CameraNode->InsertEndChild(jdInterval);
//
// // XMLElement* jdInterval = pDocument[data.cameraId]->NewElement("jdPointsLocation");
// // jdInterval->InsertEndChild(pDocument[data.cameraId]->NewText(data.getPoint(0).c_str()));
// // CameraNode->InsertEndChild(jdInterval);
//
// return pDocument[data.cameraId]->SaveFile(xmlPath);
//}
int ExportDataThread::insertXMLNode(const char* xmlPath, _XMLExportDataInfo& data) int ExportDataThread::insertXMLNode(const char* xmlPath, _XMLExportDataInfo& data)
{ {
XMLElement* root = pDocument[data.cameraId]->RootElement(); XMLElement* root = pDocument[data.cameraId]->RootElement();
@ -330,27 +277,24 @@ void ExportDataThread::run()
} }
} }
//*****************************************已经调试好,没有理清逻辑前不要动**********************
void ExportDataThread::check_save_dir(std::string dir_path) void ExportDataThread::check_save_dir(std::string dir_path)
{ {
bool b_find = false; // 如果目的路径不存在,一级一级创建
for (int i = 0; i < vec_save_dirs.size(); i++) // 目的路径格式为:"./dir/dir1/.../"
{
if (dir_path == vec_save_dirs[i])
{
b_find = true;
}
}
if (!b_find)
{
//QString dir_str = QString::fromStdString(dir_path);
QString dir_str = QString::fromLocal8Bit(QByteArray::fromRawData(dir_path.c_str(), dir_path.size()));
QDir dir; if (FALSE == FtpSetCurrentDirectoryA(hftp, dir_path.c_str())) {
if (!dir.exists(dir_str)) FtpSetCurrentDirectoryA(hftp, "/");
{ int pos = 1;
dir.mkpath(dir_str); while (pos > 0) {
// 从第二个“/”开始依次找到目的路径中的“/”位置
pos = dir_path.find_first_of('/', pos + 1);
if (pos == -1)
break;
string tempPath = dir_path.substr(0, pos + 1);
FtpCreateDirectoryA(hftp, tempPath.c_str());
} }
vec_save_dirs.push_back(dir_path); FtpSetCurrentDirectoryA(hftp, dir_path.c_str());
} }
} }
@ -361,14 +305,6 @@ void ExportDataThread::EDrecMsgFromCigarette(){
void ExportDataThread::ConnectServer(QString srcPath, QString destPath) { void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
string filePath = srcPath.toLocal8Bit().constData(); string filePath = srcPath.toLocal8Bit().constData();
string remotePath = destPath.toLocal8Bit().constData(); string remotePath = destPath.toLocal8Bit().constData();
// FTP地址
string ftpServer = "192.168.1.170";
/* 端口号一般为21 */
int port = 666;
/* 用户名 */
string userName = "FTP2";
/* 密码 */
string pwd = "123";
std::vector<string> files; std::vector<string> files;
// 判断上传的是文件还是文件夹标识 // 判断上传的是文件还是文件夹标识
@ -382,74 +318,30 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
size = files.size(); size = files.size();
} }
HINTERNET hint;
HINTERNET hftp;
DWORD dw = GetLastError(); DWORD dw = GetLastError();
do { // 上传文件源为一个文件
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); if (size == 0) {
if (hint == NULL) { int pos = filePath.find_last_of('/');
break; string destFileName = filePath.substr(pos + 1);
} check_save_dir(remotePath);
if (!FtpPutFileA(hftp, filePath.c_str(), destFileName.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
// 创建ftp连接 qDebug() << "ftp put file failed because" << dw;
hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0); }
if (hftp == NULL) { // 上传源为一个文件夹
qDebug() << "ftp connect failed"; else{
break; for (int i = 0; i < size; i++) {
} string tempFilePath = files[i].c_str();
// 获取上传路径中的文件名
// 如果目的路径不存在,一级一级创建 int pos = tempFilePath.find_last_of('/');
// 目的路径格式为:"./dir/dir1/.../" string destFileName = tempFilePath.substr(pos + 1);
int pos = 1; check_save_dir(remotePath + tempFilePath.substr(0,pos+1));
string tempPath; if(!FtpPutFileA(hftp, tempFilePath.c_str(), destFileName.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
while (pos > 0) { qDebug() << "ftp put files failed because " << dw;
// 从第二个“/”开始依次找到目的路径中的“/”位置
pos = remotePath.find_first_of('/', pos + 1);
if (pos == -1)
break;
tempPath = remotePath.substr(0, pos + 1);
//qDebug() << "tempPath is " << tempPath.c_str();
if (_access(tempPath.c_str(), 0) == -1) {
/*if (FtpCreateDirectoryA(hftp, tempPath.c_str()))
qDebug() << "ftp createDirectory successful!";
else {
qDebug() << "ftp createDirectory failed because" << dw;
}*/
FtpCreateDirectoryA(hftp, tempPath.c_str());
}
}
if (FtpSetCurrentDirectoryA(hftp, tempPath.c_str())) {
// 上传文件源为一个文件
if (size == 0) {
int pos = filePath.find_last_of('/');
string destFileName = filePath.substr(pos + 1);
if (!FtpPutFileA(hftp, filePath.c_str(), destFileName.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
qDebug() << "ftp put file failed because" << dw;
}
// 上传源为一个文件夹
else{
for (int i = 0; i < size; i++) {
string tempFilePath = files[i].c_str();
//qDebug() << "tempFilePath is " << tempFilePath.c_str();
// 获取上传路径中的文件名
int pos = tempFilePath.find_last_of('/');
string destFileName = tempFilePath.substr(pos + 1);
//qDebug() << "destFileName is " << destFileName.c_str();
if(!FtpPutFileA(hftp, tempFilePath.c_str(), destFileName.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
qDebug() << "ftp put files failed because " << dw;
}
}
} }
} while (0); }
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
} }
//*****************************************已经调试好,没有理清逻辑前不要动**********************
void ExportDataThread::GetFiles(string path, std::vector<string>& files) { void ExportDataThread::GetFiles(string path, std::vector<string>& files) {
//文件句柄 //文件句柄
intptr_t hFile = 0; intptr_t hFile = 0;

@ -12,6 +12,7 @@
#include "cigarette.h" #include "cigarette.h"
#include "qdebug.h" #include "qdebug.h"
#define EXPORTDATA_FILE "camera%1_data_info.xml" #define EXPORTDATA_FILE "camera%1_data_info.xml"
#include <wininet.h>
using namespace tinyxml2; using namespace tinyxml2;
@ -126,10 +127,12 @@ protected:
void check_save_dir(std::string dir_path); void check_save_dir(std::string dir_path);
public: public:
bool b_quit; bool b_quit;
std::vector<std::string> vec_save_dirs;
tinyxml2::XMLDocument *pDocument[NumberOfSupportedCameras]; tinyxml2::XMLDocument *pDocument[NumberOfSupportedCameras];
int flag; int flag;
HINTERNET hint;
HINTERNET hftp;
public slots: public slots:
void EDrecMsgFromCigarette(); void EDrecMsgFromCigarette();
}; };

Loading…
Cancel
Save