java模拟浏览器做文件上传

所需jar:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<properties>
<httpclient.version>4.5.13</httpclient.version>
<httpcore.version>4.4.14</httpcore.version>
</properties>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.12</version>
</dependency>

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.yzw.http;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
* @author yzw
* @date 2021/1/2
*/
public class Test {
public static String httpClientUploadFile(String url, File file, Map<String,String> params) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
//每个post参数之间的分隔。随意设定,只要不会和其他的字符串重复即可。
String boundary ="--------------20200103121104567";
try {
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);
//boundary
builder.setBoundary(boundary);
//multipart/form-data
builder.addPart("uploadimg",new FileBody(file));//相当于<input name='file' type='file'/>
// binary
// builder.addBinaryBody("name=\"file\"; filename=\"test.txt\"", new FileInputStream(file), ContentType.MULTIPART_FORM_DATA, file.getName());// 文件流
//其他参数
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.addTextBody(entry.getKey(), entry.getValue() );
}
//HttpEntity
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
// 执行提交
HttpResponse response = httpClient.execute(httpPost);
//响应
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
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.err.println("result"+result);
return result;
}

//main 方法
public static void main(String[] args) {
Map<String,String> params = new HashMap<>();

String filePath = new String("C:\\Users\\27184\\Desktop\\项目\\新建文件夹\\的文案.png");
// 把一个普通参数和文件上传给下面这个地址 是一个servlet
String httpPost = "http://1.w2wz.com/upload.php";
httpClientUploadFile(httpPost,new File(filePath),params);
}
}