I would like to create a folder that has been specified by a user i a JSP.

for example, in the jsp page below called index.jsp the user is asked to create a folder..

<form action="CreateFolder.jsp" method="post">

<h3> Create New Folder <h3>
<input name="createfolder" type="text">
<input type="submit" value"Create Folder">

</form>

from there how's the text input referenced or used to create the folder?

<%@page import="java.io.File"%>
<%
    File f = new File(); /* how exactly, does the constructor here get the value from the input
    in index.jsp to create the folder? */

    if(!f.exists()){
        f.mkdirs();
    }
%>

Recommended Answers

All 4 Replies

You need to read request parameter value:

String createfolder = request.getParameter("createfolder");

Then, construct the path (the location where you wish to create a folder):

//Create a new folder at app context

String realPath = application.getRealPath(createFolder);

thanks..
its working..

<%@page import="java.io.File"%>

<form action="CreateFolder.jsp" method="post">
    <h2>
        Create New Folder
    </h2>
    <input name="createfolder" type="text">
    <input type="submit" value="Create Folder">
</form>

<%
    String createfolder = request.getParameter("createfolder");
    String path = "C:/temp/data/" + createfolder; 
    File f = new File(path);

    if(!f.exists()){
        if(f.mkdirs()){%>
            <h2>created file <%=createfolder%></h2>
   <%}
    }
   %>

i have another question..
I would like to create a folder and upload a file at the same time..
the code below..allows me to upload..but the folder is not being created..
a null folder is created instead and the file saved there..

<form enctype="multipart/form-data" action="WriteToFolder.jsp" method="post">
    <h2>
        Create New Folder
    </h2>
    <p>Name of the folder to be Created: 
    <input name="folderName" type="text">
    </p>

    <h2>
        <B>UPLOAD THE FILE</B><br>
    </h2>

    <b>Choose the file To Upload:</b><br>
    <input name="file" type="file"></td>
    </p>

    <input type="submit" value="Create Folder">

    </form>


    <%
    String createfolder = request.getParameter("folderName");

    String path = "C:/temp/data/" + createfolder;

    File f = new File(path);

    if (!f.exists()) {
        if (f.mkdirs()) 
    }

%>


<%

    String saveFile = "";
    String contentType = request.getContentType();
    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {

        DataInputStream in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();
        byte dataBytes[] = new byte[formDataLength];
        int byteRead = 0;
        int totalBytesRead = 0;
        while (totalBytesRead < formDataLength) {
            byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
            totalBytesRead += byteRead;
        }

        String file = new String(dataBytes);
        saveFile = file.substring(file.indexOf("filename=\"") + 10);
        saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
        saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
        int lastIndex = contentType.lastIndexOf("=");
        String boundary = contentType.substring(lastIndex + 1, contentType.length());
        int pos;
        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
        saveFile = path+ "/" + saveFile;
        f = new File(saveFile);
        FileOutputStream fileOut = new FileOutputStream(f);
        fileOut.write(dataBytes, startPos, (endPos - startPos));
        fileOut.flush();
        fileOut.close();
%>
<b>You have successfully upload the file by the name of:</b>
<%
        out.println(saveFile);
    }
%>
<a href="ViewFiles.jsp">View Uploaded Files</a>

i wanna create folder or file in my jsp i.e., on jsp pase show this folder not in my system.

you should start a new thread for a new question.
you should also realize that 'a folder' is a part of the file system, not of the layout of a webpage. you'll need to be more specific about what exactly you're trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.