Java 在表单中使用多部分/表单数据类型时,无法将参数传递给servlet

Java 在表单中使用多部分/表单数据类型时,无法将参数传递给servlet,java,forms,jsp,servlets,Java,Forms,Jsp,Servlets,我正在尝试从嵌入到jsp文件中的文本编辑器传递格式化文本。我在表单标记中使用enctype=“multipart/form data”。当使用默认enctype时,我可以将参数传递给Underylingservlet。但是,在servlet中使用multipart/form数据类型时,我会得到null 我的表格 <form action="pdfGenServlet" method="post" enctype="multipart/form-data">

我正在尝试从嵌入到jsp文件中的文本编辑器传递格式化文本。我在表单标记中使用enctype=“multipart/form data”。当使用默认enctype时,我可以将参数传递给Underylingservlet。但是,在servlet中使用multipart/form数据类型时,我会得到null

我的表格

<form action="pdfGenServlet" method="post" enctype="multipart/form-data">
                    <!-- input notes title-->
                    <div class="form-group">
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="Title of the notes" name="title">
                        </div>
                    </div>
                    <!-- input notes description-->
                    <div class="form-group">
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="Enter short description" name="description">
                        </div>
                    </div>

                    <div class="form-group">
                      <textarea name="content" id="myEditor"></textarea>

                     <div id="button-panel" class="panel panel-default">
                          <p>
                              <button type="submit" class="btn btn-primary "><span class="glyphicon glyphicon-plus"></span><strong> Create Note</strong></button>
                              <button class="btn btn-primary" type="reset"><strong>Reset</strong></button>
                          </p><!-- buttons -->
                     </div><!-- panel Button -->

                    </div>

                </form> 

请参阅以下链接,它将帮助您上载带有输入参数的图像:


如果您想从HTML(JSP)表单中获取参数,请使用内容属性“enctype=”multipart/form data“

  • 您应该在一个servlet中添加@MultipartConfig,该servlet将接收所有这些参数(同时在传送/分派servlet中添加@MultipartConfig)
  • @MultipartConfig中包含三个元素:fileSizeThreshold、maxFileSize、maxRequestSize
  • (我使用Servlet 3文件上传来上传文件)

我使用下面的代码解决了这个问题。 您应该将@MultipartConfig注释添加到Servlet类中: 例如:

@WebServlet("/admin/create_book")
@MultipartConfig(
        fileSizeThreshold = 1024 * 10,  // 10 KB
        maxFileSize = 1024 * 300,       // 300 KB
        maxRequestSize = 1024 * 1024    // 1 MB 
)
public class CreateBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public CreateBookServlet() {
        super();
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BookServices bookServices = new BookServices(request, response);
        bookServices.createBook();
    }

}

因此,只有在上传文件时才使用multipart enctype,应该如何传递格式化文本,以便将其保存到pdf中..你有解决方案吗?pdf文件的文本格式化必须使用java中的某些api完成。我对它了解不多。做研究。不,先生,实际上没有帮助。
@WebServlet("/admin/create_book")
@MultipartConfig(
        fileSizeThreshold = 1024 * 10,  // 10 KB
        maxFileSize = 1024 * 300,       // 300 KB
        maxRequestSize = 1024 * 1024    // 1 MB 
)
public class CreateBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public CreateBookServlet() {
        super();
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BookServices bookServices = new BookServices(request, response);
        bookServices.createBook();
    }

}