如何将多部分表单数据从jsp发送到Web服务? [英] how to send multipart form data from jsp to web service?

查看:156
本文介绍了如何将多部分表单数据从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文件上传示例

这篇关于如何将多部分表单数据从jsp发送到Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆