I am getting the following error when I try to call a webservice at /upload
to upload a multipart-form data using RESTEasy.
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found.
/upload
webservice:
@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response uploadFile(MultipartFormDataInput input) {
String fileName = "";
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<InputPart> inputParts = uploadForm.get("uploadedFile");
for (InputPart inputPart : inputParts) {
try {
MultivaluedMap<String, String> header = inputPart.getHeaders();
fileName = getFileName(header);
//convert the uploaded file to inputstream
InputStream inputStream = inputPart.getBody(InputStream.class,null);
byte [] bytes = IOUtils.toByteArray(inputStream);
//constructs upload file path
fileName = UPLOADED_FILE_PATH + fileName;
writeFile(bytes,fileName);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
return Response.status(200)
.entity("uploadFile is called, Uploaded file name : " + fileName).build();
}
/**
* header sample
* {
* Content-Type=[image/png],
* Content-Disposition=[form-data; name="file"; filename="filename.extension"]
* }
**/
//get uploaded filename, is there a easy way in RESTEasy?
private String getFileName(MultivaluedMap<String, String> header) {
String[] contentDisposition = header.getFirst("Content-Disposition").split(";");
for (String filename : contentDisposition) {
if ((filename.trim().startsWith("filename"))) {
String[] name = filename.split("=");
String finalFileName = name[1].trim().replaceAll("\"", "");
return finalFileName;
}
}
return "unknown";
}
}
When I try to debug it with debug points inside the web service, the debug control is not going inside the webservice.
I used Postman with the correct headers to send the request and got the following error as response:
java.lang.RuntimeException: RESTEASY007500: Could find no Content-Dispostion header within part
To make sure I am looking at the right method in the web service's code, I replaced the entire method with a return statement that returned a 500 Internal Server Error. This meant I should receive a 500 Internal Server error upon calling the /upload endpoint; however, I still received the same error as before.
It appears some other logic is run before the control ever reaches the /upload method, and I can't for the life of me figure out where that's coming from. It might be a decorator of some sort, but I'm not too familiar with Java, so any pointers would be helpful.
Sidenote: As I was unsure what was causing this error, I even looked at the source code for RESTEasy on GitHub. I figured it has something to do with Content-Disposition
header not being provided correctly, but I am providing it correctly to the best of my knowledge.
I also tried to provide Content-Disposition header within the multipart form body, but that did not help.
How do I resolve this error?