Multiple Images Upload [JSP Web-application]

In JSP based web-application, while uploading multiple images from User, following code is working fine when used on Mac-OSX but does Not work on Windows.

Environment Used: Netbeans IDE Apache Tomcat Server

Can anyone please provide some pointers as to what can be the exact problem here?

I am not able to locate the root cause of this behaviour. Is the library being used are not compatible to Windows or any path need to be checked for different OS...

Thanks.

JSP Code:

<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>

<%@ page import="org.apache.commons.io.FileUtils" %>

<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>

<%@ page import="com.oreilly.servlet.multipart.MultipartParser"%>
<%@ page import="com.oreilly.servlet.multipart.Part"%>
<%@ page import="com.oreilly.servlet.multipart.FilePart"%>

<%  
    String fileSavePath_upload_dir;
    String fileSavePath_upload_user_dir;

    // change with user-id
    String user_name = "abc1234567890";

    // local dir to app
    String UPLOAD_DIRECTORY = "Uploads";

    String user_image_dir = user_name;

    String resp = "";

    int i = 1;

    int counter=0;


    fileSavePath_upload_dir = getServletContext().getRealPath("/") + "../../web" + File.separator + UPLOAD_DIRECTORY;/*save uploaded files to a 'Upload' directory in the web app*/
    fileSavePath_upload_user_dir = fileSavePath_upload_dir + File.separator + user_image_dir;

    out.print(fileSavePath_upload_dir);

    if (!(new File(fileSavePath_upload_dir)).exists())
    {
        (new File(fileSavePath_upload_dir)).mkdir();    // creates the directory if it does not exist        
    }

    if (!(new File(fileSavePath_upload_user_dir)).exists())
    {
        (new File(fileSavePath_upload_user_dir)).mkdir();    // creates the directory if it does not exist        
    }

    resp += "<br>Here is information about uploaded files.<br>";
    try
    {
        MultipartParser parser = new MultipartParser(request, 1024 * 1024 * 1024);  /* file limit size of 1GB*/
        Part _part;
        while ((_part = parser.readNextPart()) != null)
        {
            if (_part.isFile())
            {
                FilePart fPart = (FilePart) _part;  // get some info about the file

                String name = fPart.getFileName();

                if (null != name)
                {
                    long fileSize = fPart.writeTo(new File(fileSavePath_upload_user_dir+"/"+user_name+"_"+counter+".png"));
                    resp += i++ + ". " + fPart.getFilePath() + "[" + fileSize / 1024 + " KB]<br>";
                    counter++;
                }
                else
                {
                    resp = "<br>The user did not upload a file for this part.";
                }
            }
        }// end while

    }
    catch (java.io.IOException ioe)
    {
        resp = ioe.getMessage();
    }    
%>
varun bansalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rrzCommented:
I haven't seen the O'Reilly api used in a long time. Now days we use either the apache commons api or the code that is included in Servlet 3.0 api.    Below is demonstration code using the apache  commons api. You will need the jar from https://commons.apache.org/proper/commons-fileupload/  and  the jar from https://commons.apache.org/proper/commons-io/ 
Also, before running the code you need to create a folder called "fileUploads" in the root folder of your web app. Put these two JSPs in the root folder of your web app as well.  
<html><body>
             <form enctype="multipart/form-data" action="fileUpload.jsp" method="post">
                    <input type='file' name='file1'/><br/>
                    <input type='file' name='file2'/><br/>
                    <input type='file' name='file3'/><br/>
                    Type message:<input type="text" name="message" /><br/>
                    <input Type='submit' value='Submit'/>
             </form>
 </body></html>

Open in new window

and here is fileUpload.jsp
<%@ page import="java.util.*,
                 java.io.*,
                 org.apache.commons.fileupload.*,
                 org.apache.commons.fileupload.servlet.*,
                 org.apache.commons.fileupload.disk.*,
                 org.apache.commons.fileupload.util.*"%>
<%
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  out.print("Request content length is " + request.getContentLength() + "<br/>"); 
  if(isMultipart){
                  ServletFileUpload upload = new ServletFileUpload();
                  FileItemIterator iter = upload.getItemIterator(request);
                  FileItemStream item = null;
                  String name = "";
                  InputStream stream = null;
                  while (iter.hasNext()){
                                         item = iter.next();
                                         name = item.getFieldName();
                                         stream = item.openStream();
                    if(item.isFormField()){out.println("Form field " + name + " with value " 
                                               + Streams.asString(stream) + "<br/>");}
                    else {
                          name = item.getName();
                          if(name != null && !"".equals(name)){
                             String fileName = new File(item.getName()).getName();
                             out.println("Client file " + item.getName() + " <br/>with file name "
                                                        + fileName + " uploaded.<br/>");
                             
                             File file = new File(application.getRealPath("/fileUploads/" 
                                                       + fileName));
                             FileOutputStream fos = new FileOutputStream(file);
                             long fileSize = Streams.copy(stream, fos, true);
                             out.print(fileName 
                                + " was uploaded to the fileUploads folder. Size was " +
                                       fileSize + "<br/>");
                          }
                       }
                  }
  } else out.print("Wrong request type!");
%>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.