Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:38:22+00:00 2026-05-27T10:38:22+00:00

My upload servlet keeps throwing me an exception saying that the file that I’m

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:38:23+00:00Added an answer on May 27, 2026 at 10:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a servlet, that can upload a file from client to
I have a file upload form that is being posted back to a servlet
I'm trying to a upload file (or multiple files) to my servlet, which is
I've written a Servlet that handles file uploads using the Apache commons file upload
i am trying to upload an image from a jsp page using servlet. but
How do I upload a file from a Java servlet to a location on
I am trying to upload a file or stream of data to our web
I'm running a servlet in Tomcat 6.0.26. The servlet accepts file upload from the
What is the best way to upload a CSV file through a Java servlet
I'm writing a servlet that receives an xml file from the client and works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.