javascript 如何从jsp页面打开文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13965309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to open a file from a jsp page?
提问by soft
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.
我在某个本地磁盘中保存了一些 pdf 文件.. D:/filesDir/ ,我想将该文件夹中的所有文件显示到我的 jsp 页面中,并单击特定的 pdf 文件,它应该打开位于 D 中的 pdf 文件:/filesDir/ 用户点击了.. 目前我的代码如下。
<%
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...
从上面的代码中,我可以将 filesDir 文件夹中的所有 pdf 文件显示到我的 jsp 页面中,但是单击特定的 pdf 文件(例如 abc.pdf),而不是将控件转到 D:/filesDir/abc .pdf,控件将转到 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??
如何消除特定于应用程序的路径(即 locahlhost:8080/myapp/)并从链接中打开我的 pdf 文件?
回答by Raab
to solve your problem Re-Write the link line as
解决您的问题将链接行重新编写为
<A HREF="<%="file://D:/filesDir/"+fileNames[i] %>"><%= fileNames[i]%></A>
BUTif 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
但是,如果您确实希望在服务器本身以外的其他系统上访问文件,则应将文件移动到 Web 目录中,然后使用相对路径进行访问
回答by Ulises
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:
除非这是作业或练习,否则我会研究现有的解决方案。我已经使用 CKEditor 的 FileManager 插件作为浏览服务器中文件的独立解决方案,它的工作原理非常棒:
Here is the home page: http://labs.corefive.com/projects/filemanager/
这是主页:http: //labs.corefive.com/projects/filemanager/
Here is the link to the source: https://github.com/simogeo/Filemanager/tree/master/connectors/jsp
这是源的链接: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:
适应现有应用程序非常简单。只需下载,调整 filemanager.config.js 文件,就是这样:
回答by Rais Alam
Try below code. It works fine with chrome and IE.
试试下面的代码。它适用于 chrome 和 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>
回答by Sagar Maiyad
<%@ 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");
}
%>