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進行調(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ù)之間的分隔。隨意設定,只要不會和其他的字符串重復即可。
? ? ? ? String boundary = "--------------4585696313564699";
? ? ? ? try {
? ? ? ? ? ? //文件名
? ? ? ? ? ? String fileName = file.getName();
? ? ? ? ? ? HttpPost httpPost = new HttpPost(url);
? ? ? ? ? ? //設置請求頭
? ? ? ? ? ? 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);
? ? ? ? ? ? //響應
? ? ? ? ? ? HttpEntity responseEntity = response.getEntity();
? ? ? ? ? ? if (responseEntity != null) {
? ? ? ? ? ? ? ? // 將響應內(nèi)容轉換為字符串
? ? ? ? ? ? ? ? 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringCloud gateway request的body驗證或修改方式
這篇文章主要介紹了SpringCloud gateway request的body驗證或修改方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot常用注解@RestControllerAdvice詳解
這篇文章主要介紹了SpringBoot常用注解@RestControllerAdvice詳解,@RestControllerAdvice是一個組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,因此@RestControllerAdvice本質上是個Component,需要的朋友可以參考下2024-01-01
Java操作MongoDB數(shù)據(jù)庫的示例代碼
這篇文章主要介紹了Java操作MongoDB的示例代碼,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下2021-04-04
slf4j?jcl?jul?log4j1?log4j2?logback各組件系統(tǒng)日志切換
這篇文章主要介紹了slf4j、jcl、jul、log4j1、log4j2、logback的大總結,各個組件的jar包以及目前系統(tǒng)日志需要切換實現(xiàn)方式的方法,有需要的朋友可以借鑒參考下2022-03-03
MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權限控制的方法
MyBatis-Plus是一款基于MyBatis的增強工具,它提供了一些便捷的功能和增強的查詢能力,數(shù)據(jù)權限控制是在系統(tǒng)中對用戶訪問數(shù)據(jù)進行限制的一種機制,這篇文章主要給大家介紹了關于MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權限控制的相關資料,需要的朋友可以參考下2024-01-01

