在刷新缓存之前,上传的图像不会出现在新页面上

时间:2016-01-25 11:22:12

标签: servlets caching file-upload

我之前已经看过一些关于此的问题,但我仍然不清楚我应该做些什么。我有一个servlet,它将文件上传到我的web目录中的文件夹,并将图像目录(img / image.jpg)存储在数据库中。当我提交表单并上传文件时,我会重定向到另一个servlet以显示表单中的新信息,但是在我单击ctrl + f5之前图像不会显示。有没有解决的办法?以下是我的文件上传代码:

    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);

0 个答案:

没有答案