Traverse the folders, then read each file, and then call your upload interface to upload files one by one


QStringList file_path;
QDir dir(“C:/2017-09-08”);
QFileInfoList info_list = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::AllDirs);
foreach(QFileInfo file_info, info_list)
{

if (file_info.isFile())
{

qDebug() << "remove file  : " << file_info.absoluteFilePath();
file_path.append(file_info.absoluteFilePath());
QString file_name = file_info.fileName();
qDebug() <<file_name<<endl;
n++;

}
}
qDebug() <<n<<endl;
for(int i=0;i<n;i++)
{
QString filepath = file_path.at(i);
file = new QFile(filepath);
file->open(QIODevice::ReadOnly);
QNetworkAccessManager *accessManager1 = new QNetworkAccessManager(this); //在服务器上创建目录
accessManager1->setNetworkAccessible(QNetworkAccessManager::Accessible);
QByteArray data;
QUrl url1(“http://localhost/createFolder.php?foldername=upload”);
QNetworkRequest request1(url1);
request1.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String(“application/octet-stream”));
accessManager1->post(request1, data);

QNetworkAccessManager *accessManager2 = new QNetworkAccessManager(this); //Upload files to this directory
accessManager2->setNetworkAccessible(QNetworkAccessManager::Accessible);
QByteArray byte_file = file->readAll();
QUrl url2(" http://localhost/upload .php?foldername=upload&filename=99.txt ”); //Upload the file specified here to the upload directory in the HTTP server directory
QNetworkRequest request2(url2);
request2.setHeader(QNetworkRequest::ContentTypeHeader, “application/octet- stream”);
reply = accessManager2->post(request2, byte_file);

progressBar = new QProgressBar();
progressBar->setValue(0);
progressBar->show();

connect(accessManager2,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(loadError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(uploadProgress(qint64 ,qint64)), this, SLOT(loadProgress(qint64 ,qint64)));
}

///

1. Traverse the list of picture names

public List fileList(HttpServletRequest request){ List list = new ArrayList(); String path = request.getSession().getServletContext().getRealPath("/");//The absolute path of the project File file = new File(path+" /file/img");//Folder path File[] files = file.listFiles();//Traverse the folder if(null!=files){ for (int i = 0; i <files.length; i++){ File file1 = files[i]; String name = file1.getName();//Get the picture name list.add(name); } return list; }else{ return null; } }















Copyright statement: This article is the original article of the CSDN blogger "Flykos", following the CC 4.0 by-sa copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/Flykos/article/details/52168543

Guess you like

Origin blog.csdn.net/weixin_39354151/article/details/99304577