Loop on Part 和 String 重复 JSP/JEE

Loop on Part and String get duplicated JSP/JEE

我有一个输入表单,其中包含添加多个输入类型文本和类型文件的功能,我正在尝试获取所有值并将它们插入到我的数据库中。 你们能告诉我我的代码复制数据库中的所有值有什么问题吗?

String [] inst=request.getParameterValues("inst");

List<Part> fileParts = request.getParts().stream()
    .filter(part -> "picture".equals(part.getName()))
    .collect(Collectors.toList()); // Retrieves 

<input type="file" name="file" multiple="true">

for (int i=0; i< inst.length; i++ ) {
    /*   Part filePart2=request.getPart("picture");
         InputStream photo2 = null;
         photo2=filePart2.getInputStream();*/

    try {
        // connects to the database
        //Get all the parts from request and write it to the file on server
        for (Part filePartt : fileParts) {
            //   String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
            InputStream fileContent = filePartt.getInputStream();

            // ... (do your job here)
            Class.forName("com.mysql.jdbc.Driver");
            java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");
            PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
            pr.setString(1, inst[i]);
            System.out.println("numero "+i);

            if (fileContent != null) {
                // fetches input stream of the upload file for the blob column
                pr.setBlob(2, fileContent);
            }
            pr.setInt(3, idsub);
            pr.executeUpdate();

        }
    } catch(Exception e){}
}

Result

你有 2 个嵌套的 for 循环。第一个从 inst 数组运行整个长度,并为每个插入所有部分。因此,如果 inst 的长度为 3,并且您有 3 个部分,则每次外部循环变为 +1 时,您将插入 3 个部分。如果您知道列表中的顺序是正确的,您可以这样做:

// ... (do your job here)
        Class.forName("com.mysql.jdbc.Driver");
        java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");

for(int i = 0; i < fileParts.size(); i++) {
        Part p = fileParts.get(i);
        InputStream fileContent = p.getInputStream();


        PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
        pr.setString(1, String.valueOf(i));
        System.out.println("numero "+i);
        if (fileContent != null) {
            // fetches input stream of the upload file for the blob column
            pr.setBlob(2, fileContent);
        }
        // where idsub comes from??
        pr.setInt(3, idsub);
        pr.executeUpdate();

    }

我试着对你的代码没有太多改动,但在 for 循环中启动连接似乎不正确:)

我设法修复它 `String [] inst=request.getParameterValues("inst");

      List<Part> fileParts = request.getParts().stream().filter(part -> "picture".equals(part.getName())).collect(Collectors.toList()); // Retrieves <input type="file" name="file" multiple="true">

      Class.forName("com.mysql.jdbc.Driver");
      java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");

      int i=0;  

            try {
                // connects to the database



            //Get all the parts from request and write it to the file on server

                  for (Part filePartt : fileParts) {
                         //   String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
                            InputStream fileContent = filePartt.getInputStream();

                            // ... (do your job here)
                          PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
                          if (i< inst.length){
                          pr.setString(1, inst[i]);
                    System.out.println("numero "+i);
                          }
                            i++;


                  if (fileContent != null) {
                        // fetches input stream of the upload file for the blob column
                        pr.setBlob(2, fileContent);
                    }
                  pr.setInt(3, idsub);
                  pr.executeUpdate();

                  }



            }
            catch(Exception e){}`