空間商網(wǎng)站站長之家域名查詢官網(wǎng)
文章目錄
- 一、簡(jiǎn)要概述
- 二、封裝過程
- 1. 引入依賴
- 2. 定義腳本執(zhí)行類
- 三、單元測(cè)試
- 四、其他資源
一、簡(jiǎn)要概述
在Linux中curl是一個(gè)利用URL規(guī)則在命令行下工作的文件傳輸工具,可以說是一款很強(qiáng)大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具,但按傳統(tǒng),習(xí)慣稱curl為下載工具。它被廣泛應(yīng)用在Unix、多種Linux發(fā)行版中,并且有DOS和Win32、Win64下的移植版本。
借助JAVA的shell腳本執(zhí)行方法,我們可以在curl命令支持下,封裝出一個(gè)代碼精簡(jiǎn)且功能豐富的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個(gè)方法:
exec
適用于不帶返回值或返回值為非String類型的接口調(diào)用。
execute
帶String類返回值,適用于restful接口json數(shù)據(jù)返回值的接口調(diào)用。
三、單元測(cè)試
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命令高級(jí)用法,請(qǐng)參考curl文檔
另外,在線接口文檔knife4j、httpbin等已經(jīng)集成了等價(jià)curl命令,各位可以拷貝測(cè)試自行探索。
注意:如果需要將包含此工具的工程打包成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
有任何問題和建議,都可以向我提問討論,大家一起進(jìn)步,謝謝!
-over-