通过HttpClient4.5模拟Form表单文件上传
public static void main(String[] args) {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream("D:\\test.pdf");
HttpPost httpPost = new HttpPost("http://127.0.0.1/test");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("wj", inputStream, ContentType.create("multipart/form-data"), "test.pdf");
//构建请求参数 普通表单项
StringBody stringBody = new StringBody("100001", ContentType.MULTIPART_FORM_DATA);
builder.addPart("serial_no", stringBody);
stringBody = new StringBody("200", ContentType.MULTIPART_FORM_DATA);
builder.addPart("xPosition", stringBody);
stringBody = new StringBody("200", ContentType.MULTIPART_FORM_DATA);
builder.addPart("yPosition", stringBody);
stringBody = new StringBody("2", ContentType.MULTIPART_FORM_DATA);
builder.addPart("sealType", stringBody);
stringBody = new StringBody("1", ContentType.MULTIPART_FORM_DATA);
builder.addPart("paegNum", stringBody);
builder.setCharset(CharsetUtils.get("UTF-8"));
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
response = httpclient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode:" + statusCode);
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = response.getEntity();
result = EntityUtils.toString(resEntity, "UTF-8");
}
System.out.println("result:" + result);
} catch (IOException ex) {
Logger.getLogger(QzTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (null != inputStream) {
inputStream.close();
}
} catch (IOException ex) {
Logger.getLogger(QzTest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
依赖包:
commons-logging-1.2.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
httpmime-4.5.3.jar
通过HttpClient4.5模拟Form表单文件上传的更多相关文章
- Ajax(form表单文件上传、请求头之contentType、Ajax传递json数据、Ajax文件上传)
form表单文件上传 上菜 file_put.html <form action="" method="post" enctype="multi ...
- form表单文件上传 servlet文件接收
需要导入jar包 commons-fileupload-1.3.2.jar commons-io-2.5.jar Upload.Jsp代码 <%@ page language="jav ...
- form表单文件上传提交且接口回调显示提交成功
前端: <form method="post" enctype="multipart/form-data" id="formSubmit&quo ...
- SSM+form表单文件上传
这里介绍SSM如何配置上传文件 配置springmvc.xml: <!--配置上传下载--> <bean id="multipartResolver" class ...
- 模拟form表单请求上传文件
发请求 public string CameraFileUpload(string url,string path,string serverPath,string uploadfileName) { ...
- 基于Spring3 MVC实现基于form表单文件上传
http://blog.csdn.net/jia20003/article/details/8474374/
- 表单文件上传,ajax文件上传
原创链接:http://www.cnblogs.com/yanqin/p/5345562.html html代码 index.jsp(表单文件上传) <form action="sh ...
- Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request对象方法,属性和Response对象,form表单的上传
Django---CBV和FBV的使用,CBV的流程,给视图加装饰器,Request请求对象方法,属性和Response响应对象,form表单的上传 一丶CBV和FBV 在Django中存 ...
- 混合表单文件上传到数据库(基于TOMCAT)
在实际的开发中在实现文件上传的同时肯定还有其他信息需要保存到数据库,就像混合表单在上传完毕之后需要将提交的基本信息插入数据库. 在这个demo中需要用到这个架包来帮助实现 1.定义一个公共类实现文件上 ...
随机推荐
- 将Excel中的数据批量导入数据库表
private boolean import_to_database(String excel_path) throws BiffException, IOException, HsException ...
- 【docker】【Gitlab】gitlab中clone项目时,IP地址是一串数字(内网Gitlab的IP地址不正确)的问题解决
首次在内网搭建Gitlab环境,在成功后在Gitlab上新建了一个项目. 然而在IDEA上clone项目时发现,项目地址如下: git@0096ce63c43f:root/jump.git 或者这样 ...
- git服务器的建立——Git折腾小记
转自:http://blog.csdn.net/xsl1990/article/details/25486211 如果你能看到一些sshd相关的进程信息,则说明你已经有这个服务了,否则(或者你想更新的 ...
- hex string 换转
hex2string std::stringstream R; R << std::hex << 0x1254; DWORD Str2Hex(string str){ retu ...
- anaconda、pip配置国内镜像
一.anaconda配置镜像查看源:conda config --show-sources在Mac and Linux下:conda config --add channels https://mir ...
- [转]通过查看mysql 配置参数、状态来优化你的mysql
From : http://wangwei007.blog.51cto.com/68019/967278 mysql的监控方法大致分为两类: 1.连接到mysql数据库内部,使用show status ...
- Spring Boot 文件上传与下载
原文地址: https://www.cnblogs.com/studyDetail/p/7003253.html 1.在pom.xml文件中添加依赖 <project xmlns="h ...
- remote: GitLab: You are not allowed to push code to protected branches on this project.
"C:\Program Files\Git\bin\git.exe" push --recurse-submodules=check --progress "origin ...
- Centos 7安装protobuf3.6.1
新版本 google protobuf-3.6.1是现在最新版本,添加了新的特性,看说明 下载地址 https://github.com/protocolbuffers/protobuf/releas ...
- [数学] 将长为L的木棒随机折成3段,则3段构成三角形的概率
1.题目 如题,将长为L的木棒随机折成3段,则3段构成三角形的概率是多少? 2.解析 这个与其说是编程题目,倒不如说是个数学题目.可能是你没有见过,或者没有过这种解题思想.(还是那句,一回生两回熟,类 ...