我试图从嵌入在我的jsp文件中的文本编辑器传递格式化文本。我在表单标签中使用了enctype =“multipart / form-data”。我可以在使用默认的enctype时将参数传递给underyling servlet。但是在我的servlet中使用multipart / form-data enctype时我得到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();}
}
答案 0 :(得分:1)
我通过使用以下代码解决了这个问题。 您应该在Servlet类中添加@MultipartConfig批注: 例如:
@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();
}
}
答案 1 :(得分:0)
请参阅以下链接,它将帮助您上传带有输入参数的图像:
http://www.avajava.com/tutorials/lessons/how-do-i-upload-a-file-to-a-servlet.html
答案 2 :(得分:0)
如果要从HTML(JSP)表单获取该内容属性为“ enctype =“ multipart / form-data”“的参数。