Eyal
Eyal

Reputation: 55

Accepting and parsing an uploaded file in a servlet

How do I get the file in a servlet running on Tomcat after using <input type="file"> in the JSP file?

Upvotes: 0

Views: 1604

Answers (3)

Nikolaus Gradwohl
Nikolaus Gradwohl

Reputation: 20124

On the html side you define your form as multipart/formdata

<form class="sendform" method="POST" action="url.to.your.servlet" enctype="multipart/form-data">
...
<input type="file" name="sendfile" value="" />

</form>

on the serverside you can use fileupload from apache-commons

Upvotes: 2

BalusC
BalusC

Reputation: 1108732

If you're using Tomcat 6.0 or older, then use Apache Commons FileUpload. It's the defacto standard to parse file uploads in servlets. If you're using Tomcat 7.0 or newer with the fresh new Servlet 3.0 API, then use API-provided HttpServletRequest#getParts() method.

Longer answer with detailed background information and code examples can be found here.

Upvotes: 2

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

Check this simple tutorial on jGuru. You have to use a MultipartHttpRequest and get the inputstream..

Upvotes: 0

Related Questions