GetParts()在servlet中返回null


取之
2024-12-18 12:04:27 (3月前)
  1. 大家好,我面临着我的代码问题,我有一个jsp表单,将文件发送到servlet,当我尝试使用request.getParts()提取文件时,当我尝试打印它时返回null值。

2 条回复
  1. 0# 哎?小查查 | 2019-08-31 10-32




    1. Add @MultipartConfig annotation.
      Always parse your request body only once and store it in a list.
      so that one part cannot consume it.

    2. List fileParts = request.getParts().stream().filter(part -> file”.equals(part.getName())).collect(Collectors.toList());

    3. for (Part part : fileParts) { 
    4. }
    5. File uploads = new File(“/path/to/uploads”);
      File file = File.createTempFile(“somefilename-“, “.ext”, uploads);

    6. try (InputStream input = part.getInputStream()) {
      Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
      }

    7. </code>

登录 后才能参与评论