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("**")獲取參數值,則獲取的參數值為空。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM