how to list the uploaded files in jsp

hello every body . I wanted to request to plese let me know the method as to how get the uploaded files on the server to a jsp page . i am able to upload the files but not able to get those files listed on the web page . please help me ...........
[261 byte] By [joshiashutosha] at [2007-11-27 4:57:56]
# 1
Which part of coding did you fail? Please ask specific questions.It's actually fairly straightforward: grab and collect the filenames and show them.
BalusCa at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Check with following code

private static void dirlist(String fname){

File dir = new File(fname);

String[] chld = dir.list();

if(chld == null){

System.out.println("Specified directory does not exist or is not a directory.");

System.exit(0);

}else{

for(int i = 0; i < chld.length; i++){

String fileName = chld;

System.out.println(fileName);

}

}

}

JSP_Todlera at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
i was able to upload the file is uploaded ,. what i want is to list the uploaded files and the other subsequent uploads ,.but i at the same time file list to be updated every time thenew file is uploaded.
joshiashutosha at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
So you just want to retrieve a directory listing of the upload directory?
BalusCa at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
yaa exactly that , isthis directory updates itself with every upload made ?
joshiashutosha at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
After finishing of every upload just invoke the dirlist() code snippet given here above.You might find the java.io.File API useful: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html Checkout the list() method.
BalusCa at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

i tied the code but is flagging the message that the directory does not exist or invalid if i pass the file name or the path of the file . the code is as follows try

{

// the directory where the uploaded file will be saved.

String savePath ="//home//trainee//ashutosh//project//uploadedfiles//";

String filename = "";

ServletInputStream in = request.getInputStream();

byte[] line = new byte[128];

int i = in.readLine(line, 0, 128);

int boundaryLength = i - 2;

String boundary = new String(line, 0, boundaryLength);

//-2 discards the newline character

while (i != -1) {

String newLine = new String(line, 0, i);

if (newLine.startsWith("Content-Disposition: form-data; name=\""))

{

String s = new String(line, 0, i-2);

int pos = s.indexOf("filename=\"");

if (pos != -1)

{

String filepath = s.substring(pos+10, s.length()-1);

pos = filepath.lastIndexOf("\\");

if (pos != -1)

filename = filepath.substring(pos + 1);

else

filename = filepath;

}

//this is the file content

i = in.readLine(line, 0, 128);

i = in.readLine(line, 0, 128);

// blank line

i = in.readLine(line, 0, 128);

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

newLine = new String(line, 0, i);

while (i != -1 && !newLine.startsWith(boundary)) {

buffer.write(line, 0, i);

i = in.readLine(line, 0, 128);

newLine = new String(line, 0, i);

}

try {

// save the uploaded file

RandomAccessFile f = new RandomAccessFile(

savePath + filename, "rw");

byte[] bytes = buffer.toByteArray();

f.write(bytes, 0, bytes.length - 2);

f.close();

}

catch (Exception e) {}

}

i = in.readLine(line, 0, 128);

} // end while

out.println("

");

out.println("The file named"+ " " +filename+" "+ "is successfully uploaded");

File dir = new File(:);

String[] chld = dir.list();

if(chld == null)

{

out.println("Specified directory does not exist or is not a directory.");

}

else

{

for(int a = 0; a < chld.length; i++)

{

String fileName[] = chld;

out.println(fileName);

}

}

}

joshiashutosha at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

> File dir = new File(:);

You need to specify the savePath in there.

By the way, use single forwardslash "/" as path separator all the time instead of double forward slashes "//" or double (escaped) backward slashes "\\". The single forward slash works in all operating systems (Windows, UNIX), the double forward slash is meaningless and the backward slash works in Windows only.

BalusCa at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
There's open source example java code which lists uploads on web page but it's not jsp: http://www.wicket-library.com/wicket-examples/upload
Niklasa at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

i tried this also and it didn't wok instead my page got filled with these lines

The file named CSAJSP-Chapter12.pdf is successfully uploaded [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0 [Ljava.lang.String;@1ab11b0

. I am not able to make out any thing . plese suggest me some solution

joshiashutosha at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Solutions are already given. You're likely doing something wrong or misunderstanding suggesions and/or ignoring the API documentation.
BalusCa at 2007-7-12 10:13:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...