httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数
一、利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单
package com.test.httpclient; import java.io.IOException;
import java.util.Map; import javax.servlet.ServletException; import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.HttpClient; public class SendXmlAction
{
public String execute() throws ServletException, IOException
{
String xmlhead = this.getRequest().getParameter("xmlhead");
String xmlbody = this.getRequest().getParameter("xmlbody");
System.out.println("xmlhead == "+xmlhead);
System.out.println("xmlbody == "+xmlbody); // 用远程服务的URL设置生成POST方法,供HTTP客户端执行
String remoteUrl = "http://**.**.***.***:8888/project/receiveServlet"; PostMethod method = new PostMethod(remoteUrl); // multipart/form-data; boundary=---------------------------7de2b13a790640 //method.addParameter("xmlhead", xmlhead);
//method.addParameter("xmlbody", xmlbody); HttpClient HTTP_CLINET = new HttpClient(); synchronized (HTTP_CLINET)
{
try
{
//使用多重发送方式,发送两个独立的两个XML Part,基于Content-Type="multipart/form-data"形式的表单
Part[] parts = {new StringPart("xmlhead",xmlhead), new StringPart("xmlbody",xmlbody)}; //StringPart和FilePart都可以放进去
RequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams());
method.setRequestEntity(requestEntity); method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 30000);
//链接超时 30秒
HTTP_CLINET.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
//读取超时 30秒
HTTP_CLINET.getHttpConnectionManager().getParams().setSoTimeout(30000); HTTP_CLINET.executeMethod(method); String[] result = new String[2];
result[0] = String.valueOf(method.getStatusCode());
result[1] = method.getResponseBodyAsString();
System.out.println("http status : "+result[0]);
System.out.println("http response : "+result[1]); }
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (method != null)
{
method.releaseConnection();
}
method = null;
}
} return "success";
}
}
二、MultipartRequest接收参数
package com.test.servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.io.File; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import com.oreilly.servlet.MultipartRequest; public class BossServlet extends HttpServlet
{ /** serialVersionUID */
private Logger logger = Logger.getLogger(BossServlet.class); public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
this.doPost(request, response);
} protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// MultipartRequest
String head = null ;
String body = null ; try
{
File fileDir = new File(this.getServletContext().getRealPath("/formhttp"));
if (!fileDir.exists())
{
fileDir.mkdirs();
} int inmaxPostSize = 10 * 1024 * 1024; // utf-8中文编码模式上传文件
MultipartRequest multirequest = new MultipartRequest(request,fileDir.getAbsolutePath(),inmaxPostSize,"UTF-8"); head = multirequest.getParameter("head");
body = multirequest.getParameter("body");
System.out.println("xmlHead2 = " + xmlHead);
System.out.println("xmlBody2 = " + xmlBody);
}
catch (Exception e)
{
e.printStackTrace();
} response.setCharacterEncoding("UTF-8");
response.setContentType("multipart/mixed;boundary=---------------------------7de2b13a790640");
PrintWriter out = response.getWriter(); String res = null;
try
{
res = .....
}
catch (Exception e)
{
e.printStackTrace();
} if (!(res == null || "".equals(res)))
{
try
{
out.println(res);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
out.close();
} }
} public void init() throws ServletException
{
super.init();
}
}
若发送基于Content-Type="multipart/form-data"形式的表单,却通过request.getParameter("**")获取参数值,则获取的参数值为空。
httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数的更多相关文章
- html5 file upload and form data by ajax
html5 file upload and form data by ajax 最近接了一个小活,在短时间内实现一个活动报名页面,其中遇到了文件上传. 我预期的效果是一次ajax post请求,然后在 ...
- Sending forms through JavaScript[form提交 form data]
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript As in the ...
- fetch发送Form Data请求并携带cookie
今天我们来说说如何fetch发送Form Data请求并携带cookie,直接进入正题好吧,别问我今天为啥不在开始吹两句了,累到一句牛逼不想吹...... 步骤1: 设置头部,"Conten ...
- VUE axios 发送 Form Data 格式数据请求
axios 默认是 Payload 格式数据请求,但有时候后端接收参数要求必须是 Form Data 格式的,所以我们就得进行转换.Payload 和 Form Data 的主要设置是根据请求头的 C ...
- springMVC中对HTTP请求form data和request payload两种数据发送块的后台接收方式
最近在做项目中发现,前台提交数据时,如果通过form表单提交和ajax发送json时,springMVC后台接收不能都通过@ModelAttribute方式处理,经过一番查找后,ajax发送json请 ...
- form data和request payload的区别
HTML <form> 标签的 enctype 属性 在下面的例子中,表单数据会在未编码的情况下进行发送: <form action="form_action.asp&qu ...
- Web 前沿——HTML5 Form Data 对象的使用
XMLHttpRequest Level 2 添加了一个新的接口——FormData.利用 FormData 对象,我们可以通过 JavaScript 用一些键值对来模拟一系列表单控件,我们还可以使用 ...
- HTTP请求中的form data和request payload的区别
HTML <form> 标签的 enctype 属性 在下面的例子中,表单数据会在未编码的情况下进行发送: <form action="form_action.asp&qu ...
- [整理]Ajax Post请求下的Form Data和Request Payload
Ajax Post请求下的Form Data和Request Payload 通常情况下,我们通过Post提交表单,以键值对的形式存储在请求体中.此时的reqeuest headers会有Conten ...
随机推荐
- ISE 中使用system generate
本文讲解简单的ISE中使用system generate,system generate基本使用规则在此不详细说明可以见博客http://blog.csdn.net/xiabodan/article/ ...
- CSS精粹之布局技巧
1.若有疑问立即检测 在出错时若能对原始代码做简单检测可以省去很多头痛问题.W3C对于XHTML与CSS都有检测工具可用,请见http://validator.w3.org 请注意,在文件开头的错误, ...
- bug汇总 (EF,Mvc,Wcf)
此博客用于在开发过程总bug及其解决方案的记录. 1. 异常信息: ObjectStateManager 中已存在具有同一键的对象.ObjectStateManager 无法跟踪具有相同键的多个对象 ...
- 一个统计目录文件大小的php函数
早上刚到公司,头告诉我,抓紧写一个小函数,用来统计指定目录中文件大小,我了个去,动手吧,还好有点小基础,一会就完工了,哈哈.代码在下面咯. <? /** 统计目录文件大小的函数 @author ...
- rsync参数详解、利用ssh、rsync 实现数据的定时同步
rsync 简介 rsync(remote synchronize)是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机之间的文 件.也可以使用 rsync 同步本
- python: 实现sha1小工具
File1: sha1.py File2: sha1.bat ------------------ File1: sha1.py import hashlib import os,sys def Ca ...
- XHTML1.0对HTML4.0的改进
1.XHTML借鉴了XML的写法,语法更加严格: 2.XHTML实现了把页面样式和内容分离了,废弃了HTML4.0中表示样式的标签和属性,推荐使用CSS样式来描述页面的样式. XHTML1.0 分为两 ...
- 一,XAML基础
RuntimeNameProperty特性:为什么<Grid x:Name="grid1"></Grid>等价于<Grid Name="gr ...
- PAT IO-04 混合类型数据格式化输入(5)
/* *PAT IO-04 混合类型数据格式化输入(5) *2015-08-01 作者:flx413 */ #include<stdio.h> int main() { int a; fl ...
- Windows Phone中的几种集合控件
前言 Windows Phone开发过程中不可避免的就是和集合数据打交道,如果之前做过WP App的开发的话,相信你已经看过了各种集合控件的使用.扩展和自定义.这些个内容在这篇博客里都没有,那么我们今 ...