我使用下面的代码将文件上传到tomcat5.5,它给了我以下异常
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
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.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class FileUploadServlet
*/
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FileUploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("rawtypes")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("Status : "+isMultipart);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
response.sendRedirect("upload.jsp");
}
}
这是我用commons-fileupload-1.2.2.jar
答案 0 :(得分:37)
特定的异常消息告诉您类路径中缺少所提到的类。由于org.apache.commons.io
包名称提示,所提到的类是http://commons.apache.org/io
项目的一部分。
事实上,Commons FileUpload将Commons IO作为dependency。您需要download并在commons-io.jar
中删除/WEB-INF/lib
。
答案 1 :(得分:14)
使用maven依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
或将commons-io.1.3.2.jar下载到您的lib文件夹
答案 2 :(得分:6)
解决方案
默认情况下,Struts使用Apache“commons-io.jar”进行文件上传过程。要修复它,您必须将此库包含在项目依赖库文件夹中。
从官方网站获取“commons-io.jar” - http://commons.apache.org/io/
首选方法是从Maven存储库获取“commons-io.jar”
文件:pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
答案 3 :(得分:2)
将所有apache comons jar和文件上传jar放在tomcat的lib文件夹中
答案 4 :(得分:1)
如果您在WebSphere容器中收到此错误,请确保正确设置Apps类加载策略。我不得不将我的默认值更改为'parent last'以及WAR策略的'Single class loader for application'。这是因为在我的情况下,commons-io * .jar与应用程序一起打包,所以必须先加载它。
答案 5 :(得分:0)
您将必须从此处https://commons.apache.org/proper/commons-io/download_io.cgi下载文件,然后选择https://prnt.sc/tk5ewt
现在,下一步,将此下载的文件添加到您的项目中:
右键单击您的项目->构建路径->配置BuidPath-> https://prnt.sc/tk5d93
答案 6 :(得分:-4)
下载PrimeFaces 4.0。它马上解决了我的问题。我正在使用Glassfish 4.0