在enctype =" multipart / form-data"之后请求不工作 [英] after enctype="multipart/form-data" request not working

查看:150
本文介绍了在enctype =" multipart / form-data"之后请求不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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");
    }
}

这是我的Relay servelt,我发送日期来自

This is my Relay servelt, I'm sending date from

 <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>

它给出了java.lang.NullPointerException。
enctype =multipart / form-data//当我删除它的工作正常

It's giving a java.lang.NullPointerException. enctype="multipart/form-data" // when im remove its works fine

推荐答案

为什么你需要添加它呢?请保持原样。

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

如果您需要它来上传文件< input type =file> 您打算稍后添加,然后您应该在您的servlet上放置 @MultipartConfig 注释,以便 request.getParameter()将起作用,所有上传的文件都可以通过 request.getPart()检索。

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 @MultipartConfig annotation on your servlet, so that request.getParameter() will work and that all uploaded files can be retrieved by request.getPart().

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



参见:




  • 如何使用JSP将文件上传到服务器/ Servlet?

  • See also:

    • How to upload files to server using JSP/Servlet?
    • 这篇关于在enctype =&quot; multipart / form-data&quot;之后请求不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆