浏览代码

V0.3.4
1、完成了使用Curl进行ftp上传下载功能

Apple 8 月之前
父节点
当前提交
c638ff12d7
共有 5 个文件被更改,包括 107 次插入13 次删除
  1. 2 2
      CMakeLists.txt
  2. 1 0
      demo/ftp/CMakeLists.txt
  3. 83 1
      demo/ftp/widget.cpp
  4. 4 0
      demo/ftp/widget.h
  5. 17 10
      demo/ftp/widget.ui

+ 2 - 2
CMakeLists.txt

@@ -142,9 +142,9 @@ file(GLOB GLOBAL_SRC
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/mqtt)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/http)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/threadPool)
-# add_subdirectory(${CMAKE_SOURCE_DIR}/demo/ftp)
+add_subdirectory(${CMAKE_SOURCE_DIR}/demo/ftp)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/OneThread)
-add_subdirectory(${CMAKE_SOURCE_DIR}/demo/timer)
+# add_subdirectory(${CMAKE_SOURCE_DIR}/demo/timer)
 
 
 

+ 1 - 0
demo/ftp/CMakeLists.txt

@@ -32,6 +32,7 @@ add_executable(${this_exe}
 target_include_directories(${this_exe} PRIVATE
 
     ${CMAKE_CURRENT_SOURCE_DIR}
+    ${CMAKE_SOURCE_DIR}/External/common
     ${CMAKE_SOURCE_DIR}/External/common/http
     ${CMAKE_SOURCE_DIR}/External/common/LightLog
     ${CMAKE_SOURCE_DIR}/External/common/Logs

+ 83 - 1
demo/ftp/widget.cpp

@@ -15,6 +15,8 @@
 #include "fmtlog.h"
 #include "LightLog.h"
 
+#include "nlohmann/json.hpp"
+#define nJson nlohmann::json
 
 Widget::Widget(QWidget *parent)
     : QWidget(parent)
@@ -34,6 +36,72 @@ Widget::~Widget()
     delete ui;
 }
 
+/* 解析JSON示例 */
+void Widget::parseJSON()
+{
+    SPDLOG_INFO("解析JSON");
+
+    nJson json0;
+    json0["name"] = "microsoft";
+    json0["age"] = 18;
+    json0["bool"] = true;
+    json0["null"] = nullptr;
+    json0["array"] = {1, 2, 3, 4, 5};
+    json0["object"] = {{"key1", "value1"}, {"key2", "value2"}};
+
+    /* 打印json值 */
+    SPDLOG_INFO("json0: {}", json0.dump());
+
+    std::string name = json0["name"];
+    std::string name2 = json0["name"].get<std::string>();
+    std::string name1 = json0.at("name").get<std::string>();
+    std::string name5 = json0.at("name");
+    std::string name4 = json0.value("name", "default");
+    
+    int age = json0["age"].get<int>();
+    bool b = json0["bool"];
+    nJson null = json0["null"];
+    nJson array = json0["array"];
+    nJson object = json0["object"];
+    /* 如果json中的值是null,在赋值给string时可能会报错,可以使用“?”运算符解决 */
+    std::string name3 = json0["name"].is_null() ? "" : json0["name"].get<std::string>();
+
+    /* 从直接到JSON */
+    nJson json1 = R"(
+    {
+        "name": "microsoft",
+        "age": 18,
+        "bool": true,
+        "null": null,
+        "array": [1, 2, 3, 4, 5],
+        "object": {
+            "key1": "value1",
+            "key2": "value2"
+        }
+    }
+    )"_json;
+
+    /* 或者 */
+    std::string str = R"(
+    {
+        "name": "microsoft",
+        "age": 18,
+        "bool": true,
+        "null": null,
+        "array": [1, 2, 3, 4, 5],
+        "object": {
+            "key1": "value1",
+            "key2": "value2"
+        }
+    }
+    )";
+    nJson json2 = nJson::parse(str);
+
+    /* 从JSON到字符串 */
+    std::string str1 = json0.dump();
+
+}
+
 
 void Widget::on_pBtn_connect_clicked()
 {
@@ -54,7 +122,7 @@ void Widget::on_pBtn_downloadFile_clicked()
 {
     SPDLOG_INFO("点击了“下载文件”");
     std::vector<std::string> fileList;
-    bool ret = m_curlFtp.downloadFile("/SSD2/Video/哪吒之魔童降世.mp4", QApplication::applicationDirPath().toStdString() + "/哪吒之魔童降世.mp4");
+    bool ret = m_curlFtp.downloadFile("/SSD2/Video/哪吒之魔童降世.mp4", QApplication::applicationDirPath().toStdString() + "/YPM.mp4");
     if(ret)
     {
         SPDLOG_INFO("下载成功");
@@ -147,3 +215,17 @@ void Widget::on_pBtn_logSpeed_clicked()
     SPDLOG_INFO("LightLog打印耗时: {}", lightLogTime.count());
 }
 
+
+void Widget::on_pBtn_upload_clicked()
+{
+    SPDLOG_INFO("点击了“上传”");
+    bool ret = m_curlFtp.uploadFile(QApplication::applicationDirPath().toStdString() + "/YPM.mp4", "/SSD2/Video/YPM.mp4");
+    if(ret)
+    {
+        SPDLOG_INFO("上传成功");
+    }
+    else
+    {
+        SPDLOG_INFO("上传失败");
+    }
+}

+ 4 - 0
demo/ftp/widget.h

@@ -16,6 +16,8 @@ public:
     Widget(QWidget *parent = nullptr);
     ~Widget();
 
+    void parseJSON();
+
 private slots:
 
     void on_pBtn_connect_clicked();
@@ -26,6 +28,8 @@ private slots:
 
     void on_pBtn_logSpeed_clicked();
 
+    void on_pBtn_upload_clicked();
+
 private:
     Ui::Widget *ui;
 

+ 17 - 10
demo/ftp/widget.ui

@@ -30,16 +30,6 @@
       </property>
      </widget>
     </item>
-    <item row="0" column="0">
-     <widget class="QPushButton" name="pBtn_connect">
-      <property name="text">
-       <string>连接</string>
-      </property>
-     </widget>
-    </item>
-    <item row="2" column="0" colspan="2">
-     <widget class="QLineEdit" name="lineEdit"/>
-    </item>
     <item row="1" column="0">
      <widget class="QPushButton" name="pBtn_downloadVideo">
       <property name="text">
@@ -47,6 +37,9 @@
       </property>
      </widget>
     </item>
+    <item row="3" column="0" colspan="2">
+     <widget class="QLineEdit" name="lineEdit"/>
+    </item>
     <item row="1" column="1">
      <widget class="QPushButton" name="pBtn_logSpeed">
       <property name="text">
@@ -54,6 +47,20 @@
       </property>
      </widget>
     </item>
+    <item row="0" column="0">
+     <widget class="QPushButton" name="pBtn_connect">
+      <property name="text">
+       <string>连接</string>
+      </property>
+     </widget>
+    </item>
+    <item row="2" column="0">
+     <widget class="QPushButton" name="pBtn_upload">
+      <property name="text">
+       <string>上传</string>
+      </property>
+     </widget>
+    </item>
    </layout>
   </widget>
  </widget>