今天我用commons-fileupload组件上传文件,代码是正确的,而且我把commons-fileupload-1.2.1.jar,commons-io-1.4.jar也导入到项目目项中,但总是出现这样那样的错误,让我花了不少的时间,所以我把相关发出来,对有此类的问题的朋友有所帮助
如:
java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
……………………
在网上查了很多资料,后来总结了一下,如果你是在struts2项目中用commons-fileupload组件的话,那就应

该把修改web.xml,把原来的
<filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
改为
<filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>
而上传页面调用时直接用文件名调用(如upload.jsp)。struts2只处理 *.action 的请求。
或者
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
去掉也行

若是java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException错误
那就有可能是导包的问题,若用的是右击项目,选择属性,然后导入相应的包,就有可能出显此类的错误,解决方法是:把commons-fileupload-1.2.1.jar,commons-io-1.4.jar复制到到WEB-INF下的lib目录中,虽然效果是一样,但不会出错了

附:
upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'upload.jsp' starting page</title>
</head>  
  <body>
  <form action="upload" method="post" enctype="multipart/form-data">
   用户名:<input type="text" name="username"><br>
   密码:<input type="password" name="password"><br>
   文件1:<input type="file" name="file1"><br>
    文件2:<input type="file" name="file2"><br> 
   <input type="submit" value="提交"><br>
  </form>
  </body>
</html>
UploadServlet.java

package com.test.servlet1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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;

public class UploadServlet extends HttpServlet {

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  DiskFileItemFactory factory = new DiskFileItemFactory();
  String path = request.getRealPath("/upload");
  // 设置传轮输文件的大小,当小于1024时,把文件存在兹盘上
  factory.setRepository(new File(path));
  factory.setSizeThreshold(1024 * 1024);
  ServletFileUpload upload = new ServletFileUpload(factory);
  try {
   List<FileItem> list = upload.parseRequest(request);
   for(FileItem item : list){
    if(item.isFormField()){
     String name = item.getFieldName();
     String value  = item.getString("GB2312");
     request.setAttribute(name, value);
    }else{
     String name = item.getFieldName();
     String value = item.getName();
     int start = value.lastIndexOf("\\");
     String fileName = value.substring(start+1);
     request.setAttribute(name, fileName);
     //item.write(new File(path,fileName));    
     OutputStream os = new FileOutputStream(new File(path,fileName));
     InputStream is = item.getInputStream();
     // 将文件is写到os里
     byte[] buffer = new byte[400];
     int lenght = 0 ;
     while((lenght = is.read(buffer ))>0){
      os.write(buffer,0,lenght);
     }
     os.close();
     is.close();
    }
   }
  } catch (FileUploadException e) {
   // TODO Auto-generated catch block
   System.out.println("文件上传出错"+e.getMessage());
   e.printStackTrace();
  }
  request.getRequestDispatcher("upload/result.jsp").forward(request, response);
 }
}
result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>My JSP 'result.jsp' starting page</title>
  </head> 
  <body>
 用户名:${requestScope.username }
 密码:${requestScope.password }
 文件1:${requestScope.file1 }
 文件2:${requestScope.file2 }
  </body>
</html>