Java getPart()抛出空指针异常

时间:2014-11-21 22:46:08

标签: java jsp servlets nullpointerexception multipartform-data

我在尝试使用getPart()时遇到空指针异常,我无法理解为什么这会抛出异常而不是返回null。这是我在jsp中的表单:

<form onsubmit="return check()" method="post" enctype="multipart/form-data" action="/TrainingServlet">
   <input id="hotel_file" type="file" name="hotel_file" size="30">
</form>

然后在我的servlet中,我有以下代码:

public void doPost(HttpServletRequest req, HttpServletResponse resp) {
   try {
           Part filePart = req.getPart("hotel_file");  //this line throws null pointer exception
           getServletContext().getRequestDispatcher("/import.jsp").forward(req, resp);

   } catch(Exception e) {

           e.printStackTrace();
   }
}

在阅读了这个SO post后,我将@MultipartConfig注释添加到servlet类中,我仍然在req.getPart()行获得NPE。此外,我正在JBoss 6上运行servlet。任何见解都赞赏!

这是stacktrace

16:50:15,227 ERROR [STDERR] java.lang.NullPointerException
16:50:15,228 ERROR [STDERR] at org.apache.catalina.connector.Request.getPart(Request.java:3225)
16:50:15,228 ERROR [STDERR] at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1102)
16:50:15,229 ERROR [STDERR] at com.lanyon.training.javaproj.servlets.TrainingServlet.doPost(TrainingServlet.java:96)
16:50:15,229 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
16:50:15,230 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
16:50:15,230 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)
16:50:15,231 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
16:50:15,231 ERROR [STDERR] at com.lanyon.ghotel.common.pagination.web.PaginationFilter.doFilter(PaginationFilter.java:77)
16:50:15,232 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
16:50:15,232 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
16:50:15,233 ERROR [STDERR] at com.lanyon.common.serverobjects.UTF8EncodingFilter.doFilter(UTF8EncodingFilter.java:33)
16:50:15,233 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
16:50:15,234 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
16:50:15,235 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
16:50:15,235 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
16:50:15,236 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)
16:50:15,236 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
16:50:15,236 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
16:50:15,237 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
16:50:15,237 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
16:50:15,238 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
16:50:15,238 ERROR [STDERR] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
16:50:15,239 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
16:50:15,239 ERROR [STDERR] at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
16:50:15,240 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
16:50:15,240 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
16:50:15,241 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
16:50:15,241 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
16:50:15,242 ERROR [STDERR] at java.lang.Thread.run(Thread.java:619)

1 个答案:

答案 0 :(得分:0)

multipart为您提供字节流。 你必须把它转换成一个文件。

你也可以使用multipart的transferTo()方法