使用httpcomponents上传文件3个不同的完整案例代码
时间: 2024-05-14 18:18:58 浏览: 7
以下是三个使用httpcomponents上传文件的完整案例代码:
1. 使用MultipartEntityBuilder上传文件:
```
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
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.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
public class HttpComponentsUploadFileExample1 {
public static void main(String[] args) throws IOException {
String url = "http://localhost:8080/upload";
String filePath = "/path/to/file";
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
File file = new File(filePath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName());
HttpEntity entity = builder.build();
httppost.setEntity(entity);
httpclient.execute(httppost);
}
}
```
2. 使用FileBody上传文件:
```
import java.io.File;
import java.io.IOException;
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.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClients;
public class HttpComponentsUploadFileExample2 {
public static void main(String[] args) throws IOException {
String url = "http://localhost:8080/upload";
String filePath = "/path/to/file";
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
File file = new File(filePath);
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
HttpEntity entity = MultipartEntityBuilder.create()
.addPart("file", fileBody)
.build();
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
System.out.println(response.getStatusLine());
}
}
```
3. 使用InputStreamBody上传文件:
```
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.HttpClients;
public class HttpComponentsUploadFileExample3 {
public static void main(String[] args) throws IOException {
String url = "http://localhost:8080/upload";
String filePath = "/path/to/file";
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamBody inputStreamBody = new InputStreamBody(fileInputStream, ContentType.DEFAULT_BINARY, file.getName());
HttpEntity entity = MultipartEntityBuilder.create()
.addPart("file", inputStreamBody)
.build();
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
System.out.println(response.getStatusLine());
}
}
```