2

我正在使用 Spring 多部分文件上传文件,但在尝试使用 AJAX 捕获其响应时出现空指针异常。我能够成功上传文件而无需通过 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
});

弹簧控制器-

 @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 = new File("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)
4

1 回答 1

1

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

于 2015-07-01T10:34:14.360 回答