httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数
2014-11-14 13:34
666 查看
一、利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单
二、MultipartRequest接收参数
若发送基于Content-Type="multipart/form-data"形式的表单,却通过request.getParameter("**")获取参数值,则获取的参数值为空。
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("**")获取参数值,则获取的参数值为空。
相关文章推荐
- 接收content-type:multipart/form-data类型的参数
- 实现使用HTTP协议发送multipart/form-data类型的HTTP表单
- httpClient 使用multipart/form-data 类型上传文件及表单
- 实现使用HTTP协议发送multipart/form-data类型的HTTP表单
- 利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单
- Java HttpClient 发送multipart/form-data带有Json文件的Post请求
- 实现使用HTTP协议发送multipart/form-data类型的HTTP表单
- 解决jsp或serverlet 不能解析multipart/form-data 类型的表单域的问题 --http://ckasj.vicp.net/blog/index.php/archives/236
- HttpClient+List<NameValuePair>发送和接收参数——基于struts2或者ssh等框架
- java httpclient multipart/form-data 方式post
- Http multipart/form-data多参数Post方式上传数据
- HTTP协议之post multipart/form-data数据类型实例
- Python3下requests库发送multipart/form-data类型请求
- android向服务器上传multipart/form-data文件(upload using multipart post using httpclient in android)
- 请问如何用nodejs通过post发送multipart/form-data类型的http请求?
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- post multipart/form-data 类型表单如何获取File外其他参数
- Http client to POST using multipart/form-data
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)