JSP - 使用 Apache Commons 上传文件

发布于 2024-07-29 04:19:38 字数 4821 浏览 8 评论 0原文

为什么我的文件上传代码不起作用?

我正在使用“commons-fileupload-1.1.1.jar”。

另外,我在 NetBeans 6.1 中的“isMultipartContent”第二行中看到了删除线。

    // Check that we have a file upload request  
>>>>    boolean isMultipart = FileUpload.isMultipartContent(request);  

// Create variables for path, filename and extension  
appPath = application.getRealPath("\\");

// Create a factory for disk-based file items  
FileItemFactory factory = new DiskFileItemFactory();  

// Create a new file upload handler  
ServletFileUpload upload = new ServletFileUpload(factory);  

// Parse the request and Extract request items  
>>>>>    List items = upload.parseRequest(request);

// create an Iterator to iterate through request items  
Iterator iter = items.iterator();

//Form fields
//out.println("<br><br>While loop started");

while (iter.hasNext())
{  
  FileItem item = (FileItem) iter.next();

  if(item.isFormField())
  {
    String name = item.getFieldName();  
    String value = item.getString();  

    if(name.equals("txtUsername")) 
    {
      _USERNAME_ = value;

      Class.forName("org.gjt.mm.mysql.Driver");

      try
      {
        connection = DriverManager.getConnection("jdbc:mysql://localhost/ict");
        statement = connection.createStatement();
        recordset = statement.executeQuery("SELECT * FROM registered_students WHERE username=\'" + _USERNAME_ + "\'");

        while(recordset.next())
        {
          roll = recordset.getString(4);

          _ROLL_ = roll;
        }

        recordset.close();
        recordset=null;

        statement.close();
        statement=null;
      }
      finally
      {
        if(connection!=null)
        {
          connection.close();
        }
      }
    }
  }
}

我收到这样的消息:

org.apache.jasper.JasperException: An exception occurred processing JSP page /Process_FileUpload.jsp at line 75

73: 
74:         // Parse the request and Extract request items  
75:         List items = upload.parseRequest(request);  
76:         
77: 
78:         // create an Iterator to iterate through request items  
79:         Iterator iter = items.iterator();

Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.Process_005fFileUpload_jsp._jspService(Process_005fFileUpload_jsp.java:546)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
    org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:179)
    org.apache.commons.fileupload.FileUploadBase.createItem(FileUploadBase.java:500)
    org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
    org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
    org.apache.jsp.Process_005fFileUpload_jsp._jspService(Process_005fFileUpload_jsp.java:138)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

Why my file-upload code is not working?

I am using "commons-fileupload-1.1.1.jar".

Also I am seeing a strikethrough in the 2nd Line on "isMultipartContent" in NetBeans 6.1.

    // Check that we have a file upload request  
>>>>    boolean isMultipart = FileUpload.isMultipartContent(request);  

// Create variables for path, filename and extension  
appPath = application.getRealPath("\\");

// Create a factory for disk-based file items  
FileItemFactory factory = new DiskFileItemFactory();  

// Create a new file upload handler  
ServletFileUpload upload = new ServletFileUpload(factory);  

// Parse the request and Extract request items  
>>>>>    List items = upload.parseRequest(request);

// create an Iterator to iterate through request items  
Iterator iter = items.iterator();

//Form fields
//out.println("<br><br>While loop started");

while (iter.hasNext())
{  
  FileItem item = (FileItem) iter.next();

  if(item.isFormField())
  {
    String name = item.getFieldName();  
    String value = item.getString();  

    if(name.equals("txtUsername")) 
    {
      _USERNAME_ = value;

      Class.forName("org.gjt.mm.mysql.Driver");

      try
      {
        connection = DriverManager.getConnection("jdbc:mysql://localhost/ict");
        statement = connection.createStatement();
        recordset = statement.executeQuery("SELECT * FROM registered_students WHERE username=\'" + _USERNAME_ + "\'");

        while(recordset.next())
        {
          roll = recordset.getString(4);

          _ROLL_ = roll;
        }

        recordset.close();
        recordset=null;

        statement.close();
        statement=null;
      }
      finally
      {
        if(connection!=null)
        {
          connection.close();
        }
      }
    }
  }
}

And I am getting this message:

org.apache.jasper.JasperException: An exception occurred processing JSP page /Process_FileUpload.jsp at line 75

73: 
74:         // Parse the request and Extract request items  
75:         List items = upload.parseRequest(request);  
76:         
77: 
78:         // create an Iterator to iterate through request items  
79:         Iterator iter = items.iterator();

Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.Process_005fFileUpload_jsp._jspService(Process_005fFileUpload_jsp.java:546)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
    org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:179)
    org.apache.commons.fileupload.FileUploadBase.createItem(FileUploadBase.java:500)
    org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
    org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
    org.apache.jsp.Process_005fFileUpload_jsp._jspService(Process_005fFileUpload_jsp.java:138)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-08-05 04:19:38

因为 commons-fileupload 依赖于 commons-io。 将其添加到您的类路径中。

Because commons-fileupload has a dependency on commons-io. Add that to your classpath.

£烟消云散 2024-08-05 04:19:38

请注意 - 你的数据库查询让我有点畏缩。 如果 Statement 和 ResultSet 对象未关闭,则似乎容易发生内存泄漏,并且由于将输入直接复制到查询中,因此可能会受到注入攻击。

PreparedStatement select = null;
ResultSet rs = null;

try { con.prepareStatement("SELECT * FROM registered_students WHERE username = ?");
    select.setString(1, username);
    rs = select.executeQuery();
} finally {
    if (select != null) select.close();
    if (rs != null) rs.close();
}

Just a note - your database query makes me cringe a little bit. It seems open to memory leaks if the Statement and ResultSet objects arent closed, and injection attacks since you are copying input straight into the query.

PreparedStatement select = null;
ResultSet rs = null;

try { con.prepareStatement("SELECT * FROM registered_students WHERE username = ?");
    select.setString(1, username);
    rs = select.executeQuery();
} finally {
    if (select != null) select.close();
    if (rs != null) rs.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文