我们正在开发一个文件共享站点,其中包含使用servlet 2.5,jsp和jdbc的用户和管理员的概念。我们已经通过servlet实现了文件上传/下载部分。用户应该在网站上看到他上传的文件和文件夹。但我们仍然坚持通过servlet实现文件夹级别视图。我们在pl / sql中维护了一个add_file db表,并检索了仅由特定用户上传的文件详细信息。但是,我们还必须显示文件夹,我们无法显示。 我遇到了一个简单的改变apache tomcat 6.0的web.xml的解决方案,但是它会让每个用户都能看到每个文件夹和文件里面,它不依赖于数据库。但我们必须仅根据数据库显示这些内容。有没有办法实现这个目标?
UploadedFileView.jsp
-----------代码在这里
<c:forEach items="${files}" var="file">``
<c:url value="/DownloadServlet" var="url">
<c:param name="name" value="${file.name }" />
</c:url>
<a href="<c:out value="${url}"/>"><c:out value="${file.name}" />
</a>
<br>
</c:forEach>
DownloadServlet.java
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
String filename = null;
filename = request.getParameter("name");
PrintWriter out = response.getWriter();
String filepath = null;
if (session.getAttribute("deletedfiles") != null)
{
filepath = DeleteLocationBO.DELETE_DIRECTORY;
}
else
{
filepath = UploadLocationBO.UPLOAD_DIRECTORY;
}
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
FileInputStream fileInputStream = new FileInputStream(filepath
+ filename);
int i;
while ((i = fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
}
错误Stacktrace :(测试是foldername)
D:\ Users \ projectname \ uploads \ test(访问被拒绝:Http状态500)
exception
SEVERE: Servlet.service() for servlet DownloadServlet threw exception
java.io.FileNotFoundException:D:\Users\projectname\uploads\test (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.servlets.DownloadServlet.doGet(DownloadServlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
这是非常简单的算法,但这里有一个让你入门的例子:
public void displayFileTree(File rootDirectory) {
displayFileTree(rootDirectory, "");
}
private void displayFileTree(File rootDirectory, String indent) {
for (File file : rootDirectory.listFiles()) {
System.out.println(indent + file.getName());
if (file.isDirectory()) {
displayFileTree(file, indent + " ");
}
}
}
从简单的控制台程序运行此代码,然后根据您的具体Web问题进行调整。
如果您想要一个可以在JSP中使用的Java bean,它将只具有与文件相同的结构:
public class Node
private boolean directory;
private String name;
private List<Node> children;
// getters omitted for brevity
}