HttpClient通过post上传文件和提交参数
来源:互联网 发布:广州疯狂java在哪 编辑:程序博客网 时间:2024/06/09 14:17
上传:
public void postFile(String imgpath, String fileUuid) throws Exception{
String response = "";Properties pro = GetProperties.getKey("wgh.properties");
String fileurl = pro.getProperty("fileUrl");
PostMethod postMethod = new PostMethod(fileurl);
HttpClient client = new HttpClient();
String resource = this.getClass().getClassLoader().getResource("").getPath();
resource = resource.substring(1, resource.indexOf("WEB-INF/"));
String imgfile = resource + imgpath;
File file = new File(imgfile);
if (file.exists()) {
//FilePart:用来上传文件的类
FilePart fp = new FilePart("file", file);fp.setCharSet("utf-8");
fp.setContentType("image/*");
//StringPart:普通文本参数
StringPart idPart = new StringPart("fileUuid", fileUuid);
idPart.setCharSet("utf-8");
Part[] parts = {idPart, fp};
//HttpClient使用MultipartRequestEntity包装
MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(mre);
//由于要上传的文件可能比较大,因此在此设置最大的连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
InputStream inputStream = postMethod.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
response = stringBuffer.toString();
} else {
response = "fail";
}
System.out.println("图片同步结果:" + response);
}
}
接收:
//new一个DiskFileItemFactory类型的对象factory(文件工厂)
DiskFileItemFactory factory = new DiskFileItemFactory();
//为factory设置是否将上传文件已临时文件的形式保存在磁盘的临界值
//(以字节为单位的int值)
factory.setSizeThreshold(2048 * 1024);
//文件上传监听器
UploadProgressListener getBarListener = new UploadProgressListener
(request);
//new一个ServletFileUpload类型的对象upload
ServletFileUpload upload = new ServletFileUpload(factory);
//监听upload
upload.setProgressListener(getBarListener);
//请求体中获取参数赋值到formList
List formList = upload.parseRequest(request);
//建立迭代器formItem
Iterator<Object> formItem = formList.iterator();
//当formItem序列中还有元素时
while (formItem.hasNext()) {
//建立一个FileItem类的对象item
FileItem item = (FileItem) formItem.next();
//判断item封装的数据是一个普通文本表单字段,
//还是一个文件上传表单字段,
//如果是普通表单字段则返回true,否则返回false
//此处为文件上传表单字段不做任何操作
if (!item.isFormField()) {
}else{//普通文本表单字段
//定义fieldName用于返回表单标签name属性的值
String fieldName = item.getFieldName();
if (fieldName.equals("user")) {//值为user
//input_user=item的UTF-8格式
input_user = item.getString("UTF-8");
}
if (fieldName.equals("password")) {//值为password
//input_passw=item的UTF-8格式
input_passw = item.getString("UTF-8");
}
if (fieldName.equals("isselect")) {//值为isselect
//select_ts=item的UTF-8格式
select_ts = item.getString("UTF-8");
}
}
- HttpClient通过post上传文件和提交参数
- HttpClient通过post上传文件和提交参数
- HttpClient通过post上传文件和提交参数
- httpClient post提交文件上传参数的方法
- HttpClient通过Post上传文件
- HttpClient 通过Post 上传文件。
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- android http通过post上传文件和提交参数(通过拼装协议)
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- HttpClient使用Post和Get提交参数
- 大数模板
- python圣斗士修炼(十二):给你介绍个对象!
- OkHttp封装
- 接口又多又杂怎么办?试试用eolinker轻松管理API接口文档
- Eclipse进行远程调试linux 环境的tomcat配置
- HttpClient通过post上传文件和提交参数
- Linux内核编译安装
- java设计模式之代理模式(静态代理)
- 清除编译的内核源码文件
- 二叉树的顺序存储和基本操作
- tack笔记
- 在O(1)时间删除链表结点
- Spring Boot 整合 log4j2 实现日志管理
- Pahom on Water HDU