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

  • SEARCH
  • Home
  • 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 7499011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:40:09+00:00 2026-05-29T19:40:09+00:00

I have quite a simple question really. I wrote a servlet for suppliers to

  • 0

I have quite a simple question really.
I wrote a servlet for suppliers to upload XML-files to.
These files get written to a location on the server.
All the files get renamed with a timestamp.

Is there a risk of concurrency problems with the code below?
I ask because we receive files from a supplier, that look like
they have content from 2 different XML-files

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}        

public String getServletInfo() {
    return "Short description";
}// </editor-fold>

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    File dirToUse;
    boolean mountExists = this.getDirmount().exists();
    if (!mountExists) {
        this.log("MOUNT " + this.getDirmount() + " does not exist!");
        dirToUse = this.getDiras400();

    } else {
        dirToUse = this.getDirmount();
    }

    boolean useSimpleRead = true;
    if (request.getMethod().equalsIgnoreCase("POST")) {
        useSimpleRead = !ServletFileUpload.isMultipartContent(request);
    }

    if (useSimpleRead) {
        this.log("Handle simple request.");
        handleSimpleRequest(request, response, dirToUse);

    } else {
        this.log("Handle Multpart Post request.");
        handleMultipart(request, response, dirToUse);
    }
}   

protected void handleMultipart(HttpServletRequest request,
        HttpServletResponse response, File dir) throws IOException,
        ServletException {
    try {
        FileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        List<FileItem> items = upload.parseRequest(request);

        if (items.isEmpty()) {
            this.log("No content to read in request.");
            throw new IOException("No content to read in request.");
        }

        boolean savedToDisk = true;
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();

            getFilename(request);
            File diskFile = new File(dir, this.getFilename(request));
            item.write(diskFile);

            if (!diskFile.exists()) {
                savedToDisk = false;
            }
        }

        if (!savedToDisk) {
            throw new IOException("Data not saved to disk.");
        }

    } catch (FileUploadException fue) {
        throw new ServletException(fue);

    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}   

protected void handleSimpleRequest(HttpServletRequest request,
        HttpServletResponse response, File dir) throws IOException {
    // READINPUT DATA TO STRINGBUFFER
    InputStream in = request.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    StringBuffer sb = new StringBuffer();
    String line = reader.readLine();
    while (line != null) {
        sb.append(line + "\r\n");
        line = reader.readLine();
    }

    if (sb.length() == 0) {
        this.log("No content to read in request.");
        throw new IOException("No content to read in request.");
    }       

    //Get new Filename
    String newFilename = getFilename(request);
    File diskFile = new File(dir, newFilename);
    saveDataToFile(sb, diskFile);

    if (!diskFile.exists()) {
        throw new IOException("Data not saved to disk.");
    }
}

protected abstract String getFilename(HttpServletRequest request);

protected void saveDataToFile(StringBuffer sb, File diskFile) throws IOException {
    BufferedWriter out = new BufferedWriter(new FileWriter(diskFile));

    out.write(sb.toString());
    out.flush();
    out.close();
}

getFileName implementation:

@Override
protected String getFilename(HttpServletRequest request) {
    Calendar current = new GregorianCalendar(TimeZone.getTimeZone("GMT+1"));
    long currentTimeMillis = current.getTimeInMillis();

            System.out.println(currentTimeMillis);
    return "disp_" + request.getRemoteHost() + "_" + currentTimeMillis + ".xml";
}

Anyway, thanks in advance!

  • 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-29T19:40:12+00:00Added an answer on May 29, 2026 at 7:40 pm

    There would not be synchronization problems but there can be race conditions, for example, two threads might return the same file name using the method getFileName()

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

Sidebar

Related Questions

I am stuck with something quite simple but really annoying: I have an xml
I have really simple and lame question (i hope it's simple). I'm quite new
The question is quite simple : I have a controller plugin for Zend Framework.
I have a written a very simple UDP Server using Netty - it quite
I have quite a simple question I think, but I need a definitive answer.
I think its quite a simple question but not sure. I have a class:
I'm quite new to PHP OOP and I have a question regarding a simple
I have a quite simple issue I think, but no way to figure out
My problem is quite simple: I have a 400MB file filled with 10,000,000 lines
I have a problem that is quite simple to understand, but for me it

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.