问题
Here is my ajax call:
uploadExcel : function(jsonData,success, error) {
var url = "/TestProject/test/Uploader;
$.ajaxFileUpload({
url : url,
secureuri : false,
fileElementId : 'FileUpload',
contentType : 'multipart/form-data',
dataType : 'jsonString',
processData : false,
type : 'POST',
data: jsonData,
success : success,
error : error
});
}
Java Method signature:
@Path("/Uploader")
@POST
@Consumes('multipart/form-data')
public String validateAndUpload(@FormDataParam("FileUpload") byte[] inputByteArray,
@Context HttpServletRequest request,
@FormParam("jsonData") String uploadData) {}
Here is the error I'm getting
Here is the stackTrace:
SEVERE: Servlet.service() for servlet [ServletAdaptor] in context with path [/TestProject] threw exception [com.sun.jersey.api.container.ContainerException: Exception obtaining parameters] with root cause
java.lang.NullPointerException
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:43)
at com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider$FormDataInjectableValuesProvider.getInjectableValues(FormDataMultiPartDispatchProvider.java:115)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:126)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:154)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
回答1:
Try:
@FormDataParam("FileUpload") InputStream fileInputStream
Instead of:
@FormDataParam("FileUpload") byte[] inputByteArray
According to FormDataParam API, the following is supported:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA_TYPE)
public String postForm(
@DefaultValue("true") @FormDataParam("enabled") boolean enabled,
@FormDataParam("data") FileData bean,
@FormDataParam("file") InputStream file,
@FormDataParam("file") FormDataContentDisposition fileDisposition) {
...
}
From the javadoc:
Where the server consumes a
multipart/form-datarequest entity body that contains one optional named body part "enabled" and two required named body partsdataandfile.The optional part
enabledis processed as abooleanvalue, if the part is absent then the value will be true.The part
datais processed as a JAXB bean and contains some meta-data about the following part.The part
fileis a file that is uploaded, this is processed as anInputStream. Additional information about the file from theContent-Dispositionheader can be accessed by the parameterfileDisposition.
来源:https://stackoverflow.com/questions/33237958/rest-jersey-exception-obtaining-parameters