How to Create Folder - Servlet

Posted on 2005-04-13
Last Modified: 2013-11-24

I'm trying to upload file and its working. Problem is its upload under context root direcotry. Bellow is the code line...
File cfile=new File(item.getName());
File tosave=new File(getServletContext().getRealPath("/"),cfile.getName());

I need to know if the directory path not  present, how to create directory folder ??
eg: if I say getRealPath("/xyz") it should create folder xyz and follow the operation of serlvet...

Question by:princehyderabad
    LVL 92

    Accepted Solution

    File path = new File(getServletContext().getRealPath("/xyz"));
    if (!path.exists())
    LVL 16

    Expert Comment

    The mkdirs(); method will also return a boolean.  This will allow you to make sure they actually got created.

    if (!path.mkdirs()){
      System.out.println("Error: Failed to create directory structure");

    Author Comment

    one more thing can anyone plz tell me:

    In a form I'm passing this hidden value, how should I extract in Servlet.

    <form method="post" action="upload" enctype="multipart/form-data">
    <input type="hidden" name="dir" value="<% out.println(request.getAttribute("rs"));%>">

    I tried
    String a = request.getParameter("dir");
    but didnt worked ??
    LVL 92

    Expert Comment

    That should work, not sure why it isn't.

    Author Comment

    WEll the code mkdirs(); is working on windows machine. I tried on linux machine its not. Any idea whats command on Linux machine to make directory..

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Understanding @ResponseBody annotation 5 36
    scoresSpecial  challenge 13 35
    bigHeights  challenge 13 43
    changePi Challenge 15 57
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now