我正在尝试将文件从我的 html 页面上传到我的 servlet 端代码并将其存储在数组列表中
这是我的 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 错误。
帮忙?
最佳答案
因为当用户到达页面时,这是一个 GET
操作,因此根本不需要向页面提供任何数据。但您假设在代码中 getPart
未返回 null
。然而,getPart
被明确定义为返回 null
if "...此请求的类型为 multipart/form-data,但不包含请求的 Part ." (ref)。
关于java - 为什么我得到 "Error 500: java.lang.NullPointerException"java servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9268987/