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.