我正在尝试编写一个非常简单的 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) 没有任何异常