HttpClient實現(xiàn)表單提交上傳文件
本文實例為大家分享了HttpClient實現(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實現(xiàn)文件上傳功能
- C# HttpClient Post參數(shù)同時上傳文件的實現(xiàn)
- 基于HttpClient上傳文件中文名亂碼的解決
- C# 使用HttpClient上傳文件并附帶其他參數(shù)的步驟
- Android引用開源框架通過AsyncHttpClient實現(xiàn)文件上傳
- 使用HttpClient實現(xiàn)文件的上傳下載方法
- HttpClient通過Post上傳文件的實例代碼
- httpclient模擬post請求json封裝表單數(shù)據(jù)的實現(xiàn)方法
- Java利用HttpClient模擬POST表單操作應(yīng)用及注意事項
相關(guān)文章
SpringCloud gateway request的body驗證或修改方式
這篇文章主要介紹了SpringCloud gateway request的body驗證或修改方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot常用注解@RestControllerAdvice詳解
這篇文章主要介紹了SpringBoot常用注解@RestControllerAdvice詳解,@RestControllerAdvice是一個組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,因此@RestControllerAdvice本質(zhì)上是個Component,需要的朋友可以參考下2024-01-01Java操作MongoDB數(shù)據(jù)庫的示例代碼
這篇文章主要介紹了Java操作MongoDB的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04slf4j?jcl?jul?log4j1?log4j2?logback各組件系統(tǒng)日志切換
這篇文章主要介紹了slf4j、jcl、jul、log4j1、log4j2、logback的大總結(jié),各個組件的jar包以及目前系統(tǒng)日志需要切換實現(xiàn)方式的方法,有需要的朋友可以借鑒參考下2022-03-03MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權(quán)限控制的方法
MyBatis-Plus是一款基于MyBatis的增強(qiáng)工具,它提供了一些便捷的功能和增強(qiáng)的查詢能力,數(shù)據(jù)權(quán)限控制是在系統(tǒng)中對用戶訪問數(shù)據(jù)進(jìn)行限制的一種機(jī)制,這篇文章主要給大家介紹了關(guān)于MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權(quán)限控制的相關(guān)資料,需要的朋友可以參考下2024-01-01