How to pass additional information using <input type = "file">?
I need to upload files to a servlet running on tomcat. In addition to the file, I want to allow the user to add a comment related to the downloaded file. I tried the following, but this did not work:
<form action='MyUploadServlet' enctype='multipart/form-data' method='POST'>
<input type='file' name='filechooser'><br />
<textarea name='comment' cols='15' rows='5'></textarea>
<input type='Submit' value='Upload'><br />
</form>
Here is a snippet of server code:
@WebServlet("/MyUploadServlet")
public class MyUploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletContext sc = request.getServletContext();
String comment = (String)request.getParameter("comment");
....etc}
The missing part of the server code is related to retrieving the contents of the downloaded file.
The string request.getParameter ("comment") returns null. I use several servlets and several forms in my application. In all other places, if I use request.getParameter ("form-input-name"), I always get the value of the corresponding input field. This is the only exception, namely when the form has an input field of type "file".
upload submit?
API, .
HTML multipart/form-data application/x-www-form-urlencoded . multipart/form-data submit. getParameter() application/x-www-form-urlencoded.
A multipart/form-data multipart/form-data, Apache Commons FileUpload, - . , Servlet 3.0 ( , -, , Servlet 3.0, @WebServlet ), getParts(), Apache Commons FileUpload. , , Apache Commons FileUpload. : JSP/Servlet?