文件上传代码出错

时间:2014-12-14 03:55:49

标签: java file jsp tomcat file-upload

我在jsp页面中收到以下文件上传代码错误:

  

FileUploadBase类型中的方法parseRequest(RequestContext)不适用于参数(HttpServletRequest)

代码中的

错误:

List<FileItem> items = uploadHandler.parseRequest(request);

3 个答案:

答案 0 :(得分:6)

parseRequest(RequestContext ctx)期望RequestContext实例作为参数,但传递的参数是HttpServletRequest的实例

使用ServletRequestContext创建RequestContext实例,如下所示。

List<FileItem> items = uploadHandler.parseRequest(new ServletRequestContext(request));

答案 1 :(得分:0)

我遇到了同样的问题,然后发现我的导入错误了:最后一个是使用fileupload中的sun,而不是commons.fileupload。在我将其全部更改为commons.fileupload 后,错误消失了:

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

答案 2 :(得分:0)

以上的最佳答案,但是如果您要升级到名称从javax更改为jakarta的tomcat10,则1.4版以上的commons-fileupload仍未更改名称,但是您可以改为tomcat10中的自定义类! (幸运的我)

org.apache.commons.fileupload.ProgressListener          to org.apache.tomcat.util.http.fileupload.ProgressListener
org.apache.commons.fileupload.servlet.ServletFileUpload to org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload
org.apache.commons.fileupload.disk.DiskFileItemFactory  to org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory

org.apache.commons.fileupload.FileItem                  to org.apache.tomcat.util.http.fileupload.FileItem