简体   繁体   中英

how to send multipart form data from jsp to web service?

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(); %>

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" >

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

application/x-www-form-urlencoded

form more info refer

java ee file upload example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM