gpt4 book ai didi

java - Apache Commons FileUpload getString() 方法 - Java

转载 作者:行者123 更新时间:2023-12-01 14:09:23 25 4
gpt4 key购买 nike

我的 FileUpload Servlet 代码:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
NewsItems ni = new NewsItems();
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
String [] myValues = new String[6];
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();

int i = 0;
if (item.isFormField()) {
myValues[i] = item.getString();
System.out.println("my vals: "+myValues[i]); //PRINTS THE VALUES
}
i++;
}
String newsContent = myValues[2]; //PRINTS NULL
System.out.println(newsContent);

} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

这里我尝试将 getString() 值分配给 String 数组。在上面的代码中,While 循环内部的 System.out.println 打印值,但外部的 System.out.println 打印 NULL。有什么原因吗?有人可以为我提供解决方案吗?感谢您的帮助。

最佳答案

终于找到答案了。我在 while 循环内初始化 i 犯了一个愚蠢的错误。

int i = 0;
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();

if (item.isFormField()) {
myValues[i] = item.getString();
System.out.println("my vals: "+myValues[i]); //PRINTS THE VALUES
}
i++;
}

关于java - Apache Commons FileUpload getString() 方法 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18649921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com