Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:14:36+00:00 2026-06-03T14:14:36+00:00

I have a xml file content as string in my servlet, I need to

  • 0

I have a xml file content as string in my servlet, I need to call another URL with multipart post request to upload it as xml file.

Is there a way it can be done?

So far this is what i am doing

private def createConfiguration(def sessiontoken)
{
    /*reqParams is request.getParameterMap(), fileParams is again a map*/
    def charset = "UTF-8";
    def query = String.format("emailaddress=%s&projectid=%s&cfgname=%s&cfgdesc=%s&cfgfile=%s",
        URLEncoder.encode(sessiontoken, charset),
        URLEncoder.encode(reqParams.c_Cfgname[0], charset),
        URLEncoder.encode(reqParams.c_Cfgdesc[0], charset),
        URLEncoder.encode(reqParams.c_Cfgtype[0], charset),
        URLEncoder.encode(reqParams.CFGFILE[0], charset),)

    URLConnection connection = new URL(fileParams.login).openConnection()
    connection.setDoOutput(true)
    connection.setRequestProperty("Accept-Charset", charset)
    connection.setRequestProperty("Content-Type", "multipart/form-data;charset=" + charset)
    try {
        connection.getOutputStream().write(query.getBytes(charset))
    }
    finally {
        connection.getOutputStream().close()
    }
    InputStream response = connection.getInputStream()
    def xmlString=response.getText()
    xmlString
}

Below is the exception fetched

SEVERE: Servlet.service() for servlet RedirectRequest threw exception
java.io.IOException: Server returned HTTP response code: 800 for URL: http://abhishek157:10070/project/create.action
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$getInputStream.call(Unknown Source)
.
.

Update

Got this very usefull link by BalusC So I used it.

private def getStreamFromString(str)
{
    // convert String into InputStream
    InputStream is = new ByteArrayInputStream(str.getBytes())
    is
}

private def createConfiguration(def sessiontoken)
{


    println "ok good $sessiontoken"
    def charset = "UTF-8"
    def boundary = Long.toHexString(System.currentTimeMillis())
    def CRLF = "\r\n"
    String param = "value"

    URLConnection connection = new URL(fileParams.create).openConnection()
    println fileParams.create
    connection.setDoOutput(true)
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    PrintWriter writer = null
    try {

        OutputStream output = connection.getOutputStream()
        writer = new PrintWriter(new OutputStreamWriter(output, charset), true)

        // Sending normal param.
        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"sessiontoken\"$CRLF$CRLF$sessiontoken").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgname\"$CRLF$CRLF${reqParams.c_Cfgname[0]}").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgdesc\"$CRLF$CRLF${reqParams.c_Cfgdesc[0]}").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgenv\"$CRLF$CRLF${reqParams.c_Cfgtype[0]}").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        // Sending xml file.
        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgfile\"; filename=\"" + reqParams.FILENAME[0] + "\"").append(CRLF)
        writer.append("Content-Type: text/xml; charset=" + charset).append(CRLF)
        writer.append(CRLF).flush()
        BufferedReader reader = null
        try {
            reader = new BufferedReader(new InputStreamReader(getStreamFromString(reqParams.CFGFILE[0]), charset))
            for (String line; (line = reader.readLine()) != null;) {
                writer.append(line).append(CRLF)
            }
        }
        catch(Exception e)  {
            e.printStackTrace()
        }
        finally {
            if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
        }
        writer.flush();
        writer.append("--" + boundary + "--").append(CRLF)
    } 
    finally {
        if (writer != null) writer.close();
    }
    InputStream response = connection.getInputStream()
    def xmlString=response.getText()
    xmlString
}

and on the console I get

http://abhishek157:10070/project/create.action
done

but it is not at all hitting http://abhishek157:10070/project/create.action
Any help?

More Updates

The real request (working from html form, where I select file from web browser)

-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="sessiontoken"

4611685684744086913
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgname"

sadf
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgdesc"

sadf
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgenv"

Production
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgfile"; filename="C:\Simon\xmls\agentind.xml"
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?> and so on...

Updated params part after matching from the actual request in fiddler. See createConfiguration function

Exception fetched (while calling create.action from servlet)

Note: I checked in the servlet before sending the params in create.action, all are valid

java.lang.NumberFormatException: null 

None of the params are read in the server, all are coming as null. Where is the problem. Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:14:37+00:00Added an answer on June 3, 2026 at 2:14 pm

    In your updated code, you forgot to call connection.getInputStream(); to actually send the HTTP request (and to retrieve the HTTP response).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following contents in my XML file: <items> <item id=1><content><![CDATA[<p>string</p>]]></content></item> </items> I
I have a xml file whose content is <?xml version=1.0 encoding=UTF-8?> <ReturnMessage> <root>ReturnMessage</root> <cancelMessage>Request
I have the following TextView in my XML layout file:- <TextView android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/autolink_test
I have a long xml file the content of the file are below: <?xml
I have an XML file as follows, and I'm trying to read the content
I have a xml file main.xml with following markup and data. main.xml <xml> <content>
I have written simple code to get content from xml file to php. $xml
I have a Silverlight application that reads its content from an XML file. The
i'm using Spring-WS and have the following spring-ws-servlet.xml file. The injection of defaultURI and
I have to write an XML file using java.The contents should be written from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.