5:24 am - Wednesday May 22, 2013

Upload files using jsp with file uploader progress bar

This tuto­r­ial gives you an overview on how to Upload files using jsp with file uploader progress bar using upload­ify and jqU­ploader. You can down­load the fin­ished project down below.

Cre­ate a file ajaxupload.jsp and you would need to add a form element,input file ele­ment and apply some styles to it and add a uploader con­trol from JQU­ploader to a jsp page like  the code below.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>upload using progress bar</title>
        <script type="text/javascript" src="assets/js/jquery-1.4.4.min.js"></script>
        <script type="text/javascript" src="assets/js/jquery.uploadProgress.js"></script>
        <script type="text/javascript">
          $(function() {
             $('form').uploadProgress({
               /* scripts locations for safari */
               jqueryPath: "assets/js/jquery-1.4.4.min.js",
               uploadProgressPath: "assets/js/jquery.uploadProgress.js",
               start:function(){},
               uploading: function(upload) {$('#percents')
                            .html(upload.percents+'%');},
               interval: 2000
             });
         });
       </script>
 <style type="text/css">
 .bar {
 width: 300px;
 }

 #progress {
 background: #eee;
 border: 1px solid #222;
 margin-top: 20px;
 }
 #progressbar {
 width: 0px;
 height: 24px;
 background: #333;
 }
 </style>
 </head>
 <body>
 <form id="upload" enctype="multipart/form-data"
    action="upload.jsp" method="post">
 <input name="file" type="file"/>
 <input type="submit" value="Upload"/>
 </form>

 <div id="uploading">
 <div id="progress" class="bar"></pre>
<div id="progressbar">&nbsp;</div>
<pre>
 </div>
 </div>
 <div id="percents"></div>
 </body>
</html>


Then, you w’d need to add a jsp page where in you have to write the func­tion­al­ity for upload­ing files using apache com­mons library.Create a file “upload.jsp” and add the code like the one below.

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

<%
       boolean isMultipart = ServletFileUpload.isMultipartContent(request);
           FileItemFactory factory = new DiskFileItemFactory();
           ServletFileUpload upload = new ServletFileUpload(factory);
           //System.out.println(upload.parseRequest(request));
           List items=null;
           try {
                items = upload.parseRequest(request);
           } catch (FileUploadException e) {
                   e.printStackTrace();
           }
           Iterator itr = items.iterator();
           while (itr.hasNext()) {
                FileItem item = (FileItem)(itr.next());
                //out.println("itr");
                if (item.isFormField()) {
                            try{
                                String field=item.getFieldName();
                                String value=item.getString();
                                System.out.println("field="+value);
                            }
                            catch(Exception e){}
                }
                else {
                        try {
                                UUID uuid = UUID.randomUUID();
                                String itemName = item.getName();
                                String tempName = uuid.toString();
                                File savedFile =
                                 new File("D:\\file_uploads\\"+ tempName + "_"+
                                         itemName);
                                item.write(savedFile);
                                out.println("done");
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
           }
%>
</table>
</center>

Here is the demo project which gives you an overview on how to upload files using  jsp. If you have already installed Net­beans you can sim­ply down­load the project which is zip file, you need to extract the zip file and open it with net­beans. You can upload files using upload­ify control, open uploadify.jsp file. Build the project and run the project.

down­load the project here

You May Also Like:

Filed in: Java, Jsp

3 Responses to “Upload files using jsp with file uploader progress bar”

  1. Satish
    December 12, 2012 at 7:04 pm #

    thanx…working fine..

  2. Anirban
    March 28, 2013 at 1:00 pm #

    Hi,

    It will work fine but I am search­ing for a dif­fer­ent solu­tion. Like here you have used a time inter­val of 2 sec. Thats a fixed value but if I want to upload a file of 3 kb it wont take that time so what the pro­gressn bar will show??

    my query is the exact upload time and the exact progress bar per­cent­age. If I upload a file with 3 kb it will show the actual time and actual pre­cen­t­age of load­ing and also for 3 MB file exact tim­ing and percentage.

    If you have any solu­tion please let me know.

    Thanks in advance.

    Rea­grds,
    Anirban

  3. Cavalera
    May 1, 2013 at 1:55 am #

    Thanks man! I was search­ing a long time for some­thing like this.

Leave a Reply