Sunday, April 7, 2013

Woking with enctype="multipart/form-data" and request.getParameter for file upload with jsp,servlet using Eclipse and Apache Tomcat



when we works with enctype="multipart/form-data" many coders faces problem regarding request.getParameter that it is not working and here we are providing solution regarding this problem.
Here we are using enctype="multipart/form-data" on jsp form page for uploading file(eg. image and image size limit is 1 mb for multipart)
To work with MultipartRequest class you need to import cos.jar to your project. So firstly Download it.

Download link:

Project Structure in Project Explorer:

stuform.jsp
here in form tag you can see enctype="multipart/form-data"  which we are using for file upload.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Insert title here</title>  
 </head>  
 <body bgcolor="gray">  
 <center >  
 <h1> Student Information</h1>  
 <form action="Test" method="post" enctype="multipart/form-data" >  
 <table bgcolor="lightblue"><tr><td>Select Image:</td><td><input type="file" name="pic"/><br/></td></tr>  
 <tr><td>Select Sign:</td><td><input type="file" name="sign"/><br/></td></tr>  
 <tr><td>Name:</td><td><input type="text" name="name"/><br/></td></tr>  
 <tr><td>Email:</td><td><input type="text" name="email"/><br/></td></tr>  
 <tr><td>Password:</td><td><input type="password" name="pwd"/><br/></td></tr>  
 <tr><td><input type="submit" value="Upload"></td></tr></table>  
 </form>  
 </center></body>  
 </html>  


web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  <display-name>Test</display-name>  
  <welcome-file-list>  
    <welcome-file>stuform.jsp</welcome-file>  
  </welcome-file-list>  
  <servlet>  
   <description></description>  
   <display-name>Test</display-name>  
   <servlet-name>Test</servlet-name>  
   <servlet-class>blog.webideaworld.Test</servlet-class>  
  </servlet>  
  <servlet-mapping>  
   <servlet-name>Test</servlet-name>  
   <url-pattern>/Test</url-pattern>  
  </servlet-mapping>  
 </web-app>  

Test.java
com.oreilly.servlet.MultipartRequest
A utility class to handle multipart/form-data requests,the kind of requests that support file uploads.
 package blog.webideaworld;  
 import java.io.IOException;  
 import java.io.PrintWriter;  
 import javax.servlet.ServletException;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import com.oreilly.servlet.MultipartRequest;  
 @SuppressWarnings("serial")  
 public class Test extends HttpServlet {  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
      }  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
           response.setContentType("text/html");  
           PrintWriter out=response.getWriter();  
           MultipartRequest m=new MultipartRequest(request, "C:/MyWork/Test/WebContent/images/");  
           String pic=m.getFilesystemName("pic");  
           String sign=m.getFilesystemName("sign");  
           String name=m.getParameter("name");  
           String email=m.getParameter("email");  
           String pwd=m.getParameter("pwd");  
           out.println("<body bgcolor=gray><center><h1>Student Information</h1>");  
           out.println("<table bgcolor=lightblue><tr><td>Photo of "+name+" :</td><td><img src=/Test/images/" +pic + " width=120px height=150px><br/></td></tr>");  
           out.println("<tr><td>Sign of "+name+" :</td><td><img src=/Test/images/" +sign + " width=120px height=40px><br/></td></tr>");  
           out.println("<tr><td>Email:</td><td>"+email+"</td></tr>");  
           out.println("<tr><td>Password:</td><td>" +pwd+"</td></tr></table>");  
           out.println("</center></body>");  
      }  
 }  

Download Code(Test.rar-78kb)

View in Browser:

5 comments:

AngularTechAmit said...

good content..............

Rishikesh said...

very helpful...thanks

Anonymous said...

very helpful, thanks but how do we do to limit and check the extensions. For example to limit .jpeg

Blog Admin said...

it is a validation part that you want to perform...
there are two ways to perform it
1. client side 2. server side

Example with client side using javascript
html code
<body>
<form action="uploadAction" method="post" enctype="multipart/form-data" onsubmit="return validation(this)">
<input type="file" name="file"/>
<input type="submit" value="Upload"/>
<div id="valid_msg"/>
</form>
</body>

javascript code: put inside head tag
function validation(thisform)
{
with(thisform)
{
if(validateFileExtension(file, "valid_msg", "pdf/office/image files are only allowed!",
new Array("jpg","pdf","jpeg","gif","png","doc","docx","xls","xlsx","ppt","txt")) == false)
{
return false;
}
if(validateFileSize(file,1048576, "valid_msg", "Document size should be less than 1MB !")==false)
{
return false;
}
}
}

Blog Admin said...

please try to connect us with your email id...so that we can answer to your email...thanks for your precious comments

Popular Posts