2016-09-26 2 views
0

파일을 업로드하는 동안 중복을 피하고 싶습니다. 이전에 업로드 한 파일과 이름이 같더라도 파일이 업데이트되면 해당 파일을 서버에 업로드 할 수 있어야합니다. 두 파일을서버에 파일을 업로드하는 동안 중복을 피하십시오.

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

그러나, 나는 점점 오전 같은 마지막으로 수정 한 날짜와 시간 :

나는 다음과 같은 서블릿을 작성했습니다. 새 파일이 업로드되면 storeFile.lastModified()가 Thu를 반환합니다 Jan 01 05:30:00 IST 1970 값

+0

아, 아이러니 .... (중복 방지에 관한 질문은 중복입니다.) –

+0

업데이트 된 파일은 중복으로 분류해서는 안됩니다. –

답변

0

OS 탐색기에서 이미 업로드 한 파일의 실제 lastModified 날짜를 확인할 수 있습니까?

SimpleDateFormat의 생성자의 인자의 m에서

두 번째 것은 분의 약자 month.Also S가 SimpleDateFormat의 것 millsecond.So 올바른 코드의 약자에 대한 M 스탠드 ("YYYY-MM-DD HH : MM : S")

이러한 변경을 시도하고 확인할 수 있습니까?

+0

실제 수정 날짜는 2016/25/09 10:46 : 009 –

관련 문제