作为标题,我想使用servlet在我的jsp项目中执行多个上传文件。我在新项目中对它进行了测试,并且没有遇到任何问题。然后我试图将它实现到我的项目中,该项目有代码:
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
} else {
try {
String itemName = item.getName();;
File savedFile = new File("D://uploadedFiles");
item.write(savedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
但我总是得到这样的错误:
HTTP Status 500 -
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1392: Error instantiating servlet class servlet.ManagementProdukServlet
root cause
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException
root cause
java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileUploadException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2 logs.
但是我已经在我的servlet中导入了这个:
import controller.Produk;
import dao.DataAksesAdmin;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
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;
有人能告诉我我的代码有什么问题导致我的上传表单在我的项目中没有用,但是它在新项目中有效吗? 有时候我没有收到错误代码,但我有错误:"连接重置"在我的浏览器上。这会影响我的项目吗?导致问题的原因是什么?
抱歉英语不好。答案 0 :(得分:2)
阅读此链接,非常好解释。使用apache fileupload 1.3,Servlet 3.0无法正常工作。 How to upload files to server using JSP/Servlet?
答案 1 :(得分:1)
即使您在源文件中导入,jar也应该在classpath中。如果从ide运行,请确保执行clean build,并且一旦没有错误部署并启动项目。如果要部署为战争,请确保您的战争包含所需的jar文件。