想用jsp显示文件列表 [英] Want to display file list using jsp

查看:129
本文介绍了想用jsp显示文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < html> 
< body>
< form id =file_upload_formmethod =postenctype =multipart / form-dataaction =upload.java>
< div style =float:center>
< center>
选择一个文件:
< input type =filename =first/>
< input type =submitname =buttonvalue =upload/>
< / center>
< / div>
< center>
< / br>
< / br>
< iframe id =uploadstyle =background-color:white; width = 90%height = 80%>< / iframe>
< / center>
< / form>
< / body>
< / html>

提交请求时,我想将上传的文件存储在驱动器中(ex.c:/上传)和iframe(id = upload)应显示文件夹(c:/ upload)中的文件。有人知道我在哪里可以找到一些示例代码,显示如何完成这项工作?

处理HTTP multipart / form-data servlet中的请求,请使用 Apache Commons FileUpload 。您应该将上传的文件放在 的FileItem 。它有一个 write()方法。

  String filename = FilenameUtils。的getName(fileItem.getName()); 
fileItem.write(new File(c:/ upload,filename)); //名称必须是唯一的!

有关使用指南和更多代码示例,请检查它们的 $ b




要显示文件夹中的文件列表,您需要使用
java.io.File API,它有一个 listFiles()方法,它返回特定路径中所有文件(路径)的列表。在预处理 servlet 中执行此操作,该请求将请求转发给JSP以显示列表。

  File [] files = new File(c:/ upload)。listFiles(); 
request.setAttribute(files,files);
request.getRequestDispatcher(/ WEB-INF / uploads.jsp)。forward(request,response);

/WEB-INF/uploads.jsp 文件使用 JSTL < c:forEach> 来迭代通过 File []

 < c:forEach items = $ {files}var =file> 
< c:out value =$ {file.name}/> ($ {file.length / 1024} KB)< br />
< / c:forEach>

请注意,这当然可以用于更多(UI)微调,但这取决于您。我假设你已经知道HTML / JSP / Servlet的基础知识。


I am new to netbeans with glassfish server.My html code looks like,

<html>
    <body>
<form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload.java">
    <div style="float:center">
        <center>
            Select a file:
            <input type="file" name="first" />
            <input type="submit" name="button" value="upload" />
        </center>
    </div>
    <center>
        </br>
        </br>
        <iframe id="upload" style="background-color:white;" width=90% height=80%></iframe>
    </center>                
</form>
</body>
</html>

when the request is submitted,i want to store the uploaded file in drive(ex.c:/upload) and the iframe(id=upload) should display the files in folder(c:/upload). Does anyone know where I can find some sample code that shows how this is done?

解决方案

To process HTTP multipart/form-data requests in a servlet, use Apache Commons FileUpload. You should end up with the uploaded file in a FileItem. It has a write() method.

String filename = FilenameUtils.getName(fileItem.getName()); 
fileItem.write(new File("c:/upload", filename)); // Name must be unique!

For the usage guide and more code examples, just check their User Guide.


To display a list of files in the folder, you need to use the java.io.File API, it has a listFiles() method which returns a list of all files (paths) in a certain path. Do it in a preprocessing servlet which forwards the request to a JSP to display the list.

File[] files = new File("c:/upload").listFiles();
request.setAttribute("files", files);
request.getRequestDispatcher("/WEB-INF/uploads.jsp").forward(request, response);

In the /WEB-INF/uploads.jsp file use JSTL <c:forEach> to iterate over the File[].

<c:forEach items="${files}" var="file">
    <c:out value="${file.name}" /> (${file.length / 1024}KB)<br/>
</c:forEach>

Note that this is of course open for more (UI) finetuning, but that's up to you. I assume that you already know the HTML/JSP/Servlet basics.

这篇关于想用jsp显示文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆