This is the continue from this link: http://community.bonitasoft.com/questions-and-answers/trouble-calling-servlet-upload-file
I manage to find the require libraries.
My current code for calling Bonita's formUpload servlet is as below:
def uploadFile(String server, String path, String contentType, String fileName) {
// File upload using pageUpload servlet to a temporary folder
def uploadResponse
LOGGER.info("Create HTTPBuilder: " + server)
HTTPBuilder http = new HTTPBuilder(server)
http.request(POST, TEXT ) { multipartRequest ->
// URL of page upload servlet (REST API extension is handle as a page)
uri.path = "/API/formFileUpload"
// We use multipart content type to upload the file
requestContentType = "multipart/form-data"
LOGGER.info("requestContentType: " + requestContentType)
// Create a multipart entity builder for our local test file (the SQL data source REST API extension)
MultipartEntityBuilder multiPartContent = MultipartEntityBuilder.create().addBinaryBody("file", new File(path))
def entity = multiPartContent.build()
// This code should work, but return casting exception
multipartRequest.entity = entity
// Handle successful response
response.success = { resp, reader ->
LOGGER.info("Status code: " + resp.statusLine.statusCode)
if (resp.statusLine.statusCode == 200) {
// Store response information that include information about temporary storage of the file on server side
LOGGER.info("Response text: " + reader.text)
uploadResponse = reader.text
}
}
response.failure = { resp, reader ->
LOGGER.info("Upload unsuccessful")
}
}
if(uploadResponse == null) {
throw new Exception("Failed to upload the file")
} else {
return uploadResponse
}
}
The part "multipartRequest.entity = entity" keep throw casting error, but as I research, this code should work.
Exception:
Cannot cast object 'org.apache.http.entity.mime.MultipartFormEntity@12446679' with class 'org.apache.http.entity.mime.MultipartFormEntity' to class 'org.apache.http.HttpEntity'
Can you guy help me.