Reputation: 71
I have some pdf files saved in some local disk.. D:/filesDir/ , I want to display all the files in that folder into my jsp page & on click of a particular pdf file, it should open the pdf file located in D:/filesDir/ on which the user has clicked.. currently I have my code like below.
<%
String sourceDirectory = "D:\\filesDir\\";
File f = new File(sourceDirectory);
String [] fileNames = f.list();
File [] fileObjects= f.listFiles();
%>
<UL>
<%
for (int i = 0; i < fileObjects.length; i++) {
if(!fileObjects[i].isDirectory()){
%>
<LI>
<A HREF="<%="D:/filesDir/"+fileNames[i] %>"><%= fileNames[i]%></A>
(<%= Long.toString(fileObjects[i].length()) %> bytes long)
<%
}
}
%>
</UL>
From the above code, I can display all my pdf files from the filesDir folder into my jsp page, but on click of a particular pdf file(for ex. abc.pdf), instead of the control going to D:/filesDir/abc.pdf, the control is going to localhost:8080/myapp/D:/filesDir/abc.pdf...
How can I eliminate the application specific path (ie., locahlhost:8080/myapp/) & open my pdf file from the link??
Upvotes: 4
Views: 22513
Reputation: 35572
to solve your problem Re-Write the link line as
<A HREF="<%="file://D:/filesDir/"+fileNames[i] %>"><%= fileNames[i]%></A>
BUT if you really want the files to be accessed on other systems as well other than the server itself, you should move your file into your web directory and then use relative path for access
Upvotes: 2
Reputation: 12733
<%@ page import="java.io.*"%>
<%
FileOutputStream out;
try{
out = new FileOutputStream("C://Hello.txt");
new PrintStream(out).println ("All glitters are not gold");
out.close();
}
catch (IOException e){
out.println ("Unable to write to file");
}
%>
Upvotes: 0
Reputation: 7016
Try below code. It works fine with chrome and IE.
<%@page import="java.io.File"%>
<html>
<body>
<%
String sourceDirectory = "D:\\books";
File f = new File(sourceDirectory);
File[] fileObjects = f.listFiles();
%>
<UL>
<%
for (int i = 0; i < fileObjects.length; i++)
{
if (!fileObjects[i].isDirectory())
{%>
<LI><A HREF="file:\\\<%=fileObjects[i].getAbsolutePath()%>"><%=fileObjects[i].getName()%></A>
(<%=Long.toString(fileObjects[i].length())%> bytes long)
<%}
}%>
</UL>
</body>
</html>
Upvotes: 0
Reputation: 13419
Unless this is homework or an exercise, I would look into an existing solution. I've used the FileManager plugin for CKEditor as a stand-alone solution to browse files in the server and it works like a charm:
Here is the home page: http://labs.corefive.com/projects/filemanager/
Here is the link to the source: https://github.com/simogeo/Filemanager/tree/master/connectors/jsp
It's very straight forward to adapt to existing apps. Just download, tweak the filemanager.config.js file and that's it:
Upvotes: 2