Java 为什么FileItemIterator.hasNext()返回false?
请帮忙。我几乎看过每一个论坛,但都是徒劳的。我不明白为什么hasNext()返回falseJava 为什么FileItemIterator.hasNext()返回false?,java,servlets,Java,Servlets,请帮忙。我几乎看过每一个论坛,但都是徒劳的。我不明白为什么hasNext()返回false public class FileUploadServ extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated
public class FileUploadServ extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
JOptionPane.showMessageDialog(null,"is multipart");
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator itr = upload.getItemIterator(request);
boolean c=itr.hasNext(); // why does this return false
JOptionPane.showMessageDialog(null,"hasNext value:"+c);
while (c=itr.hasNext()){
FileItemStream item = itr.next();
if (item.isFormField()) {
}
else {
String path= getServletContext().getRealPath("/");
if (FileUpload.processFile(path, item))
response.getWriter().println("Upload Successful");
else
response.getWriter().println("Upload Failed!");
}
}
//JOptionPane.showMessageDialog(null,"out of while hasNext value:"+c);
} catch (FileUploadException fue) {
//fue.printStackTrace();
JOptionPane.showMessageDialog(null,"error!:"+ fue.getMessage());
}
}
}
}
您要调用hasNext()两次。第一次是在为变量赋值时,第二次是在循环中。由于您不在任何地方使用该变量,我建议您去掉它,只将hasNext()保留在while循环中。@Kayaman让我们暂时忘记while循环。第一次使用itr.hasNext()初始化布尔变量时,显示的结果为false。为什么@Saahyl我不会忘记while循环,因为你在那里犯了一个基本的错误,你可能在其他地方犯了基本的错误。如果hasNext()返回false,则说明您没有上载文件,或者使用的
ServletFileUpload
错误。