Oct
11
2014

Servlet file upload example in java

In this post of java tutorial i will post about how to upload file in java and what is best method of file upload.

There are 2 ways to upload file.

  • Store your file in database
  • Store file path in database and the actual file in disk space.

Now what is difference in both the approaches.

In first approach where you store file in database is worst approach ,because by doing so you are not only increasing your database size very rapidly but slowing down your system performance .The reason for performance degradation is can be find by comparing the two approach .

When we store a complete file in database then we are increasing number of hits to our database system.  When upload a 2 MB file then to store a 2 MB your database system has to more stuff than storing a 2 byte file path. So to it is always recommended to store file path in database not the complete .

The Second reason is database size .When we host our site over remote server then limited database size is provided depending on your hosting plan.In case if your website has a corner to upload data ( e.g – example many job portal has resume upload and photo upload section) in that case your database size would increase very rapidly if you opted first approach .

Finally lets suppose we have a complete file in database then while retrieving that file from database would take more time than to retrieving it from disk space . So server  response is this case is quick.

That was comparison between the two approach . Now in second approach i have seen most of beginner level programmer specify file path to a Drive path like  E:\new folder . This work fine on local system. But when we deploy our website on remote server ,then problem arise because on remote server there is no such Drive . In this case it fall to error .

Now i will let you know the best and simple approach for file upload . create a folder in WebContent opf your project as shown

Servelet File Upload in java

prerequisites

  • Eclipse IDE
  • Tomcat 7.0 and above
  • JDK 6.0 and above

index.jsp

Below is simple form with one input field whose type is ‘file’ and a submit button .Please note that in form tag do not forget to add enctype = multipart /form-data .Otherwise it will end up with an error.

 

home.jsp

This is simple page to give a message  that your file has been uploaded .

DBConnection.java

DBConnection.java is created not to mess up our file upload code with database connectivity code . It is always batter approach to keep separate your database connectivity code from rest of the code .  If you do know database connectivity the please read database connectivity post .In the below code i have written getMySQlConnection() static function which will return a connection object.We will call this method from UploadServlet.java .

 

UploadServlet.java

UploadServlet.java is Servlet class to upload file . Folder name that you have created in WebContent need to specify in following line and with file path. In my case i have created a folder ‘fileFolder’ in webcontent . You need to replace it with your folder name .

File savedFile = new File(request.getRealPath(“/”)+”fileFolder/”+itemName);

url=”/fileFolder/”+itemName;

 

web.xml

In web.xml we need to specify welcome file name and Servlet cladd and their mapping .

This is all we are done with the coding part .Now run your project .Here is what i got .

Servelt File Upload

 

After browse a file when submit then if file get upload successfully the following   screen will appear .

Servlet File Upload

 

 

Below is the database image of table .

Servlet File Upload

Now if you might be thinking that you can see this file in your folder that you created in WebContent . Of course it is there but you can not see it directly in eclipse .If you want to see it then you have to search it workspace of your project .If you don’t know workspace of your project then just this.  RightClick on your project  >Properties

Here is the workspace of your project .

Servlet File Upload

Note that my workspace name is D:\java workspace\luna  

ServletFileUpload is the name of project .

In workspace follow the path as shown below .In the folder name you will find file that you have uploaded .

D:\java workspace\luna\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletFileUpload\fileFolder

Now you might be thinking how to read this file from here . We don’t need to get file from here .File path that we stored in database will be enough to get the stored file .

Download this example 

This is all in this post . In the next post i will post about how to read a file in Servlet . By then take care and enjoy development .

Comments are closed.