Listing files from Directory in JSP

hello guys,

today i gonna share some interesting stuff in JSP (Java Server Pages). so this post is regarding display list of all files from particular directory using jsp.

so let’s go for coding.

<%@ page import="java.io.*" %>
<% 
String file = application.getRealPath("/upload");
File f = new File(file);
String [] fileNames = f.list();
File [] fileObjects= f.listFiles();
for (int i = 0; i < fileObjects.length; i++) {
if(!fileObjects[i].isDirectory()){
    String fname = file+fileNames[i];
	out.println(fileNames[i]);
 }
}
%>

you can insert some html code in jsp file to display it in proper format. you can also you
<a HREF=”<%= “upload/”+fileNames[i] %>”><%= fileNames[i] %></a> to put hyperlink on file so user can also view that file.

hope you enjoy this post.! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s