ajax 向 spring mvc 控制器请求图像

Request with image by ajax to spring mvc controller

我正在尝试通过 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";
}

Multipart Resolver 已在调度程序配置文件中注册:

<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,但我已经找到了。

此问题是使用 Servlet 2.5 (Tomcat 6.0) 引起的。 HttpServletRequest class 不包含 getParts() 方法。 所以我通过改变控制器的方法解决了我的问题:

@RequestMapping(value="/image", method=RequestMethod.PUT, consumes="multipart/form-data") 
public @ResponseBody void addImage(HttpServletRequest request) throws ImageException {
    byte[] bytes = getBytesFromFile(request);
    Image image = new Image();
    image.setByteData(bytes);
    imageService.addImage(image);
}

private byte[] getBytesFromFile(HttpServletRequest request) throws ImageException {
    ServletFileUpload upload = new ServletFileUpload();
    byte[] bytes = null;
    FileItemIterator iter;
    try {
        iter = upload.getItemIterator(request);
        while(iter.hasNext()) {
            FileItemStream item = iter.next();
            InputStream stream = item.openStream();
            bytes = IOUtils.toByteArray(stream);
        }
        return bytes;
    } catch (IOException | FileUploadException e) {
        throw new ImageException("The problem while storing file. Try again.",e);
    }
}