简体   繁体   中英

Request with image by ajax to spring mvc controller

I'm trying to send image by 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:

<form>
<input type="file" multiple accept="image/*,image/jpeg" id="image"/>
<input type="submit" value="Отправить" onClick="putImage(); return false;" />
</form>

The controller's method:

@RequestMapping(value="/image", method=RequestMethod.PUT) 
public @ResponseBody String addImage(@RequestPart("image") MultipartFile image) {
    return "RECEIVED";
}

Multipart Resolver is registered in dispatcher config file:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="5000000" />
</bean>

I received org.springframework.beans.BeanInstantiationException at server

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)

And request status is

Request URL:http://localhost:8080/ImageStorageREST/image
Request Method:PUT
Status Code:500 Internal Server Error
Remote Address:[::1]:8080

But I see the parameter at browser:

Content-Disposition: form-data; name="image"; filename="format_jpg.jpg"
Content-Type: image/jpeg

......

So why is this exception thrown? I looked a lot of links where the solution was to add multipartResolver bean, but I have it yet.

This problem was caused by using Servlet 2.5 (Tomcat 6.0). The HttpServletRequest class doesn't contain getParts() method. So I solved my problem by changing controller's method:

@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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM