Multipart/form-data how to get parameter hidden

早过忘川 提交于 2019-12-11 07:16:20

问题


I 'm working on a web application. I have my jsp with enctype="multipart/form-data" and when I submit my request, I am unable to get the request parameters in servlet.

The getParameter() calls will all return null. The question is how can overcome this problem?

When it 's not enctyped, this code works fine. I know that this has been asked many times, but I did not find any straight answer

JSP

<form action="upload" method="post" enctype="multipart/form-data">
                <input type="file" name="uploadfile[]" id="uploadfile" size="50" multiple="true" />
                <br/><br/>
                <input type="hidden" name ="e_id" value= <%=userBean.getEid%> />
                <input type="hidden" name ="Uid" value= <%=userBean.getUid()%> />
                <input type="submit" name ="button1" value="Upload" />
            </form>

Servlet

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{

    int e_id =0;
    String uid = null;

    HttpSession session1 = request.getSession(true);    
    if(ServletFileUpload.isMultipartContent(request)){//process only if its multipart content
        try 
        {
          List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
          for(FileItem item : multiparts)
          {
              if(!item.isFormField())
              {
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + name));

                    e_id = Integer.parseInt(request.getParameter("e_id"));
                    uid = request.getParameter("Uid");
              }
              else {}
...

回答1:


You need to annotated your servlet with the @MultipartConfig and to get the value of the parameters you use:

Part idPart = req.getPart("e_id");
try (Scanner scanner = new Scanner(idPart.getInputStream())) {
    String idValue = idPart.nextLine(); // read from the part
} 

I have a project in github with an example of how to use it:

  • Servlet
  • Upload page


来源:https://stackoverflow.com/questions/20827284/multipart-form-data-how-to-get-parameter-hidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!