Question

My upload servlet keeps throwing me an exception saying that the file that I'm trying to replace (near the end of my code) could not be deleted at (seemingly) random. I don't know what's causing this since I'm not using any streams and the file isn't open in my browser. Does anyone know what could be causing this? I'm completely clueless on this one as the code seems correct to me. This is the first time I've used DiskFileItem so I'm not sure if there are any nuances to handle there.

Keep in mind that it sometimes works, sometimes doesn't. I'm lost on that.

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

My System outs always say that both file and destination exist. I'm trying to get the upload to overwrite the existing file.

Full code: (& 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();
    }
  }

edit: to add, getHome() and "home" aren't really in the code, that's just to protect my home path

Was it helpful?

Solution

After much testing and aggravation, finally tried it on a different machine, same code, worked great. Has something to do with me transferring domains on my work machine and it messing with permissions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top