[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Self creating/maintaining download page

Posted on 2005-04-06
11
Medium Priority
?
174 Views
Last Modified: 2010-04-01
Hi folks

Is there a way in JSP/Servlets/Java to create a webpage that will display a list of files with file sizes from a specified folder so that when someone goes to the page they can click and download/view the file in a new window?  The page needs to be dynamic so that when new files are added to the page the list will refresh whenever someone browses to it.  Ideally the name displayed for the file should be truncated to remove the file extension e.g. .PDF  Also if its configurable to traverse folders and sort the display it would be handy.  Are there any applets/solutions for this type of scenario?

I am no JSP/Java programmer so would appreciate some help!!


Thanks

Chris
0
Comment
Question by:Chris-Moore
  • 6
  • 5
11 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13716865
You can adapt this:

<%@ page import="java.io.*, java.util.*" %>
<html>
<head><title>list files and directories</title>
</head>
<body>
<%
      String currentLocation = getServletContext().getRealPath("") ;
      String[] arDirs = new File( currentLocation ).list();
      Arrays.sort(arDirs) ;
      for( int ii=0 ; ii<arDirs.length ; ii++){
            File file = new File(currentLocation + arDirs[ii]) ;
            String type = file.isFile() ? " &lt;file&gt;" : "  &lt;dir&gt;";
            out.println( "<a href='"+arDirs[ii]+"'>"+ arDirs[ii] + type +"</a><br>") ;
            
      }
%>
</body>
</html>
0
 

Author Comment

by:Chris-Moore
ID: 13717085
Thanks!

This kinda works, has the following issues:

It listed all files from the root folder and all the website so I changed the getRealPath to point to the folder I needed i.e. "/downloads/test"
Then I got a list of the files in that folder but everything was shown as a <dir> even the files
On trying to navigate through folders the path was not updating so if I clicked on a file say /downloads/test/folder/chris.pdf the link pointed to /downloads/chris.pdf

The JSP file is in the /downloads folder so I guess it is using that as the default path rather than the actual navigated path

Any suggestions?

Thanks again

0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 2000 total points
ID: 13717212
I used the root path as that is the version I sometimes use for webapp admin. That is where the code is from!

This version truncates the filename and corrects the file/dir selection (it was due to missing out the file separator "\\" in this case as it's java) and puts the full name in the href attribute

---------
<%@ page import="java.io.*, java.util.*" %>
<html>
<head><title>list files and directories</title>
</head>
<body>
<%
      String currentLocation = "C:\\programs\\tomcat4_1"; //fullname required
      String[] arDirs = new File( currentLocation ).list();
      Arrays.sort(arDirs) ;
      for( int ii=0 ; ii<arDirs.length ; ii++){
            File file = new File(currentLocation + "\\" +arDirs[ii]) ;
            String type = file.isFile() ? " &lt;file&gt;" : "  &lt;dir&gt;"; //file or directory
            out.println( "<a href='"+ currentLocation + "\\" + arDirs[ii]+"'target='new'>"+ arDirs[ii].split("\\.")[0] + type +"</a> " + ""+ "<br>") ;
      }
%>
</body>
</html>
-----------
outputs this for me as this is where my tomcat install is

LICENSE <file>
bin <dir>
common <dir>
conf <dir>
jetspeed-database <dir>
logs <dir>
server <dir>
shared <dir>
temp <dir>
tomcat <file>
uninst-tomcat4 <file>
webapps <dir>
work <dir>



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Chris-Moore
ID: 13717734
When I do this I get a file: URL that points directly to the area (it has the correct file path) but as its on our webserver people cannot and I do not want them to access in this manner, the path somehow needs to be relative from the specified location rather than absolute with a drive path displayed in the URL - additional security risks exposing full paths, etc.

Is there an applet that can read all the folders under a specfied folder and create like a Windows Explorer type traversal system for viewing files and folders?

Again thanks for your help so far!
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13717914
What do you need this for?

All you need to do is create a FileInputStream to read your file and then pass it on to them. This means that you can govern the accessablilty by stopping them from accessing vertain areas. I would suggest a Regex along the lines of:

String bannedAreaRegex = "WEB-INF" ; //one example but you can extend this
boolean bBanned = location.matches(  bannedAreaRegex ) ;
if (bBanned){
....if they're here, they're trying to go somewhere banned
}

You can use relative paths by also using the line for the webapp root.

String currentLocation = getServletContext().getRealPath(".") ;

This means that you can use a relative path knowing where you are starting from.
0
 

Author Comment

by:Chris-Moore
ID: 13718053
I need this for a dynamic updating navigatable download page on my corporate website

Basically I want to be able to have my users upload files and folders to a specific area and then when others go to the downloads.jsp page they will see the files and be able to open them and if there are folders/subfolders be able to navigate through those and then open files within those levels.  Your solution so far is great for files in one folder - I managed to get the download path set relative so thats covered now but its not possible to navigate further folders to display files at a lower level and then view those.  I thought thats why a servlet/control or something might be needed for a tree type navigation?
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13718103
Well in that case you can build a javascript object that represents the directory structure and hide/display the results depending in the users clicks to create your tree structure. That would be the nicest way for your HTML.

The next thing you need to do is to recurse through your directory structure using this method. All you do is find out whether the File object is a directory. If it is, apply the same logic to it as to it's parent directory until you have recursed through all the  directories.

Again, access can be limited by you.
0
 

Author Comment

by:Chris-Moore
ID: 13718142
.....and thats where you lose me as I am no programmer :-)  thats why I hoped there would be a plug-in kind of solution!
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13718348
Unfortunately not. If you need someone to do the coding for you over and above what I have done then you can go here http://www.rentacoder.com/RentACoder/default.asp. I don't mind pointing you in the right direction but it's unfair on my paying clients for me to do it for free, I hope you understand. As it's a commercial job, you should have no trouble as it shouln't cost that much.

Even if you did get an applet/plug-in, you would still need to set it up which would involve some expertise anyway :-/

0
 

Author Comment

by:Chris-Moore
ID: 13718369
I understand perfectly, will award you for the code you provided as I can use this as a simple solution until I get the more involved solution sorted out.

Thanks for your time and efforts, I'll check out rentacoder too!!

0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13721473
Thanks and good luck.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
LinkedIn blogging is great for networking, building up an audience, and expanding your influence as well. However, if you want to achieve these results, you need to work really hard to make your post worth liking and sharing. Here are 4 tips that ca…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question