I have the next method:
public String uploadFile(String body, File uploadFile) throws Exception {
String xmlBody = startEnvelopeTag + body + endEnvelopeTag;
URL urlObj = new URL(urlWS);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestMethod("POST");
connection.setRequestProperty("SOAPAction", action);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
// Now I attach the xml body...
wr.writeBytes(xmlBody);
wr.flush();
// Now I Attach file...
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
wr.write(buffer, 0, bytesRead);
}
wr.flush();
wr.close();
int responseCode = connection.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
But I receive a code 500. If I comment the part of the attach file, the service send a code 200, but with an error of the non-existent file (into of xml response).
This is my body:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<sch:CreateItemRequest>
<sch:AuthenticationData>
<sch:ServerDef>
<sch:ServerType>ICM</sch:ServerType>
<sch:ServerName>icmnlsdb</sch:ServerName>
</sch:ServerDef>
<sch:LoginData>
<sch:UserID>xxxxxx</sch:UserID>
<sch:Password>xxxxxx</sch:Password>
</sch:LoginData>
</sch:AuthenticationData>
<sch:Item>
<sch:ItemXML>
<sch:X field1="x" field2="y" field3="z">
<sch:ICMBASE>
<sch:resourceObject xmlns="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema" MIMEType="application/pdf">
<sch:label name="test" />
</sch:resourceObject>
</sch:ICMBASE>
</sch:X>
</sch:ItemXML>
</sch:Item>
</sch:CreateItemRequest>
</soap:Body>
</soap:Envelope>
In SOAP-UI I can attach file, but I need make this in Java.
Any idea how I could attach the body and then the file?