根据以下代码设置映射时遇到问题。通过这种方式,从用户接收参数的值,但在根据相关键在映射中输入值后,打印null。请引导我
public class ImportBatchCardRespServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private FileOutputStream out;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// Path path = Paths.get("F:\");
System.out.println("ImportBatchCardRespServlet-AccessFilesPath: " + getInitParameter("AccessFilesPath")); // vahid-log
Path path = Paths.get(getInitParameter("AccessFilesPath"));
// List<FileItem> items = new ServletFileUpload(new
// DiskFileItemFactory()).parseRequest(request);
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory())
.parseRequest(new ServletRequestContext(req));
Map<String,String> formFields = new HashMap<>();
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select,
// etc).
String fieldName = item.getFieldName();
String fieldValue = item.getString();
System.out.println("req-fieldName: "+fieldName+",req-fieldValue: "+fieldValue); //vahid-log
formFields.put(fieldName,fieldValue);
System.out.println("map-fieldName: "+formFields.get(fieldName)+",map-fieldValue: "+formFields.get(fieldValue)); //vahid-log
formFields.put(fieldName,fieldValue(;
将值/密钥对添加到Hashmap中;fieldName";是关键;fieldValue";值。在下面的行中。。。
System.out.println("map-fieldName: "+formFields.get(fieldName)+",map-fieldValue: "+formFields.get(fieldValue)); //vahid-log
您似乎想要测试打印键/值对。然而,HashMap的";得到";方法参数是键/值对的键,因此
formFields.get(fieldName)
将返回键/值对的值(即fieldValue(和
formFields.get(fieldValue)
可能会返回null,因为没有与该值同名的键。正确的输出应该可以使用这个检查行:
System.out.println("map-fieldName: "+fieldName+",map-fieldValue: "+formFields.get(fieldName)); //vahid-log
干杯!