Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get files from HttpServletRequest in Java Servlet [duplicate]

Tags:

java

servlets

The form in the HTML is like

...
<form method="post" action="/foobar">
  <input type="file" name="attachment" />
  <input type="text" name="foo" />
  ... other input fields
</form>

And the Servlet will be like

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String attachment = request.getParameter("attachement");
    String foo = request.getParameter("foo");
    // get other parameters from the request
    // and get the attachment file
}

And I'm wondering

  1. Is there any ways that do not use 3rd-party libraries to get files from a HttpServletRequest object?

  2. What request.getParameter("attachement") returns? Is it the file name or something else?

  3. Would the binary input be stored automatically by a web container in file system or just in memory temporarily?

like image 971
Wenhao Ji Avatar asked Mar 01 '13 15:03

Wenhao Ji


1 Answers

before anythging your form action should be "POST" and enctype="multipart/form-data".

that said...for you to get the file you must prepare the request yourself.

you should check:

Multipart requests/responses java

like image 144
Ricardo Avatar answered Nov 08 '22 12:11

Ricardo