Tuesday, 19 March 2013

How to upload image and read other form data too in jsp

Here we are using the two packages for upload the picture which provides by apache.org.
1)commons-fileupload-1.2.2
2)commons-io-2.4
This two files you can download from google or you can download full example of this program.
after download this two files you have to paste .jar file from this two folder into apche tomate lib files
.these are files
1)commons-fileupload-1.2.2.jar,commons-io-2.4.jar
The first jar files is used for upload your image and second jar file provides functionality for reading the stream data and save data to a file beyond that point.
eg.DeferredFileOutputStream-This class provides utility for store file into disk and this class
available in commons-io-2.4.jar

first your index.html file

<html>
    <head>
        <title>Upload photos</title>
    </head>
    <body>
    <form name="fupload" action="upload.jsp" method="post" enctype="multipart/form-data">
    <table>
    <tr>
    <td>Name:</td>
    <td><input type="text" name="uname" id="uname"></td>
    <tr>
    <tr>
    <td>Address:</td>
    <td><textarea  name="address" id="address"></textarea></td>
    <tr>
    <td>Profile Picture:</td><td><input type="file" name="photo" id="photo"/></td>

    <tr><td><input type="submit" value="upload"/></td>
    </tr>
    </form>
    </body>
</html>

This is above html file which used select the pdf file.and when you uploding any file you have to specify the form attribute  enctype="multipart/form-data" and method="post". after upload your file when you click on submit button its will call the upload.jsp file.

second is upload.jsp fle

<%@ page import="java.util.List" %>
   <%@ page import="java.util.Iterator" %>
   <%@ page import="java.io.File" %>
   <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
   <%@ page import="org.apache.commons.fileupload.disk.*"%>
   <%@ page import="org.apache.commons.fileupload.*"%>
  
   <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  
<%

     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     if (!isMultipart)
     {
     }
     else
     {
       FileItemFactory factory = new DiskFileItemFactory();
       ServletFileUpload upload = new ServletFileUpload(factory);
       List items = null;
       try
       {
               items = upload.parseRequest(request);
       }
       catch (FileUploadException e)
       {
               e.printStackTrace();
       }
       Iterator itr = items.iterator();     //this will create iterator object from list..used for traversing the data.
       String uname="",uadd="";
       while (itr.hasNext())
       {
           FileItem item = (FileItem) itr.next();
         
           if (item.isFormField())   //checking if its normal field then we read as normal. no need to store in disc
            {
                        String name = item.getFieldName();
                       String value = item.getString();
                                           
                       if(name.equals("uname"))
                       {
                               uname=value;
                              
                        }
                       else if(name.equals("address"))
                        { 
                                   uadd=value;                       
                           
                        }                                                       
                                
            }
            else                                      //this else part for process about PDF file
             {
                try
                {
       
                   String itemName = item.getName();    //this will return the pdf file name
                   String filename=request.getRealPath("") + "/uploads/";
                   filename=filename+itemName;       //now concatenation the file name with upload  path.
                   File savedFile=new File(filename);
                      
                       item.write(savedFile);    //saving file into disc,item contain which you select the file.
                                                                //here item will copy into the savedFile and store into disk

                       out.println("successfull");
                       response.sendRedirect("index.html");
                }
                catch(Exception ste)
                {
                    out.println(ste);
                }
              }
          }
          out.println("the user name is"+uname);
          out.println("the address is"+uadd);
       }
%>
The above file which is used the
 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
This will used for checking the whether your request is contain file data and other data or not.

for save pdf file into disc we need to creare FileItemFactory object and ServletFileUpload object

 FileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(factory);

for reading the request we need to parse and it will return the List types.
items = upload.parseRequest(request);
Now items will contain the all data.

The request.getRealPath() will return the where deploy the your application and in my application i create uploads folder and i want to save pdf file in the uploads folder.


     



You can download full example from here

2 comments: