无法发送 multipart/form-data

impossible to send multipart/form-data

我做了一个申请,它收到了两部分:一张图片和一些文字。代码的开头是:

  @WebServlet("/App")
@MultipartConfig(fileSizeThreshold=1024*1024*10,    // 10 MB
        maxFileSize=1024*1024*50,       // 50 MB
        maxRequestSize=1024*1024*100)       // 100 MB
public class App extends HttpServlet {

    private static final long serialVersionUID = 205242440643911308L;
    private static final String PREFIX = "stream2file";
    private static final String SUFFIX = ".tmp";

    private static final String UPLOAD_DIR = "uploads";

    protected void doPost(HttpServletRequest req,
                          HttpServletResponse resp) throws ServletException, IOException {

        JsonObject servletResponse = new JsonObject();

        System.out.println("------------------------------");
        System.out.println(req.getContentType());
        System.out.println("------------------------------");
        String text = null;
        String uuid = UUID.randomUUID().toString();
        Properties props = new Properties();
        File dbPropsFile = new File("/config.properties");
        FileReader fileReader = new FileReader(dbPropsFile);
        props.load(fileReader);

        for (Part part : req.getParts()) {
            if(getFileType(part).equals("image")) {

代码在

上失败
for (Part part : req.getParts()) {

如果我 运行 它与 mvn jetty:run 一起工作,它完全可以正常工作,但是当部署到服务器时,它就崩溃了。请帮忙!!!

当尝试执行 System.out.println(req.getContentType()); 时,它在 Tomcat 和 Jetty 服务器上显示为 null,但它在 mvn jetty:run

上带有预期值

如果 HttpServletRequest.getContentType() 为空,则您的请求不满足 @MultipartConfig 的要求,后者要求 Content-Type 值为 multipart/form-data

确保您的表单首先在网络上正确提交。

也许 确保 servlet 按预期运行,让您将注意力集中在表单提交/请求端。