当使用表单中的multipart / form-data编码类型时,无法将参数传递给servlet

问题描述 投票:2回答:2

我正在尝试从嵌入在我的jsp文件中的文本编辑器传递格式化的文本。我在表单标签中使用enctype =“ multipart / form-data”。使用默认的enctype时,我可以将参数传递给servlet底图。但是在我的servlet中使用multipart / form-data编码时,我得到了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> 

我的pdfGenServlet.java

  @WebServlet("/pdfGenServlet")
 public class pdfGenServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      try {
            // Get the text that will be added to the PDF
            request.setCharacterEncoding("UTF-8");

            String title = request.getParameter("title");
            String description = request.getParameter("description");
            String notes_content = request.getParameter("content");
            Date date = new Date();
        } catch(exception e)
        {e.printStackTrace();}
      }
java forms jsp servlets
2个回答
0
投票

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

http://www.avajava.com/tutorials/lessons/how-do-i-upload-a-file-to-a-servlet.html


0
投票

如果要从HTML(JSP)表单获取该内容属性为“ enctype =“ multipart / form-data”“的参数。

  • 您应该在将接收所有这些参数的Servlet中添加@MultipartConfig(也应在Convey / Dispatch Servlet中添加@MutipartConfig)。
  • @ MultipartConfig中包含三个元素:fileSizeThreshold,maxFileSize,maxRequestSize;
  • ((我使用Servlet 3 File Upload上传文件)
© www.soinside.com 2019 - 2024. All rights reserved.