【问题标题】:GetParts() returning null in servletGetParts() 在 servlet 中返回 null
【发布时间】:2019-08-03 20:31:46
【问题描述】:

大家好,我的代码有问题,我有一个将文件发送到 servlet 的 jsp 表单,当我尝试使用 request.getParts() 提取文件时,它在我尝试打印时返回空值它到控制台,任何人都可以帮助我。这是我的代码。

我的jsp:

 <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>File Upload</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
            <form method="POST" action="/PartsServlet" enctype="multipart/form-data" >
                File:
                <input type="file" name="file" id="file" /> <br/>
                Destination:
                <input type="text" value="/tmp" name="destination"/>
                </br>
                <input type="submit" value="Upload" name="upload" id="upload" />
            </form>
        </body>
    </html>

我的小服务程序:

package Servv;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PartsServlet
 */
@WebServlet("/PartsServlet")
public class PartsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public PartsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        System.out.println("Get Parts "+request.getPart("file"));
        System.out.println("Get Parts "+request.getPart("destination"));
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

【问题讨论】:

  • 添加注释@MultipartConfig.

标签: java file web servlets jakarta-ee


【解决方案1】:
Add @MultipartConfig annotation.
Always parse your request body only once and store it in a list.    
so that one part cannot consume it.

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

    for (Part part : fileParts) { 

    }

File uploads = new File("/path/to/uploads");
File file = File.createTempFile("somefilename-", ".ext", uploads);

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

【讨论】:

  • 你知道如何将我的零件文件存储到新的目的地吗?并感谢您的快速回复。
  • 我已经编辑了我的答案。它显示了如何上传文件并将其存储在您的目的地。
猜你喜欢
  • 1970-01-01
  • 2021-12-10
  • 2023-03-30
  • 2021-01-27
  • 2017-01-05
  • 2015-04-09
  • 2012-05-04
  • 2014-07-25
  • 1970-01-01
相关资源
最近更新 更多