我正在尝试通过ajax发送图片:
function putImage() {
var image = document.getElementById('image').files[0];
var formData = new FormData();
formData.append('image', image);
$.ajax({
url: 'http://localhost:8080/ImageStorageREST/image',
type: 'put',
data: formData,
contentType: false,
processData: false,
async: true,
success: function(data) {
console.log("success");
console.log(data);
},
error: function(data) {
console.log("error");
console.log(data);
}
});
}
HTML表单:
<form>
<input type="file" multiple accept="image/*,image/jpeg" id="image"/>
<input type="submit" value="Отправить" onClick="putImage(); return false;" />
</form>
控制器方法:
@RequestMapping(value="/image", method=RequestMethod.PUT)
public @ResponseBody String addImage(@RequestPart("image") MultipartFile image) {
return "RECEIVED";
}
多部分解析器在调度程序配置文件中注册:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5000000" />
</bean>
我在服务器上收到了 org.springframework.beans.BeanInstantiationException
错误。
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartFile]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:879)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
请求状态为
Request URL:http://localhost:8080/ImageStorageREST/image
Request Method:PUT
Status Code:500 Internal Server Error
Remote Address:[::1]:8080
但是我在浏览器上看到的参数是:
Content-Disposition: form-data; name="image"; filename="format_jpg.jpg"
Content-Type: image/jpeg
......
那么为什么会抛出这个异常?我查看了很多链接,解决方案是添加 multipartResolver
bean,但我已经有了。
enctype="multipart/form-data"
了吗? - Viltér PanitzmultipartResolver
,另一个类型设置为org.springframework.web.multipart.MultipartFile
,这是一个接口。请检查您的XML文件。 - Bnrdo