【发布时间】:2010-06-11 00:21:30
【问题描述】:
我正在尝试将文件上传到 jsp 中,然后在其他代码中使用该文件。我的问题是它通过 request.getAttribute() 调用作为对象进入 servlet,所以我不知道将其转换为什么。
到目前为止,我有这段代码可以尝试测试它是什么,但我得到了 NullPointerException。
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Input Test</title>
</head>
<body>
<form action="InputServlet" method="POST">
<input type="file" name="file1">
<input type="submit" value="submit">
</form>
</body>
</html>
inputservlet.java
public class InputServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(request.getAttribute("file1").getClass());
}
}
是我对发生的事情的理解有缺陷还是我只是编码错误?
此外,我希望类型是 Object,所以如果有人知道我也应该转换它,那也将非常有帮助。
【问题讨论】:
标签: java jsp servlets jakarta-ee