0

I've seen a few questions about this before but I'm still unclear with what I'm supposed to do. I have a servlet that uploads a file to a folder in my web directory and stores the image directory (img/image.jpg) in a database. When I submit the form and the file is uploaded I redirect to another servlet to display the new information from the form but the image doesn't show until I click ctrl + f5. Is there a way around this? Below is my code for my file upload:

    String upDir = "C://Users//Ben//Documents//NetBeansProjects//GigForMe//web//img";
    userDAO uDAO = new userDAO();
    String userid = null;
    String Email = null;
    String BIO = null;
    String admin = null;
    String ban = null;
    String password = null;
    String password1 = null;
    int i = 0;


    //process only if its multipart content
    if(ServletFileUpload.isMultipartContent(request)){
        try {
            List<FileItem> multiparts = new ServletFileUpload(
                                     new DiskFileItemFactory()).parseRequest(request);

            for(FileItem item : multiparts){

                if(item.isFormField()){


                    if (i ==0) {
                        userid = item.getString();
                        System.out.println(userid);
                    }else if( i== 1){
                        Email = item.getString();
                        System.out.println(Email);
                    }else if(i == 2){
                        BIO = item.getString();
                        System.out.println(BIO);
                    }else if(i == 3){
                        admin = item.getString();
                        System.out.println(admin);
                    }else if(i == 4){
                        ban = item.getString();
                        System.out.println(ban);
                    }else if(i == 5){

                        password = item.getString();
                        System.out.println(password); 
                        if(password.isEmpty()){
                            int UserID = Integer.parseInt(userid);
                            String userPassword = uDAO.getUserPassword(UserID);
                            uDAO.updateUser(UserID, Email, BIO, admin, ban, userPassword);
                        }else if( password.equals("checkboxPassword")){

                        }



                    }else if(i==6){
                        int UserID = Integer.parseInt(userid);
                        password1 = item.getString();
                        System.out.println(password1);
                        if (password.equals("checkboxPassword")) {
                            uDAO.updateUser(UserID, Email, BIO, admin, ban, password1);
                        }
                    }


                    //uDAO.updateUser(UserID, Email, BIO, admin, ban, userPassword);
                    i++;    

                }else if(!item.isFormField()){
                    String name = new File(item.getName()).getName();
                    Random rn = new Random();
                    int answer = rn.nextInt(1000000000);
                    if (name.length() == 0) {


                    }else{
                        String rename = answer + name;
                        String rename1 = rename.replace(" ","");
                        int UserID = Integer.parseInt(userid);

                        item.write( new File(upDir+ File.separator + rename1));
                        uDAO.updateDirectory(UserID, rename1);
                        //File uploaded successfully
                        request.setAttribute("message", "File Uploaded Successfully");
                    }   
                }
            }


            System.out.println("File Uploaded Successfully");
        } catch (Exception ex) {
            request.setAttribute("message", "File Upload Failed due to " + ex);
        }          

    }else{
        request.setAttribute("message",
                             "Sorry this Servlet only handles file upload request");
    }

    Random rn = new Random();
    int uid = rn.nextInt(1000000000);
    response.sendRedirect("UserManagement?id="+uid);
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Ben C
  • 97
  • 1
  • 10
  • That answer you recommended is not what I'm looking for. I'm able to upload to a directory but the browser doesn't show the uploaded image until I refresh the cache – Ben C Jan 25 '16 at 11:53
  • Found a solution. I used Thread.sleep(3000); before using sendRedirect. This gave the server time to upload the file before loading the next page – Ben C Jan 25 '16 at 12:38
  • That's not a solution. You seem to not understand the problem at all. Carefully re-read the answer in the duplicate, especially the blockquote. – BalusC Jan 25 '16 at 13:25

0 Answers0