httpclient模拟表单上传参数和文件
2018-02-08 11:53
309 查看
上传文件需要添加httpmime.jar支持,mime多用途互联网邮件扩展类型。
/**
* 表单提交参数和文件
* @param params
* @param files
* @return
*/
public static String postForm(String urlPath, Map<String, String> params, List<File> files){
System.out.println("请求地址:"+urlPath);
HttpPost httpPost = new HttpPost(urlPath);
CloseableHttpClient httpClient = HttpClients.createDefault();
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
// 文件参数
if(files!=null&&files.size()>0){
// for(File file : files){
// FileBody fileBody = new FileBody(file);
// // <input type="file" name="fileName"/>
// entityBuilder.addPart(file.getName(), fileBody);
// }
// 用特定的字段名,接收方用对应的字段名接收
entityBuilder.addPart("file3", new FileBody(files.get(0)));
entityBuilder.addPart("file4", new FileBody(files.get(1)));
}
// 字符串参数
if(params!=null&&!params.isEmpty()){
for(String key : params.keySet()){
StringBody stringBody = new StringBody(params.get(key), ContentType.create("text/plain", CHARSET));
// <input type="text" name="userName" value="userName">
entityBuilder.addPart(key, stringBody);
}
}
try {
HttpEntity httpEntity = entityBuilder.build();
httpPost.setEntity(httpEntity);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT) // read time out
.build();
httpPost.setConfig(config); // 请求配置
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
int responseCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("【状态码:"+responseCode+"】");
InputStream in = httpResponse.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String readLine = null;
while((readLine=bufferedReader.readLine())!=null){
sb.append(readLine);
}
System.out.println("返回报文:"+sb.toString());
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
System.out.println("请求异常");
return null;
}
}
/**
* 表单提交参数和文件
* @param params
* @param files
* @return
*/
public static String postForm(String urlPath, Map<String, String> params, List<File> files){
System.out.println("请求地址:"+urlPath);
HttpPost httpPost = new HttpPost(urlPath);
CloseableHttpClient httpClient = HttpClients.createDefault();
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
// 文件参数
if(files!=null&&files.size()>0){
// for(File file : files){
// FileBody fileBody = new FileBody(file);
// // <input type="file" name="fileName"/>
// entityBuilder.addPart(file.getName(), fileBody);
// }
// 用特定的字段名,接收方用对应的字段名接收
entityBuilder.addPart("file3", new FileBody(files.get(0)));
entityBuilder.addPart("file4", new FileBody(files.get(1)));
}
// 字符串参数
if(params!=null&&!params.isEmpty()){
for(String key : params.keySet()){
StringBody stringBody = new StringBody(params.get(key), ContentType.create("text/plain", CHARSET));
// <input type="text" name="userName" value="userName">
entityBuilder.addPart(key, stringBody);
}
}
try {
HttpEntity httpEntity = entityBuilder.build();
httpPost.setEntity(httpEntity);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT) // read time out
.build();
httpPost.setConfig(config); // 请求配置
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
int responseCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("【状态码:"+responseCode+"】");
InputStream in = httpResponse.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String readLine = null;
while((readLine=bufferedReader.readLine())!=null){
sb.append(readLine);
}
System.out.println("返回报文:"+sb.toString());
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
System.out.println("请求异常");
return null;
}
}
相关文章推荐
- 利用HttpClient模拟form表单将文件上传到Servlet
- 使用httpclient模拟表单上传文件,后台用struts2接收
- CURL模拟表单post提交及相关常用参数的使用(包括提交表单同时上传文件)
- CURL模拟表单post提交及相关常用参数的使用(包括提交表单同时上传文件)
- HttpClien实现使用post方式模拟表单上传大文件和字符参数
- 模拟提交有文件上传的表单(通过http模拟上传文件)
- [置顶] FineUploader文件/图片和form表单参数一起上传,从入门到精通教程
- HttpClient通过post上传文件和提交参数
- nodejs 实现模拟form表单上传文件
- php Socket模拟表单上传文件函数_学习
- Servlet获取form表单上传文件及其他参数
- 使用python模拟post表单上传文件
- Net模拟http表单文件上传
- c# 模拟表单提交,post form 上传文件、大数据内容
- iOS网络 POST模拟表单上传单个与多个文件(直接调用分类里的方法即可)
- Java使用HTTP编程模拟多参数多文件表单信息的请求与处理
- 使用httpclient提交表单方式上传文件
- Asp.Net模拟表单提交数据和上传文件的实现代码
- Spring MVC实现的带参数的form表单多文件上传
- java客户端模拟表单上传文件