0

I want to avoid duplication while uploading file. If a file is updated then eventhough it has same name as which was uploaded previously, I should be able to upload that file on server.

I have written following servlet:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("from").equals("upload")) {

            // checks if the request actually contains upload file
            if (!ServletFileUpload.isMultipartContent(req)) {
                PrintWriter writer = resp.getWriter();
                writer.println("Request does not contain upload data");
                writer.flush();
                return;
            }

            // configures upload settings
            DiskFileItemFactory factory = new DiskFileItemFactory();

            factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

            ServletFileUpload upload = new ServletFileUpload(factory);

            // constructs the directory path to store upload file
            String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
            // creates the directory if it does not exist
            File uploadDir = new File(uploadPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }

            try {
                // parses the request's content to extract file data
                List formItems = upload.parseRequest(req);
                Iterator iter = formItems.iterator();

                // iterates over form's fields
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    // processes only fields that are not form fields
                    if (!item.isFormField()) {
                        String fileName = new File(item.getName()).getName();

                        filePath = uploadPath + File.separator + fileName;
                        File storeFile = new File(filePath);
                        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
                        System.out.println(f.format(storeFile.lastModified()));
                        System.out.println(storeFile.lastModified());
                        System.out.println(f.parse(f.format(storeFile.lastModified())));

                        File[] files = new File(
                                "C:\\bootcamp\\programs\\eclipse-jee-neon-RC3-win32-x86_64\\eclipse\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\excelFileManagement\\upload")
                                        .listFiles();
                        int uploadFiles=0;
                        for (File file : files) {


                                if (fileName.equals(file.getName())) {
                                    uploadFiles =1;
                                    System.out.println("same");
                                    DateFormat df = new SimpleDateFormat("yyyy-mm-dd hh:mm:sss");
                                    String currentFile = df.format(storeFile.lastModified());
                                    String storedFile = df.format(file.lastModified());
                                    System.out.println("currentFile" + currentFile + "storedFile" + storedFile);
                                    if (currentFile.contains(storedFile)) {
                                        System.out.println("Same file cannot be uploaded again");
                                        getServletContext().getRequestDispatcher("/Error.jsp").forward(req, resp);
                                    } else {
                                        // saves the file on disk
                                        item.write(storeFile);
                                        System.out.println("Upload has been done successfully!");
                                        // Reading excel file
                                        ReadingExcelFile rd = new ReadingExcelFile();
                                        rd.readExcel(filePath);
                                        getServletC

ontext().getRequestDispatcher("/DisplayTables.jsp").forward(req, resp);


                                    } 
                                }
}
catch (Exception ex) {
                System.out.println("There was an error: " + ex.getMessage());
            }}

However, I am getting same last modified date and time for both the files. And if a new file is uploaded storeFile.lastModified() returns Thu Jan 01 05:30:00 IST 1970 value

Radhika Kulkarni
  • 298
  • 1
  • 4
  • 19

1 Answers1

0

Can you confirm what is actual lastModified date of the file already uploaded by OS explorer ?

Second thing in SimpleDateFormat constructor arg m stands for minutes and M stands for month.Also S stands for millsecond.So your correct code would be SimpleDateFormat("yyyy-MM-dd hh:mm:S")

Can you try with these changes and check ?

gero
  • 15
  • 3