2
回答
求助,servlet3.0 part上传文件 报空指针
终于搞明白,存储TCO原来是这样算的>>>   
public class UploadPartServlet extends HttpServlet {

	private static final long serialVersionUID = 2593024650023174284L;

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String savePath = request.getServletContext().getRealPath("/images");
		Part part = request.getPart("file");

		String header = part.getHeader("content-disposition");
		String filename = header.substring(header.lastIndexOf("=") + 4,
				header.length() - 1);
		part.write(savePath + File.separator + filename);
		PrintWriter out = response.getWriter();
		out.println("上传成功");
		out.flush();
		out.close();

	}
String header = part.getHeader("content-disposition");这行报空指针
SEVERE: Servlet.service() for servlet [Upload] in context with path [/servlet3upload] threw exception
java.lang.NullPointerException
 at cn.guhai.servlet3.UploadPartServlet.service(UploadPartServlet.java:26)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
 at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
 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)
<无标签>
举报
guhai2004
发帖于5年前 2回/1K+阅

以下是问题补充:

  • @guhai2004 :这是表单: <form action="Upload" method="post" enctype="mutipart/form-data"> <input type="file" name="file"/> <input type="submit" /> </form> (5年前)
顶部