问题描述
- 为什么我访问路径报出空指针异常(javaweb)
-
这是异常提示:java.lang.NullPointerException at java.io.FileOutputStream.<init>(FileOutputStream.java:206) at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
**at com.mifan.web.UpLoadServlet.doGet(UpLoadServlet.java:41)**
at com.mifan.web.UpLoadServlet.doPost(UpLoadServlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:285) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2439) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2428) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:722)
这是源代码:
public class UpLoadServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ //创建工厂类 这是阿帕奇提供的那个第三包jar包 DiskFileItemFactory factory = new DiskFileItemFactory(); //生产文件上传核心类 ServletFileUpload fileUpload = new ServletFileUpload(factory); List<FileItem> list = fileUpload.parseRequest(request); //遍历所有的FileItem for(FileItem item : list){ if(item.isFormField()){ //当前是一个普通的字段项 String name = item.getFieldName();//普通字段的名字 String value = item.getString(); //普通字段名字所对应的值 System.out.println(name+":"+value); }else {//当前是一个文件上传项 //获取文件上传项的文件名 String filename = item.getName(); InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream( this.getServletContext().getRealPath("upload/"+filename)); IOUtils.InAndOut(in, out); IOUtils.close(in, out); } } //利用文件上传核心类解析request }catch (Exception e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
}
解决方案
Debug 跟一下你就知道哪个为空了。
时间: 2025-01-21 13:34:57