想要使用jsp显示文件列表

时间:2020-11-30 11:50:18

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

我是玻璃鱼服务器netbeans的新手。我的HTML代码看起来像,

<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?

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

2 个解决方案

#1


1  

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.

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

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.

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

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[].

在/WEB-INF/uploads.jsp文件中,使用JSTL 迭代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.

请注意,这当然是开放的更多(UI)微调,但这取决于你。我假设您已经了解HTML / JSP / Servlet的基础知识。

#2


-1  

you can do like that

你可以这样做

     <%!
      Object path;
      public void getDirectory(String path, Vector files, Vector folder){
      File directory=new File(path); 
     File []file=directory.listFiles();
        for(int i=0; i<file.length; i++){
       if(file[i].isDirectory()){
        folder.add(file[i].getName());
       }
       else{
        files.add(file[i].getName());
       }
    }
 }
 %>

<table>
<%

    path=session.getAttribute("fileName");
   Vector file=new Vector(), folder=new Vector();
  getDirectory("C:/FileFolderProject/WebContent/"+path,file,folder);
   out.println("<music>");

  for(int a=0; a<file.size(); a++){
    %>
     <tr>
     <td>
      <img src="images/editfileimg.jpg" alt="file">
      </td>
     <td>
     <% 
     out.println("<file>"+file.elementAt(a).toString()+"</file><br/>");
    %>
     </td>
     <tr>
  <%

  }
  out.println("</music>");
  %>
   </table>

#1


1  

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.

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

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.

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

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[].

在/WEB-INF/uploads.jsp文件中,使用JSTL 迭代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.

请注意,这当然是开放的更多(UI)微调,但这取决于你。我假设您已经了解HTML / JSP / Servlet的基础知识。

#2


-1  

you can do like that

你可以这样做

     <%!
      Object path;
      public void getDirectory(String path, Vector files, Vector folder){
      File directory=new File(path); 
     File []file=directory.listFiles();
        for(int i=0; i<file.length; i++){
       if(file[i].isDirectory()){
        folder.add(file[i].getName());
       }
       else{
        files.add(file[i].getName());
       }
    }
 }
 %>

<table>
<%

    path=session.getAttribute("fileName");
   Vector file=new Vector(), folder=new Vector();
  getDirectory("C:/FileFolderProject/WebContent/"+path,file,folder);
   out.println("<music>");

  for(int a=0; a<file.size(); a++){
    %>
     <tr>
     <td>
      <img src="images/editfileimg.jpg" alt="file">
      </td>
     <td>
     <% 
     out.println("<file>"+file.elementAt(a).toString()+"</file><br/>");
    %>
     </td>
     <tr>
  <%

  }
  out.println("</music>");
  %>
   </table>