gpt4 book ai didi

eclipse - 简单 Apache FileUpload API 示例的问题

转载 作者:行者123 更新时间:2023-11-28 23:05:39 25 4
gpt4 key购买 nike

我是 Apache 的 Java FileUpload API 的新手,首先,我找到了一个 tutorial这解释了如何在 Servlet 中使用 FileUpload。我正在使用 Eclipse 3.7 并创建了动态项目来尝试链接中解释的示例。以下是我的项目目录结构。

Project Directory Structure

虽然UploadImage.java的代码与示例中提到的相同,只是我在 servlets 中有 servlet 文件包和文件的 MIME 类型是 JPEG 图片而不是纯文本。现在,我是 Eclipse 中 servlet 开发的新手,但据我了解,从 servlet 创建的类文件必须保存在 WEB-INF\classes 内。文件夹及其在 web.xml 中的条目.此外,index.jsp 代码与给定教程示例中提到的相同。现在,我有 <form action="/servlets.UploadImage" enctype="multipart/form-data" method="post">在我的 index.jsp . 当我尝试运行该项目时,index.jsp看起来很有趣,但是当我选择图像文件并点击上传时,我最终得到了 404 not found错误。另外,如何让 Eclipse 放入生成的类文件 UploadImage.javaWEb-INF\classes当我构建项目时。

在过去的 4 小时里,我一直在努力运行这个简单的示例,并且是 Eclipse 中 servlet 开发的新手,我对如何使用它一无所知,因此非常感谢您的帮助。

注意:所有必需的 .jar 文件都包含在项目库中。

更新:按照 BalusC 的建议进行更改后,我仍然无法解决问题。我提供了项目的 3 个重要文件的确切代码,我认为这些文件与问题有关。项目的目录结构仍然如上所示。

  • index.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Image Upload Example</title>
    <style type="text/css">
    #uploadimage {
    width: 150px;
    height: 150px;
    background: #f8f8f8;
    }
    </style>
    </head>
    <body>
    <div id="uploadimage">&nbsp;</div>
    <form action="servlets.UploadImage" enctype="multipart/form-data" method="post">
    <input type="file" name="file1"><br/>
    <input type="submit" value="Upload File"><br/>
    </form>
    </body>
    </html>
  • UploadImage.java (servlet)

    package servlets;

    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.annotation.*;
    import javax.servlet.http.*;

    import org.apache.commons.fileupload.*;
    import org.apache.commons.fileupload.disk.*;
    import org.apache.commons.fileupload.servlet.*;
    /**
    * Servlet implementation class UploadImage
    */
    @WebServlet("/UploadImage")
    public class UploadImage extends HttpServlet
    {

    private static final long serialVersionUID = 1L;
    private static final String temppath = System.getenv("temp");
    private File tempdir;
    private static final String storepath = "/Uploads";
    private File storedir;

    public void init(ServletConfig config) throws ServletException
    {
    super.init(config);

    tempdir = new File(temppath);
    if (!tempdir.isDirectory())
    {
    throw new ServletException(temppath + " is not a directory.");
    }

    String realpath = getServletContext().getRealPath(storepath);

    storedir = new File(realpath);
    if (!storedir.isDirectory())
    {
    throw new ServletException(storepath + " is not a directory.");
    }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    PrintWriter out = response.getWriter();
    response.setContentType("image/jpeg");
    out.println("<h2 align='center'>Impage Upload Example</h2>");

    DiskFileItemFactory fif = new DiskFileItemFactory();
    fif.setSizeThreshold(5 * 1024 * 1024);
    fif.setRepository(tempdir);

    ServletFileUpload uh = new ServletFileUpload(fif);
    try
    {
    List items = uh.parseRequest(request);
    Iterator itr = items.iterator();
    while (itr.hasNext())
    {
    FileItem item = (FileItem) itr.next();
    if (item.isFormField())
    {
    out.println("File Name = " + item.getFieldName() + ", Value = " + item.getString());
    }
    else
    {
    out.println("Field Name = " + item.getFieldName()
    + ", File Name = " + item.getName()
    + ", Content type = " + item.getContentType()
    + ", File Size = " + item.getSize());
    File file = new File(storedir, item.getName());
    item.write(file);
    }
    out.close();
    }
    }
    catch (FileUploadException fex)
    {
    out.println("Error encountered while parsing the request<br/>" + fex);
    }
    catch (Exception ex)
    {
    out.println("Error encountered while parsing the request<br/>" + ex);
    }
    }
    }
  • web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Apache FileUpload</display-name>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
    <servlet-name>UploadImage</servlet-name>
    <servlet-class>servlets.UploadImage</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>UploadImage</servlet-name>
    <url-pattern>/servlets.UploadImage</url-pattern>
    </servlet-mapping>
    </web-app>

抱歉问了这么长的问题。 :-P

最佳答案

<form action="/servlets.UploadImage" ...>

您的表单操作 URL 以 / 开头,因此与域根相关。假设您的 JSP 文件由

打开

http://localhost:8080/contextname/index.jsp

然后这个相对表单 Action URL会向下面的绝对URL发送POST请求

http://localhost:8080/servlets.UploadImage

虽然它真的应该是

http://localhost:8080/contextname/servlets.UploadImage

因此,删除前导斜杠。

<form action="servlets.UploadImage" ...>

关于您的附加问题:

Also, how can I make Eclipse put generated class file of UploadImage.java in WEb-INF\classes when I build the project.

这已经自动完成了。好吧,准确地说是在 WEB-INF/classes 中,而不是在 WEb-INF/classes 中。 Java 区分大小写。

就是说,您的 URL 模式很奇怪。为什么不只是 /upload

另见:

关于eclipse - 简单 Apache FileUpload API 示例的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9959632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com