Perché ho non deterministiche di file errori di eliminazione quando si utilizza DiskFileItem?

StackOverflow https://stackoverflow.com/questions/8313117

  •  25-10-2019
  •  | 
  •  

Domanda

Il mio caricati servlet mi tiene un'eccezione dicendo che il file che sto cercando di sostituire (verso la fine del mio codice) non potrebbe essere eliminato a (apparentemente) casuali. Non so che cosa sta causando questo dato che non sto usando qualunque flusso e il file non è aperto nel browser. Qualcuno sa che cosa potrebbe causare questo? Sono completamente all'oscuro su questo come il codice sembra corretto per me. Questa è la prima volta che ho usato DiskFileItem quindi non sono sicuro che se ci sono delle sfumature per gestire lì.

Tenete a mente che a volte funziona, a volte no. Mi sono perso su questo.

Problema Area:

File destination = new File(wellnessDir + File.separator + fileName + ".pdf");

  System.out.println("destination file exists: " + destination.exists());
  System.out.println("file to be moved exists: " + uploadedFile.exists());

  if(destination.exists()){
    boolean deleted = destination.delete();
    if(!deleted)
      throw new Exception("Could not delete file at " + destination);
  }        

out il mio sistema sempre dire che entrambi file e una destinazione esistono. Sto cercando di ottenere l'upload di sovrascrivere il file esistente.

codice completo: (& pastebin )

private void uploadRequestHandler(ServletFileUpload upload, HttpServletRequest request)
  {
    // Handle the request
    String fileName = "blank";
    try{         
      List items = upload.parseRequest(request);
      //Process the uploaded items
      Iterator iter = items.iterator();
      File uploadedFile = new File(getHome() + File.separator + "temp");
      if(uploadedFile.exists()){
        boolean tempDeleted = uploadedFile.delete();
        if(!tempDeleted)
          throw new Exception("Existing temp file could not be deleted.");
      }
      //write the file
      while (iter.hasNext()) {
        DiskFileItem item = (DiskFileItem) iter.next();
        if(item.isFormField()){
          String fieldName = item.getFieldName();
          String fieldValue = item.getString();
          if(fieldName.equals("fileName"))
            fileName = fieldValue;
            //other form values would need to be handled here, right now only need for fileName
        }else{
          item.write(uploadedFile);
        }
      }
      if(fileName.equals("blank"))
        throw new Exception("File name could not be parsed.");
      //move file
      File wellnessDir = new File(getHome() + File.separator + "medcottage" + File.separator + "wellness");
      File destination = new File(wellnessDir + File.separator + fileName + ".pdf");

      System.out.println("destination file exists: " + destination.exists());
      System.out.println("file to be moved exists: " + uploadedFile.exists());

      if(destination.exists()){
        boolean deleted = destination.delete();
        if(!deleted)
          throw new Exception("Could not delete file at " + destination);
      }        
      FileUtil.move(uploadedFile, new File(wellnessDir + File.separator + fileName + ".pdf"));
      writeResponse();
    } catch (Exception e) {
      System.out.println("Error handling upload request.");
      e.printStackTrace();
    }
  }

modifica: per aggiungere, getHome () e "casa" non sono realmente nel codice, che è solo per proteggere la mia strada di casa

È stato utile?

Soluzione

Dopo molte prove e aggravamento, infine provato su una macchina diversa, stesso codice, funzionava. Ha qualcosa a che fare con me il trasferimento di domini sulla mia macchina di lavoro e scherzi con i permessi.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top