我尝试从这里复制教程JSP文件上传。我正在使用 Eclipse 和 Tomcat,但是当我在服务器上运行它时,出现错误:
An error occurred at line: 24 in the jsp file: /index.jsp
DiskFileItemFactory cannot be resolved to a type
21: String contentType = request.getContentType();
22: if ((contentType.indexOf("multipart/form-data") >= 0)) {
23:
24: DiskFileItemFactory factory = new DiskFileItemFactory();
25: // maximum size that will be stored in memory
26: factory.setSizeThreshold(maxMemSize);
27: // Location to save data that is larger than maxMemSize.
我已经下载了 commons fileupload 和 io 包,并将这些 jar 添加为构建路径中的外部。该代码是教程的直接副本,包含导入和所有内容,以下是链接中的完整代码:
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%
File file ;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");
// Verify the content type
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("c:\\temp"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>JSP File upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + filePath +
fileName + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}else{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
}
%>
您应该将 jar 复制到
WEB-INF/lib
。这是保证运行部署的 Web 应用程序时库位于类路径上的唯一方法。
不要将此文件上传业务逻辑写入JSP 文件中。而不是使用servlet来进行文件上传。您可以在here找到此类 servlet 的示例。
Eclipse 项目结构: