解决ftp传送问题

main
Jeffrey_Li 1 year ago
parent 92d931e56d
commit 2020f6a810

@ -31,43 +31,27 @@ ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent)
XMLDeclaration* declaration = pDocument[index]->NewDeclaration();
pDocument[index]->InsertFirstChild(declaration);
}
}
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (hint == NULL) {
return;
}
//if (error != XML_SUCCESS)
//{
// XMLDeclaration* declaration = pDocument[index]->NewDeclaration();
// pDocument[index]->InsertFirstChild(declaration);
//
// XMLElement* root = pDocument[0]->NewElement("Root");
// pDocument[0]->InsertEndChild(root);
// pDocument[index]->SaveFile(filePath.toLocal8Bit().constData());
//}
/*
_ExportDataInfo data;
data.cameraId = index;
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);
}
// FTP地址
string ftpServer = "192.168.1.170";
/* 端口号一般为21 */
int port = 666;
/* 用户名 */
string userName = "FTP2";
/* 密码 */
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) {
qDebug() << "ftp connect failed";
return;
}
}
void ExportDataThread::init()
@ -88,6 +72,8 @@ void ExportDataThread::stop()
//export_Data_Info_queue->put(data);
_XMLExportDataInfo data;
export_XMLData_Info_queue->put(data);
InternetCloseHandle(hftp);
InternetCloseHandle(hint);
}
bool _ExportDataInfo::getAverageData(map<string, float> &averageData, int index)
@ -138,45 +124,6 @@ bool _ExportDataInfo::getAverageData(map<string, float> &averageData, int index)
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)
{
XMLElement* root = pDocument[data.cameraId]->RootElement();
@ -330,27 +277,24 @@ void ExportDataThread::run()
}
}
//*****************************************已经调试好,没有理清逻辑前不要动**********************
void ExportDataThread::check_save_dir(std::string dir_path)
{
bool b_find = false;
for (int i = 0; i < vec_save_dirs.size(); i++)
{
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 (!dir.exists(dir_str))
{
dir.mkpath(dir_str);
// 如果目的路径不存在,一级一级创建
// 目的路径格式为:"./dir/dir1/.../"
if (FALSE == FtpSetCurrentDirectoryA(hftp, dir_path.c_str())) {
FtpSetCurrentDirectoryA(hftp, "/");
int pos = 1;
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) {
string filePath = srcPath.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;
// 判断上传的是文件还是文件夹标识
@ -382,74 +318,30 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) {
size = files.size();
}
HINTERNET hint;
HINTERNET hftp;
DWORD dw = GetLastError();
do {
hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (hint == NULL) {
break;
}
// 创建ftp连接
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";
break;
}
// 如果目的路径不存在,一级一级创建
// 目的路径格式为:"./dir/dir1/.../"
int pos = 1;
string tempPath;
while (pos > 0) {
// 从第二个“/”开始依次找到目的路径中的“/”位置
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;
}
}
// 上传文件源为一个文件
if (size == 0) {
int pos = filePath.find_last_of('/');
string destFileName = filePath.substr(pos + 1);
check_save_dir(remotePath);
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();
// 获取上传路径中的文件名
int pos = tempFilePath.find_last_of('/');
string destFileName = tempFilePath.substr(pos + 1);
check_save_dir(remotePath + tempFilePath.substr(0,pos+1));
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) {
//文件句柄
intptr_t hFile = 0;

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

Loading…
Cancel
Save