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);
}
}
}
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);
}
}
}
> 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.
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