中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

空間商網(wǎng)站網(wǎng)站是如何建立的

空間商網(wǎng)站,網(wǎng)站是如何建立的,蘇州新途網(wǎng)絡(luò)科技有限公司,wordpress英文語言包文章目錄 一、簡要概述二、封裝過程1. 引入依賴2. 定義腳本執(zhí)行類 三、單元測試四、其他資源 一、簡要概述 在Linux中curl是一個利用URL規(guī)則在命令行下工作的文件傳輸工具,可以說是一款很強大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具&…

文章目錄

  • 一、簡要概述
  • 二、封裝過程
    • 1. 引入依賴
    • 2. 定義腳本執(zhí)行類
  • 三、單元測試
  • 四、其他資源

一、簡要概述

在Linux中curl是一個利用URL規(guī)則在命令行下工作的文件傳輸工具,可以說是一款很強大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具,但按傳統(tǒng),習(xí)慣稱curl為下載工具。它被廣泛應(yīng)用在Unix、多種Linux發(fā)行版中,并且有DOS和Win32、Win64下的移植版本。

借助JAVA的shell腳本執(zhí)行方法,我們可以在curl命令支持下,封裝出一個代碼精簡且功能豐富的HTTP調(diào)用工具類。

二、封裝過程

1. 引入依賴

pom.xml

<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.16</version>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.5</version>
</dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.15.0</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version><scope>provided</scope>
</dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.5.2</version>
</dependency>

2. 定義腳本執(zhí)行類

ShellExecutor.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;import lombok.extern.slf4j.Slf4j;@Slf4j
public class ShellExecutor
{/*** execute命令* * @param command* @throws IOException*/public static void exec(String command){try{log.info("????? WILL EXECUTE COMMAND: {} ?????", command);String[] cmd = SystemUtils.IS_OS_WINDOWS ? new String[] {"cmd", "/c", command} : new String[] {"/bin/sh", "-c", command};Runtime.getRuntime().exec(cmd);}catch (IOException e){log.error(e.getMessage(), e);}}/*** execute命令* * @param command* @return 執(zhí)行結(jié)果* @throws IOException*/public static String execute(String command){try{log.info("????? WILL EXECUTE COMMAND: {} ?????", command);String[] cmd = SystemUtils.IS_OS_WINDOWS ? new String[] {"cmd", "/c", command} : new String[] {"/bin/sh", "-c", command};List<String> resultList = new ArrayList<>();try (BufferedReader br = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(cmd).getInputStream()))){String line;while ((line = br.readLine()) != null){resultList.add(line);}}return StringUtils.join(resultList, System.lineSeparator());}catch (IOException e){log.error(e.getMessage(), e);return "";}}
}

上面的代碼中,我們我們定義了2個方法:
exec 適用于不帶返回值或返回值為非String類型的接口調(diào)用。

execute帶String類返回值,適用于restful接口json數(shù)據(jù)返回值的接口調(diào)用。

三、單元測試


import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;import lombok.extern.slf4j.Slf4j;@Slf4j
public class ShellExecutorTest
{@Testpublic void test001()throws IOException{String response = ShellExecutor.execute("curl https://00fly.online/upload/data.json");log.info(response);}@Testpublic void test002()throws IOException{ShellExecutor.exec("curl -X GET -H  \"Accept:image/jpeg\" -H  \"Content-Type:application/x-www-form-urlencoded\" \"https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg\" --output cat.jpg");}@Testpublic void test003()throws IOException{String response = ShellExecutor.execute("curl https://00fly.online/upload/data.json");FileUtils.writeStringToFile(new File("test.json"), response, StandardCharsets.UTF_8, false);}@Testpublic void test004()throws IOException{ShellExecutor.exec("curl https://00fly.online/upload/data.json  -o test.json");ShellExecutor.exec("curl https://00fly.online/upload/data.json  --output test2.json");}@Testpublic void test005()throws IOException{// 模仿瀏覽器、偽造refererString response = ShellExecutor.execute("curl -A \"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)\" -e \"blog.csdn.net\" \"https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=20&businessType=lately&noMore=false&username=qq_16127313\"");log.info(response);}@Testpublic void test006()throws IOException{// 偽造refererString response = ShellExecutor.execute("curl -e \"blog.csdn.net\" \"https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=20&businessType=lately&noMore=false&username=qq_16127313\"");log.info(response);}
}

四、其他資源

更多CURL命令高級用法,請參考curl文檔

另外,在線接口文檔knife4j、httpbin等已經(jīng)集成了等價curl命令,各位可以拷貝測試自行探索。

注意:如果需要將包含此工具的工程打包成docker鏡像,一定要在鏡像構(gòu)建文件Dockerfile中安裝curl

#安裝curl
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update && apk add curl

在這里插入圖片描述
在這里插入圖片描述


有任何問題和建議,都可以向我提問討論,大家一起進步,謝謝!

-over-

http://www.risenshineclean.com/news/52007.html

相關(guān)文章:

  • 哪個網(wǎng)站可以做圖片杭州專業(yè)seo公司
  • 深圳制作手機網(wǎng)站制作app平臺需要多少錢
  • 平臺網(wǎng)站設(shè)計廣西壯族自治區(qū)免費百度推廣
  • 山西網(wǎng)站建設(shè)推廣百度手機端排名如何優(yōu)化
  • 上海網(wǎng)站推廣珈維互動營銷案例100
  • 湖南網(wǎng)站seo公司迅雷bt磁力鏈 最好用的搜索引擎
  • 用django做的網(wǎng)站百度品牌
  • 邢臺市的做網(wǎng)站制作公司江蘇網(wǎng)站seo設(shè)計
  • 代做畢設(shè)的網(wǎng)站廣州網(wǎng)站優(yōu)化運營
  • 計算機應(yīng)用技術(shù)專業(yè)網(wǎng)站開發(fā)方向大數(shù)據(jù)營銷案例分析
  • 長沙創(chuàng)建一個網(wǎng)站需要多少錢深圳寶安seo外包
  • 做設(shè)計網(wǎng)站模塊的網(wǎng)站軟文代寫價格
  • 網(wǎng)站項目建設(shè)背景seo發(fā)外鏈的網(wǎng)站
  • 網(wǎng)站開發(fā)大數(shù)據(jù)鄭州關(guān)鍵詞優(yōu)化費用
  • 企業(yè)網(wǎng)站的種類中國網(wǎng)絡(luò)營銷公司排名
  • 靖江市屬于哪里有做網(wǎng)站的網(wǎng)絡(luò)銷售這個工作到底怎么樣
  • 網(wǎng)頁制作與網(wǎng)站建設(shè)試題如何優(yōu)化關(guān)鍵詞
  • 秦皇島做網(wǎng)站優(yōu)化公司nba最新排名
  • 影視網(wǎng)站建設(shè)教程公司網(wǎng)頁
  • 國外比較好的資源網(wǎng)站企業(yè)網(wǎng)站seo診斷報告
  • 短視頻素材下載網(wǎng)站 免費輿情分析
  • 網(wǎng)站如何快速被baud百度一下
  • 網(wǎng)站如何做點擊鏈接地址網(wǎng)絡(luò)推廣是以企業(yè)產(chǎn)品或服務(wù)
  • 彩妝網(wǎng)站模板手機cpu性能增強軟件
  • 北京專業(yè)建設(shè)網(wǎng)站公司哪家好seo推廣官網(wǎng)
  • 小型電子商務(wù)網(wǎng)站規(guī)劃廊坊關(guān)鍵詞優(yōu)化報價
  • 四川長昕建設(shè)工程有限公司網(wǎng)站友情鏈接怎么連
  • 51ape是誰做的網(wǎng)站推廣app最快的方法
  • 長春市長春網(wǎng)站建設(shè)網(wǎng)百度免費安裝
  • 網(wǎng)站建設(shè)的目標(biāo)是什么 提供了哪些欄目seo崗位是什么意思