使用isFormField stream/multipart表单从多个select获取数组。Javaservlet

使用isFormField stream/multipart表单从多个select获取数组。Javaservlet,java,forms,servlets,Java,Forms,Servlets,试图从多选字段中获取值。但只能检索第一个值 在普通形式中,使用for循环迭代字段名,然后将值添加到数组中是没有问题的 但是当使用多部分请求表单时,很难理解如何准确地获得相同的值 比如说, servlet: String sctype = null, sfieldname = null, sname = null; FileItemIterator iterator; FileItemStream item = null; InputStream s

试图从多选字段中获取值。但只能检索第一个值

在普通形式中,使用for循环迭代字段名,然后将值添加到数组中是没有问题的

但是当使用多部分请求表单时,很难理解如何准确地获得相同的值

比如说,

servlet:

      String sctype = null, sfieldname = null, sname = null;
      FileItemIterator iterator;
      FileItemStream item = null;
      InputStream stream = null;
try{

        ServletFileUpload upload = new ServletFileUpload();
        iterator = upload.getItemIterator(request);

      while (iterator.hasNext()) { // iterate over form fields

                          item = iterator.next();
                          stream = item.openStream();
                          String fieldname = item.getFieldName();

         if (item.isFormField()) { // Problem here

                          String value = Streams.asString(stream);

                          //String[] valueArray = Streams.asString(stream) //test

                 if (fieldname.equals("title")){

                      title = value;
                  }

                 if (fieldname.equals("multipleSelect")){

                      //multipleSelect = valueArray[]; //test
                      multipleSelect = value;  // only gives one value :S 

                 } else { // gets values from uploaded files

                      sfieldname = item.getFieldName();
                      sname = item.getName();
                      sctype = item.getContentType();

                      stream.close();

                 }

      } // if form
  } // while

} ....( try catch block/finally, etc)...
jsp/html

    <form action="FormServlet" method="post" enctype="multipart/form-data">

        <textarea class="form-control" name="title" placeholder="Title"></textarea>

        <select  class="form-control"  name="multipleSelect" multiple="multiple">
            <option value="ring">Ring</option>
            <option value="necklace">Necklace</option>
        </select>

       <input type="file" name="file1" size="50" multiple>

    </form>

戒指
项链

希望这是清楚的。非常感谢任何指点。谢谢你抽出时间

编辑:

解析的结果是一个文件项列表,每个文件项都实现FileItem接口。​

e、 g

List items=upload.parseRequest(请求);
迭代器iter=items.Iterator();
while(iter.hasNext()){
FileItem=(FileItem)iter.next();
if(item.isFormField()){
字符串名称=item.getFieldName();
字符串值=item.getString();
List valueArray=新建ArrayList();
if(name.equals(“multipleSelect”))
值数组​.增加(价值);
}

因此,您所要做的就是逐个获取所有值,然后将其转换为所需的数据类型(即-收集列表中的所有内容并将其转换为字符串[]:)

以下是我使用的一个简单的解决方法

  • 之间创建一个隐藏文本字段,用于将下拉列表中的选定项值存储为分隔字符串(本例中为逗号)。(即:“戒指,项链”)

  • 在servlet中,可以使用string
    split()
    命令解析值

    if (fieldname.equals("multipleSelect")){
        String[] valueArray = value.split(","); //split string by ,
        //do your array stuff here, for example
        for (String individualValue: valueArray ) {           
            //play with individual dropdown item here, for example
            System.out.println(individualValue);
        }
    }
    

  • 我没有发现您提供的代码有任何问题


    代码运行良好,我认为这可能是commons jar版本的问题。

    感谢您提供的信息,但这并不能真正回答问题。获取“title”的字段名不是问题。困难的是在多部分表单中获取多选字段……(因此在流中处理数组,而不是字符串)看到你的编辑,尝试过,仍然不起作用。不幸的是..这似乎只给出了多选字段的最后一个选定值,仍然。感谢你的帮助。谢谢你,老兵,这非常好!非常感谢。尝试选择多个选项作为选择输入。你会看到你只能选择1个选项。我会如果没有问题,ldn就不会问这个问题。所有JAR都已正确实现。我没有复制它,并且我使用了上面提供的代码,我能够在select中选择多个值。当我选择多个值时,我可以看到打印多个值,而当我选择单个值时,可以看到单个值。“它没有在我这端复制”很明显。如果您使用与上面完全相同的代码,您不可能有多个选择选项工作……也许您忘记了enctype=“multipart/form data”“,或者正在做一些不同的事情。请显示您的代码/屏幕截图。我很好奇您认为如何使用上面提供的相同代码使其工作。Talk很便宜。
    <input type="hidden" id="multipleSelectValues" name="multipleSelectValues" />
    
    $("select[name='multipleSelect']").change(function() {
        var arr = $("select[name='multipleSelect']").val(); //automatically creates an array of selected values
        var foo = arr.join(","); //creates a comma delimited string (i.e:'ring,necklace')
        $( "#multipleSelectValues" ).val(foo); //update hidden field value
    });
    
    if (fieldname.equals("multipleSelect")){
        String[] valueArray = value.split(","); //split string by ,
        //do your array stuff here, for example
        for (String individualValue: valueArray ) {           
            //play with individual dropdown item here, for example
            System.out.println(individualValue);
        }
    }