在Java中发送form-data请求,可以使用Apache HttpClient或OkHttp这样的HTTP客户端库来发送请求。
添加依赖
org.apache.httpcomponents httpclient 4.5.13
代码
package request;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ContentType;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;import java.io.File;import java.io.IOException;public class FormDataExample { public static void main(String[] args) throws IOException { String url = "http://127.0.0.1:3000/aaa/fileUpload.jsp"; MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("text2", "value1"); builder.addTextBody("text3", "value2"); builder.addBinaryBody("file1", new File("D:\\fanwei\\学习\\工具地址.txt"), ContentType.APPLICATION_OCTET_STREAM, "filename2.txt"); String StringResult = sendPostFormData(url, builder); System.out.println(StringResult); } public static String sendPostFormData(String url, MultipartEntityBuilder builder){ String StringResult = ""; //不需要手动关闭httpClient HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); // 构建form-data请求体 HttpEntity entity = builder.build(); httpPost.setEntity(entity); // 发送请求并获取响应 HttpResponse response = null; try { response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); StringResult = EntityUtils.toString(responseEntity); } catch (IOException e) { StringResult = "errorException:" + e.getMessage(); e.printStackTrace(); } return StringResult; }}
对于HttpClient对象,HttpClientBuilder.create().build()创建的实例是不需要手动关闭的。
在使用HttpClientBuilder创建HttpClient对象时,不需要手动关闭HttpClient对象。这是因为HttpClientBuilder在创建HttpClient对象时,会自动管理底层的连接池和资源释放。
HttpClientBuilder会创建一个CloseableHttpClient的实例,它实现了Closeable接口,可以自动关闭相关资源。当不再需要使用HttpClient时,可以让垃圾回收器自动回收HttpClient对象,并自动关闭底层的连接池和资源。
需要注意的是,如果使用了连接池,关闭HttpClient并不会立即关闭底层的连接池,而是将连接返回给连接池以供重用。连接池会根据一定的策略来管理连接的生命周期。因此,关闭HttpClient并不会立即关闭与服务器的连接,而是将连接返回给连接池以供下次使用。
总结起来,使用HttpClientBuilder创建的HttpClient对象不需要手动关闭,它会自动管理底层的连接池和资源释放。当不再需要使用HttpClient时,可以让垃圾回收器自动回收HttpClient对象。
添加依赖
org.apache.httpcomponents httpclient 4.5.13
代码
package request;import okhttp3.*;import java.io.File;import java.io.IOException;public class FormDataExample2 { public static void main(String[] args) throws IOException { OkHttpClient client = new OkHttpClient(); // 构建form-data请求体 RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("field1", "value1") .addFormDataPart("field2", "value2") .addFormDataPart("file", "filename.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("D:\\fanwei\\学习\\工具地址.txt"))) .build(); // 构建请求 Request request = new Request.Builder() .url("http://127.0.0.1:3000/aaa/fileUpload.jsp") .post(requestBody) .build(); // 发送请求并获取响应 try (Response response = client.newCall(request).execute()) { String responseBody = response.body().string(); // 处理响应 System.out.println(responseBody); } }}
其他发送请求,请参阅
httpclient 发送请求_httpclient发请求_有梦想的菜的博客-CSDN博客
文件上传,请参阅
Java文件上传_java上传文件_有梦想的菜的博客-CSDN博客
来源地址:https://blog.csdn.net/qq_51534884/article/details/132233372