java 在 enctype="multipart/form-data" 请求不起作用之后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12974890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:01:28  来源:igfitidea点击:

after enctype="multipart/form-data" request not working

javajspservlets

提问by Amal Prasad

public class Relay extends HttpServlet {   
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String command = request.getParameter("command");
        RequestDispatcher rd =request.getRequestDispatcher(command);
        rd.forward(request, response);
        System.out.println("Request forwarded to " + command + " servlet");
    }
}

This is my Relay servlet, I'm sending data from this form

这是我的中继 servlet,我从这个表单发送数据

<form action="Relay" method="POST" enctype="multipart/form-data"> /
    <input type="hidden" name="command" value="AddProduct" />
    <input type="text" name="pname" value="" />
    <input name="" type="submit" value="Add Product">
</form>

It is throwing a java.lang.NullPointerException.

它正在抛出一个java.lang.NullPointerException.

But works fine when I remove this:

但是当我删除它时工作正常:

enctype="multipart/form-data"

回答by BalusC

Why do you need to add it then? Just keep it out.

那为什么需要添加呢?把它放在外面。

If you need it in order to upload a file by <input type="file">which you intend to add later on, then you should put @MultipartConfigannotation on your servlet, so that request.getParameter()will work and that all uploaded files can be retrieved by request.getPart().

如果您需要它来上传<input type="file">您打算稍后添加的文件,那么您应该@MultipartConfig在您的 servlet 上添加注释,这样就request.getParameter()可以工作并且所有上传的文件都可以通过request.getPart().

@WebServlet("/Relay")
@MultipartConfig
public class Relay extends HttpServlet {   
    // ...
}

See also:

也可以看看:

回答by Maciej Dragan

Parameters encoded with multipart/form-dataare sent in POST body - not as regular request parameters, therefore can't be read using request.getParamter(...).

用 编码的参数multipart/form-data在 POST 正文中发送 - 不是作为常规请求参数,因此不能使用request.getParamter(...).

Check out Commons file uploadpackage for multipart requests processing.

查看Commons 文件上传包以进行多部分请求处理。

回答by Amol Patil

I am including this just for additional information for troubleshooting. if you are stuck and want to know about what all parameters are coming through multipart request you can print all parameters using following code.

我包含此内容只是为了提供故障排除的附加信息。如果您遇到困难并想知道通过多部分请求获得的所有参数是什么,您可以使用以下代码打印所有参数。

MultipartRequest multi = <Your code to retrieve multipart request goes here. Sorry but can not post code as I use proprietary APIs>  

Enumeration en1 = multi.getParameterNames();
        while (en1.hasMoreElements()) { 
            String strParamName = (String)en1.nextElement(); 
            String[] strParamValues = multi.getParameterValues(strParamName); 

            for (int i = 0; i < strParamValues.length; i++) { 
            System.out.println(strParamName + "=" + strParamValues[i]); 
            } 

        }