Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 15th, 2007, 03:12 AM
Registered User
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default change the directory of uploaded files : jsp code

Hi all

i have jsp upload code and its working, when i upload a file it saves them in Windows\system32 but i want to create a new folder in system32 and save the uploaded files there

so could anyone help me in that

this is the form
<%@ page language="java" %>


<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>


<FORM ENCTYPE="multipart/form-data" ACTION="single_upload_page.jsp" METHOD="POST">


 <center><table border="2" >

 <tr><center><td colspan="2"><p align="center"><B>PROGRAM FOR UPLOADING THE FILE</B><center> </td></tr>

 <td><b>Choose the file To Upload:</b></td>

  <td><INPUT NAME="F1" TYPE="file"></td>

<tr><td colspan="2"><p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td>


and this is the uploading code
<%@ page import="java.io.*" %>
    //to get the content type information from JSP Request Header

    String contentType = request.getContentType();

    //here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0

    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))

         DataInputStream in = new DataInputStream(request.getInputStream());

    //we are taking the length of Content type data

        int formDataLength = request.getContentLength();

        byte dataBytes[] = new byte[formDataLength];

        int byteRead = 0;

        int totalBytesRead = 0;

        //this loop converting the uploaded file into byte code

        while (totalBytesRead < formDataLength)


        byteRead = in.read(dataBytes, totalBytesRead, formDataLength);

            totalBytesRead += byteRead;


        String file = new String(dataBytes);

        //for saving the file name

        String 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;

        //extracting the index of file

        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;

        // creating a new file with the same name and writing the content in new file

        FileOutputStream fileOut = new FileOutputStream(saveFile);

        fileOut.write(dataBytes, startPos, (endPos - startPos));




        <Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>

        <% out.println(saveFile); %></td></tr></table>


Thanks in advance:D

Old May 17th, 2007, 06:53 AM
Friend of Wrox
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post

You can do that by prefixing the folder name to the file name when you create FileOutputStream object.

BUT WHAT I SUGGEST IS not to store them in system32 directory as it is windows operating systems directory. Instead use a property file which will have a property with the value of the directory path where you want to store the uploaded files

some thing like that, use this value while creating FileOutputStream object.

Hope its usefull.

Old May 19th, 2007, 06:06 AM
Registered User
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks alot Rakesh

i relly appreciate it :-)

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to mail uploaded files as an attachment jijish Classic ASP Professional 0 April 29th, 2008 03:35 PM
How to get Path of uploaded file in JSP murulikblr JSP Basics 0 April 17th, 2007 02:05 AM
Renaming uploaded multiple files sequentially flyguylol Beginning PHP 4 January 15th, 2006 10:33 PM
How do i dl files that i have uploaded Beginner123 VB How-To 0 March 2nd, 2005 10:42 PM
PHP uploaded to root directory arnec BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 August 15th, 2003 03:24 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.