0
Find.java
import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class Find {
  public  File folder = new File("");
  static String temp = "";

  private String text;

  public void setText(String text) {
                  this.text = text;
  }

  public String getText() {
                  return text;
  }

  public List<String> getList() {
                  return getFiles(new File(text));
  }

  private List<String> getFiles(File f) {
                  List<String> result = new ArrayList<String>();

                  if (f.isDirectory()) {
                                 File[] list = f.listFiles();
                                 if (list == null) {
                                                 return result;
                                 }
                                 for (File d : f.listFiles()) {
                                                 if (d.isDirectory()) {
                                                                 result.addAll(getFiles(d));



                                                 } else {
                                                                 result.add(d.getName());
                                                                 System.out.println(d.getPath());


                                                 }
                                 }
                  }
                  return result;
  }

secondpage.jsp

!DOCTYPE HTML>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="ru.sbrf.asfs.stub.stubsUtils.Find"%>
<%@page import="java.util.List"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<% Find find = new Find();
String name = request.getParameter("text");
System.out.print(name);
       find.setText(name);
       List<String> list = find.getList();

%>


<html>
<head>
<title>secondpage</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
       <table border="1">

                           <th value=>
                                  <c:forEach var="l" items='<%= list %>'>
                                       <c:out value="${l}"/> <br>
                                  </c:forEach>
                           </th>
                           <th Путь>

                           </th>

       </table>
       </body>

1 ответ 1

2

Грешно в нашем веке скриптлеты использовать. Лучше так:

Lister.java

package com.example;

import java.io.IOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

public class Lister {
    private String path;

    public Lister() {}

    public void setPath(String path) throws IOException {
        if (!new File(path).isDirectory())
            throw IOException("Not a directory!");
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public List<File> getFiles() throws IOException {
        return Files.walk(Paths.get(path))
                 .filter(Files::isRegularFile)
                 .collect(Collectors.toList());
    }            
}

secondpage.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!doctype html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <jsp:useBean id="lister" class="com.example.Lister">
            <jsp:setProperty property="path" value="${param.text}"/>
        </jsp:useBean>
        <table>
            <c:forEach var="f" items="${lister.files}">
            <tr>
                <td>
                    <c:out value="${f.absolutePath}" />
                </td>
                <td>
                    <c:out value="${f.length()}" />
                </td>
                <td>
                    <c:out value="${f.lastModified()}" />
                </td>
            </tr>
            </c:forEach>
        </table>
    </body>
</html>

Можно Lister ещё чуточку усложнить, чтобы вместо списка объектов File он возвращал список обёрток, красиво показывающих размер и дату модификации.

1
  • Спасибо, все отлично, работает. Только к сожалению там, где нужно стоит jdk 1.7 (Вам покажется странным, но я это изменить не могу, так как от меня это не зависит), поэтому метод walk отсутствует. 11 сен 2017 в 15:23

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.