0

我正在尝试编写一个非常简单的 servlet,而 HTML 表单的操作使用该 servlet。

这是我的 HTML 表单:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Video</title>
</head>
<body>

<%
    Object userSession = session.getAttribute("email");

if(userSession == null)
{
    out.println("Welcome to the MyTube, you are not logged in. <br /><br >");
    out.println("Please click the following to login: <a href=\"login.jsp\">here</a>");
    out.println("Or Click this to register: <a href=\"register.jsp\">here</a>");
}
else
{
    out.println("<form action=\"servletClass\" method=\"post\" enctype=\"multipart/form-data\">");
    out.println("<input type=\"file\" name=\"file\" />");
    out.println("<input type=\"submit\" />");
    out.println("</form>");
}

%>

</body>
</html>

这是我的 servlet 类:

public class uploadServlet extends HttpServlet
{
    String awsAccessKey = "WHOOOPS";
    String awsSecretKey = "WHOOOPS/YWPkmKfe";
    AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);

    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
    {
        boolean isMulti = ServletFileUpload.isMultipartContent(request);
        if (isMulti) 
        {
            ServletFileUpload upload = new ServletFileUpload();

            try 
            {
                FileItemIterator iter = upload.getItemIterator(request);
                while (iter.hasNext()) 
                {
                    FileItemStream item = iter.next();
                    InputStream inputStream = item.openStream();
                    if (item.isFormField()) 
                    {

                    } 
                    else
                    {
                        String fileName = item.getName();
                        if (fileName != null && fileName.length() > 0) 
                        {
                            S3Service s3Service = new RestS3Service(awsCredentials);

                            S3Object fileObject = new S3Object();
                            fileObject.setDataInputStream(inputStream);
                            fileObject.setContentLength(Integer.parseInt(request.getHeader("Content-Length")));
                            s3Service.putObject("vidvidbucket", fileObject);

                            //read stream of file uploaded

                            //store as a temporary file

                            //upload the file to s3
                        }
                    }
                }
            } 
            catch (Exception e) 
            {

            }
        }

        response.sendRedirect("location of the result page");
    }
}

这是我的 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>MyTubeServer</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>servletClass</servlet-name>
    <servlet-class>uploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>servletClass</servlet-name>
    <url-pattern>/servletClass</url-pattern>
  </servlet-mapping>

</web-app>

这是日食设置:

在此处输入图像描述

我查看了其他帖子,您还应该在 WEB-INF 文件夹中包含 java 文件。但我不断收到classNotFoundException。

编辑:堆栈跟踪表明找不到uploadServlet,所以我很困惑。

编辑 2:将 servlet 类的 XML 文件设置为 servletClass.uploadServlet 而不是 servlet 类后,但现在我得到了请求的源 (X/Location%Of%The%File%Is%Not%Found) 没有任何异常

在此处输入图像描述

4

2 回答 2

4

web.xml的不正确。图像显示包层次结构

在此处输入图像描述

所以,应该是这样的

<servlet>
<servlet-name>servletClass</servlet-name>
<servlet-class>servletClass.uploadServlet</servlet-class>
</servlet>

重启Tomcat

其次,尝试在表单操作中给出上下文路径。

<%
String contextPath = request.getContextPath();
%>

out.println("<form action=\""+contextPath+"/servletClass\" method=\"post\" enctype=\"multipart/form-data\">");

编辑:我认为现在调用 servlet 但这是错误的

response.sendRedirect("location of the result page");

将其更改为某个有效JSP页面。

于 2013-04-17T06:24:19.087 回答
3

1)在你的 web.xml 中试试这个:

<servlet>
<servlet-name>servletClass</servlet-name>
<servlet-class>servletClass.uploadServlet</servlet-class>
</servlet>

重新启动服务器并检查 .

2)第二个错误的原因是这一行:

 response.sendRedirect("location of the result page");   

在您的 Servlet 代码中。请重定向到适当的可用资源。有关更多信息,请参阅API

于 2013-04-17T05:41:45.560 回答