如何将多部分表单数据从jsp发送到Web服务?

问题描述:

我正在尝试为我的网站创建一个注册页面。
当用户将数据提交到signup.jsp时,我想使用application / x-www-form-urlencoded将此数据发送到我的Web服务。我怎么能在JSP中做到这一点。试过以下代码,但这会以原始数据的形式发送数据。

I'm trying to create a Signup page for my website. When the user submits the data to signup.jsp, I want to send this data to my web service using "application/x-www-form-urlencoded". How can I do this in JSP. Tried following code but this sends data in the form of raw data.

<%    URL url = new URL("http://www.externalsite.com/sample.html");
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);

    String postParams = "foo="+req.getParameter("foo");

    DataOutputStream paramsWriter = new DataOutputStream(con.getOutputStream());
    paramsWriter.writeBytes(postParams);
    paramsWriter.flush();
    paramsWriter.close();

    InputStream remoteResponse = conn.getInputStream();
    OutputStream localResponder = resp.getOutputStream();
    int c;
    while((c = remoteResponse.read()) != -1)
        localResponder.write(c);
    remoteResponse.close();
    localResponder.close();

    conn.disconnect(); %>


在形式的行动标签中只需提供网址的网址服务。内容类型由表单上的 enctype 属性决定

In action tag of form just give url of the web service . The content-type is determined by enctype attribute on form

如果您的表单包含文件输入元素,则形成操作标签应该是这样的

If your form contains file input element then form opeaning tag should be like this

<form method="POST" action="<your web service address>" enctype="multipart/form-data" >

如果它只包含文本和其他输入,文件除 enctype

If it contains only text and other inputs except file than enctype will be

application/x-www-form-urlencoded

表格更多信息参考

java ee文件上传示例