为什么我收到“Error 500: java.lang.NullPointerException” java servlet
我正在尝试将文件从我的 html 页面上传到我的 servlet 端代码并将其存储在 arraylist 中,
这里是 my html:
<pre>
<!DOCTYPE HTML>
<html>
<head>
<title>file upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="url to my servlet java code" method="post" ENCTYPE="multipart/form-data">
<input type="file" value="browse..."/>
<br/>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
</pre>
。 。
。 。
这是我的 servlet 页面的 doGet() 方法中的内容
Part p1 = request.getPart("textfile.txt");
Scanner in = new Scanner(p1.getInputStream());
ArrayList<String> newList = new ArrayList<String>();
while(in.hasNextLine()){
newList.add(in.nextLine());
}
Collections.shuffle(newList);
,因此一旦我选择所需的文本文件并点击上传,我就会收到 nullpointerexception 错误。
帮助?
I am attempting to upload a file from my html page to my servlet side code and store it in an arraylist
heres my html:
<pre>
<!DOCTYPE HTML>
<html>
<head>
<title>file upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="url to my servlet java code" method="post" ENCTYPE="multipart/form-data">
<input type="file" value="browse..."/>
<br/>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
</pre>
.
.
.
.
heres what i have in my servlet page's doGet() method
Part p1 = request.getPart("textfile.txt");
Scanner in = new Scanner(p1.getInputStream());
ArrayList<String> newList = new ArrayList<String>();
while(in.hasNextLine()){
newList.add(in.nextLine());
}
Collections.shuffle(newList);
so once i select the text file i want and hit upload, i get a nullpointerexception error.
help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为当用户到达页面时,这是一个
GET
操作,因此根本不需要向页面提供任何数据。但您假设在代码中getPart
未返回null
。然而,getPart
被明确定义为返回null
if "...此请求的类型为 multipart/form-data,但不包含请求的 Part ." (参考)。您的表单被定义为使用
POST
,因此您希望在doPost
函数,不是您的doGet
函数。Because when the user arrives on the page, it's a
GET
operation, and so there's no requirement at all that any data has been provided to the page. But you're assuming in your code thatgetPart
is not returningnull
. And yet,getPart
is clearly defined as returningnull
if "... this request is of type multipart/form-data, but does not contain the requested Part." (ref).Your form is defined as using
POST
, so you want to handle it in yourdoPost
function, not yourdoGet
function.有 2 个主要问题:
该代码必须位于
doPost()
方法内。不要混淆它们,也不要将其中一个称为另一个。这是明显糟糕的设计(是的,我知道大多数教程都是这样展示的,但这更多地讲述了教程本身)。另请参阅我们的 servlet wiki 页面,了解如何正确使用 servlet。当您期望元素的名称为“textfile.txt”时,您尚未在输入元素上指定任何名称(这本身没有任何意义,您似乎期望上传文件的文件名自动成为输入元素名称,您认为如果最终用户选择具有不同名称的文件,它会如何工作?)。您需要以通常的方式为输入元素指定一个名称,以便您可以通过该名称准确地获取
Part
。例如与
您只需确保已输入 servlet 上的
@MultipartConfig
注释。另请参阅 如何使用 JSP/ 将文件上传到服务器的底部Servlet?There are 2 major problems:
That code has got to be inside
doPost()
method. Do not mix them up nor call the one from the other on. This is plain bad design (yes, I know that most tutorials show them that way, but that tells more about the tutorial itself). See also our servlets wiki page to learn how to use servlets properly.You haven't specified any name on the input element while you're expecting that the element has the name of
"textfile.txt"
(which makes at its own no sense, you seem to be expecting that the filename of the uploaded file automagically becomes the input element name, how did you think that it would work if the enduser chooses a file with a different name?). You need to give the input element a name the usual way so that you can get thePart
by exactly that name. E.g.with
You only need to make sure that you've put
@MultipartConfig
annotation on the servlet. See also bottom part of How to upload files to server using JSP/Servlet?