I have a jsp page which contains the code which prints all files in a given directory and their file paths. The code is
if (dir.isDirectory())
{
File[] dirs = dir.listFiles();
for (File f : dirs)
{
if (f.isDirectory() && !f.isHidden())
{
File files[] = f.listFiles();
for (File d : files)
{
if (d.isFile() && !d.isHidden())
{
System.out.println(d.getName()+
d.getParent() + (d.length()/1024));
}
}
}
if (f.isFile() && !f.isHidden())
{
System.out.println(f.getName()+
f.getParent() + (f.length()/1024));
}
}
}
The problem is that it prints the complete file path, which when accessed from tomcat is invalid. For example, the code spits out the following path:
/usr/local/tomcat/sites/web_tech/images/scores/blah.jpg
and I want it to only print the path up to /images ie
/images/scores/blah.jpg
I know I could just mess around with an actual string, ie splitting it or string matching, but is there an easier way to do it?
Thanks
You’ll need to substring the root path away.
By the way, those
System.out.println()lines actually won’t print to the response. They prints to the stdout which may be the IDE console or the server’s logfile. Further, this kind of logic doesn’t belong in a JSP file. Do it in a real Java class and forward to JSP for display.