本文主要是介绍parseRequest(request)类型不匹配问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在写菜鸟教程上的Servlet文件上传的demo时,突然报错。
List<FileItem> formItems = upload.parseRequest((RequestContext) request);
必须把request强制转换,跟样例代码有些不一样,先测试。
上传一个.txt文件后,并没有出现上传成功的字样。
回到eclipse,报错信息。
java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.apache.tomcat.util.http.fileupload.RequestContextat com.runoob.test.UploadServlet.doPost(UploadServlet.java:95)at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:748)
重点是在第一行的类型不匹配。
反复对比样例代码,一模一样的代码为什么会报错。
记得在编写代码前的一个重要的导包工作,如果不先导包,那默认的导入的包是错误的。
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
正确的应该是:
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
所以应该提前导入
修改导入的包后,上传文件成功。
这篇关于parseRequest(request)类型不匹配问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!