HttpClient實(shí)現(xiàn)表單提交上傳文件
本文實(shí)例為大家分享了HttpClient實(shí)現(xiàn)表單提交上傳文件的具體代碼,供大家參考,具體內(nèi)容如下
需求:如何利用HttpClient,發(fā)起post請求,模擬表單提交,在后端上傳文件?
上傳文件接口:
/**
? ? ?* 文件上傳測試接口
? ? ?* @return
? ? ?*/
? ? @PostMapping("/upload")
? ? public Object uploadFileTest(@RequestParam("file") MultipartFile file, @RequestParam("file_name") String file_name, @RequestParam("file_code") String file_code) {
? ? ? ? System.out.println(file_name+","+file_code);
? ? ? ? return "OK";
? ? }啟動后,可以通過postman進(jìn)行調(diào)用,最后打印OK,表示接口可以調(diào)用通

然后就是正式編碼環(huán)節(jié)了
首先引入需要的包:
<dependency> ? ?<groupId>org.apache.httpcomponents</groupId> ? ?<artifactId>httpmime</artifactId> ? ?<version>4.5.3</version> </dependency>
編寫main方法,直接發(fā)起調(diào)用
?public static String httpClientUploadFile(String url, File file) {
? ? ? ? CloseableHttpClient httpClient = HttpClients.createDefault();
? ? ? ? String result = "";
? ? ? ? //每個post參數(shù)之間的分隔。隨意設(shè)定,只要不會和其他的字符串重復(fù)即可。
? ? ? ? String boundary = "--------------4585696313564699";
? ? ? ? try {
? ? ? ? ? ? //文件名
? ? ? ? ? ? String fileName = file.getName();
? ? ? ? ? ? HttpPost httpPost = new HttpPost(url);
? ? ? ? ? ? //設(shè)置請求頭
? ? ? ? ? ? httpPost.setHeader("Content-Type", "multipart/form-data; boundary="+boundary);
?
? ? ? ? ? ? //HttpEntity builder
? ? ? ? ? ? MultipartEntityBuilder builder = MultipartEntityBuilder.create();
? ? ? ? ? ? //字符編碼
? ? ? ? ? ? builder.setCharset(Charset.forName("UTF-8"));
? ? ? ? ? ? //模擬瀏覽器
? ? ? ? ? ? builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
? ? ? ? ? ? builder.setContentType(ContentType.MULTIPART_FORM_DATA);
? ? ? ? ? ? //boundary
? ? ? ? ? ? builder.setBoundary(boundary);
? ? ? ? ? ? //multipart/form-data
? ? ? ? ? ? builder.addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY));
? ? ? ? ? ? // binary
? ? ? ? ? ? //builder.addBinaryBody("name=\"file\"; filename=\"mysql.docx\"", new FileInputStream(file), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
? ? ? ? ? ? //其他參數(shù)
? ? ? ? ? ? //builder.addTextBody("file_name", fileName, ContentType.create("text/plain", Consts.UTF_8));
? ? ? ? ? ? builder.addTextBody("file_name", fileName, ContentType.MULTIPART_FORM_DATA);
? ? ? ? ? ? builder.addTextBody("file_code", "111111", ContentType.MULTIPART_FORM_DATA);
? ? ? ? ? ? //HttpEntity
? ? ? ? ? ? HttpEntity entity = builder.build();
? ? ? ? ? ? httpPost.setEntity(entity);
? ? ? ? ? ? // 執(zhí)行提交
? ? ? ? ? ? HttpResponse response = httpClient.execute(httpPost);
? ? ? ? ? ? //響應(yīng)
? ? ? ? ? ? HttpEntity responseEntity = response.getEntity();
? ? ? ? ? ? if (responseEntity != null) {
? ? ? ? ? ? ? ? // 將響應(yīng)內(nèi)容轉(zhuǎn)換為字符串
? ? ? ? ? ? ? ? result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? httpClient.close();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println("result" + result);
? ? ? ? return result;
? ? }
?
? ? //main 方法
? ? public static void main(String[] args) {
? ? ? ? httpClientUploadFile("http://127.0.0.1:8080/test/tempA/upload",new File("e:/tmp/mysql.docx"));
? ? }最后返回OK,調(diào)用成功
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- HttpClient實(shí)現(xiàn)文件上傳功能
- C# HttpClient Post參數(shù)同時上傳文件的實(shí)現(xiàn)
- 基于HttpClient上傳文件中文名亂碼的解決
- C# 使用HttpClient上傳文件并附帶其他參數(shù)的步驟
- Android引用開源框架通過AsyncHttpClient實(shí)現(xiàn)文件上傳
- 使用HttpClient實(shí)現(xiàn)文件的上傳下載方法
- HttpClient通過Post上傳文件的實(shí)例代碼
- httpclient模擬post請求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
- Java利用HttpClient模擬POST表單操作應(yīng)用及注意事項(xiàng)
相關(guān)文章
SpringBoot+Vue前后端分離實(shí)現(xiàn)審核功能的示例
在實(shí)際開發(fā)中,審核功能是一個非常常用的功能,本文就來介紹一下使用SpringBoot+Vue前后端分離實(shí)現(xiàn)審核功能的示例,具有一定的參考價值,感興趣的可以了解一下2024-02-02
解決Mybatis-Plus操作分頁后數(shù)據(jù)失效問題
這篇文章主要介紹了解決Mybatis-Plus操作分頁后數(shù)據(jù)失效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
springBoot+mybatis-plus實(shí)現(xiàn)監(jiān)聽mysql數(shù)據(jù)庫的數(shù)據(jù)增刪改
mybatis-plus技術(shù)是簡化了繁瑣的代碼操作,把增刪改查的語句都內(nèi)置了,直接調(diào)用就可以實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查了,這篇文章主要給大家介紹了關(guān)于springBoot+mybatis-plus實(shí)現(xiàn)監(jiān)聽mysql數(shù)據(jù)庫數(shù)據(jù)增刪改的相關(guān)資料,需要的朋友可以參考下2024-01-01
Java 如何將表格數(shù)據(jù)導(dǎo)入word文檔中
這篇文章主要介紹了Java將表格數(shù)據(jù)導(dǎo)入word文檔中的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

