关于Java代码的parseRequest()方法的参数传递问题
FileItemFactoryfactory=newDiskFileItemFactory();ServletFileUploadupload=newServletFil...
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items =upload.parseRequest(request);
这是我用java ee用dopost方式请求文件上传的几行代码,可是java提示parseRequest()传递的参数是RequestContext,但是我看到网上传递的都只是Request。。。于是乎我进行了强转cast,代码变为List<FileItem> items =upload.parseRequest((RequestContext) request);代码没有没报,可是运行却报错了,提示如下:org.apache.catalina.connector.RequestFacade cannot be cast to org.apache.tomcat.util.http.fileupload.RequestContext;
我用的是64位系统,是不是有些代码写起来需要不一样,我是新手,求大神指点下,谢谢! 展开
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items =upload.parseRequest(request);
这是我用java ee用dopost方式请求文件上传的几行代码,可是java提示parseRequest()传递的参数是RequestContext,但是我看到网上传递的都只是Request。。。于是乎我进行了强转cast,代码变为List<FileItem> items =upload.parseRequest((RequestContext) request);代码没有没报,可是运行却报错了,提示如下:org.apache.catalina.connector.RequestFacade cannot be cast to org.apache.tomcat.util.http.fileupload.RequestContext;
我用的是64位系统,是不是有些代码写起来需要不一样,我是新手,求大神指点下,谢谢! 展开
2个回答
引用jingyukxy的回答:
/**源码中是这样定义的,所以你在doPost方法里面直接传入request就好了,这个不需要强转*/ public List /* FileItem */ parseRequest(HttpServletRequest request) throws FileUploadException { return parseRequest(new ServletRequestContext(request)); }
/**源码中是这样定义的,所以你在doPost方法里面直接传入request就好了,这个不需要强转*/ public List /* FileItem */ parseRequest(HttpServletRequest request) throws FileUploadException { return parseRequest(new ServletRequestContext(request)); }
展开全部
调用的jar错误,该方法应该调用
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
即引入的jar包为commons-fileupload
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
即引入的jar包为commons-fileupload
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询