【问题标题】:Eclipse JSP "cannot be resolved to a type" Apache commonsEclipse JSP“无法解析为类型”Apache commons
【发布时间】:2016-05-12 18:15:05
【问题描述】:

我试图从JSP File Uploading 复制教程。我正在使用 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 包,并将 jars 作为外部添加到构建路径中。该代码是教程的直接副本,包含导入和所有内容,这是链接中的完整代码。

    <%@ 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>");
   }
%>

【问题讨论】:

    标签: java eclipse jsp apache-commons


    【解决方案1】:

    您应该将 jar 复制到 WEB-INF/lib。这是在您运行已部署的 Web 应用程序时保证库位于类路径上的唯一方法。

    不要在 JSP 文件中写这个文件上传业务逻辑。而不是使用 servlet 进行文件上传。您可以找到此类 servlet 的示例here

    Eclipse 项目结构:

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 2015-02-19
      • 2012-07-17
      • 2013-07-17
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2011-10-08
      • 2019-08-06
      相关资源
      最近更新 更多