admin管理员组

文章数量:1279241

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> 
&nbsp;&nbsp;&nbsp;&nbsp;
(<%= 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??

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> 
&nbsp;&nbsp;&nbsp;&nbsp;
(<%= 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??

Share Improve this question asked Dec 20, 2012 at 4:25 softsoft 711 gold badge1 silver badge3 bronze badges 2
  • 3 file://d:/filedir/etc/etc, but it will stop working in the moment that your server and your client are not on the same machine. – Edwin Dalorzo Commented Dec 20, 2012 at 4:28
  • Does this answer your question? Opening files in the browser with Java Web App – SilverNak Commented Dec 19, 2019 at 7:51
Add a ment  | 

4 Answers 4

Reset to default 2

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./projects/filemanager/

Here is the link to the source: https://github./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:

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

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>
                        &nbsp;&nbsp;&nbsp;&nbsp; 
                        (<%=Long.toString(fileObjects[i].length())%>    bytes long) 
                <%}
            }%>
    </UL>
</body>
</html>
<%@ 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");
}
%>

本文标签: javahow to open a file from a jsp pageStack Overflow