在spring中上传文件并使用ajax捕获响应时出错

时间:2013-11-01 09:45:17

标签: jquery ajax spring spring-mvc multifile-uploader

我正在使用Spring多部分文件上传文件,但在尝试使用AJAX捕获其响应时获取Null Pointer Exception。我能够成功上传文件而不通过AJAX捕获响应,即当我删除表单或脚本的id属性时由于某些错误而未执行。

JSP表单

<form action="actionXMLUpload" method="POST"
    enctype="multipart/form-data" id="dealform">
    Select XML file to upload <input type="file" name="file"
        accept="text/xml" /> <input type="submit" value="Upload XML" />
</form>

AJAX代码 -

$('#dealform').submit(function() { // catch the form's submit event
    $.ajax({ // create an AJAX call...
        data : $(this).serialize(), // get the form data
        type : $(this).attr('method'), // GET or POST
        url : $(this).attr('action'), // the file to call
        success : function(response) { // on success..
            $('#pages').html(response); // update the DIV       
        }
    });
    return false; // cancel original event to prevent form submitting
});

Spring Controller -

 @RequestMapping(value = "/actionXMLUpload", method = RequestMethod.POST)
public String uploadXML(FileUploadUtil xmlUtil, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "xmlupload";
    }
    try {           
        File destFile = new File("D:\\Travel_Portal\\"
                + xmlUtil.getFile().getOriginalFilename());           
        xmlUtil.getFile().transferTo(destFile);          
        dealXMLParser.xmlParser();
        logger.info("********XML FILE SUCCESSFULLY UPLOADED*********");
        model.addAttribute(MESSAGE, "XML File Successfully Uploaded!!!");
        return "xmlupload";
    } catch (Exception e) {
        logger.error("********XML FILE NOT SUCCESSFULLY UPLOADED*********",
                e);
        model.addAttribute(MESSAGE, "Error Uploading XML File!!! Please Try Again!!!");
        return "xmlupload";
    }
}

文件destFile =新文件的异常(“D:\ Travel_Portal \”                     + xmlUtil.getFile()。getOriginalFilename());

java.lang.NullPointerException
at com.travelportal.controller.UploadController.uploadXML(UploadController.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:1)

使用@RequestBody解决了我的问题。这是因为文件上传需要一些时间,我需要上传的文件内容。