Показать файл на основе выбранного пользователем индекса - PullRequest
1 голос
/ 13 мая 2019

Я занимаюсь разработкой веб-страницы с использованием jsp. Я отображаю список файлов с локального диска, и я хочу открыть правильный файл при нажатии. Это то, что я сделал до сих пор.

<select id="folder" name="folder">
                <option>- SELECT -</option>
                <%
                for (int i=0; i<folder.length; i++) { %>
                    <option value="<%= folder[i] %>"><%= folder[i] %></option>
                    <% }
                %>
            </select>

Это отобразит список папок в выпадающем списке. Когда пользователь выбирает папку. Содержимое папки будет отображаться в виде таблицы.

    //open file in folder B
            if (s!= null && s.equals(folder[1])) {
                String subfolder2 = folder[1];
                String folderfilelist2 = dir + "\\" + subfolder2 + "\\" + "pdf";
                File completefolder2 = new File(folderfilelist2);
                completefolder2.getParentFile().mkdirs();
                File[] file_array2 = completefolder2.listFiles();
                        out.print("<table width=1200><tr>");
                        out.print("<p><th width=30><font size=\"6\">File under Company B</th></p>");
                        out.print("</tr></table>");
                        //create table 
                        out.print("<table width=1200><tr>");
                             out.print("<th width=50><b>No</b></th>");
                             out.print("<th width=400><b>File Name</b></th>");
                             out.print("<th width=200><b>Date Converted</b></th>");
                             out.print("<th width=100><b>Mark File</b></th>");
                             out.print("<th width=100><b>Download</b></th>");
                             out.print("<tr><th colspan=5><hr></th></tr>");
                        out.print("</tr></table>");
                        //list file
                        for (int i = 0; i < file_array2.length; i++) {
                             out.print("<table width=1200><tr>");
                                  out.print("<th width=50><font size=\"4\">" + (i +1 ) + "</font></th>");
                                  out.print("<th width=400><font size=\"4\"><a HREF='openfile'"+ file_array2[i].getName() + "\">" + file_array2[i].getName() + "</a></th>");
                                  out.print("<th width=200><font size=\"4\">" + df.format(new Date( file_array2[i].lastModified())) + "</font></th>");
                                  out.print("<th width=100> <input type='checkbox' name='selectedfile' value="+ file_array2[i].getName() +"\"></td>");
                                  out.print("<th width=100><a href='downloadfile'>Download</a></td>");
                             out.print("</tr></table>");
                        }

Нажмите на файл, он вызовет сервлет для отображения файла.

    out.print("<th width=400><font size=\"4\"><a HREF='openfile'"+ file_array2[i].getName() + "\">" + file_array2[i].getName() + "</a></th>");

openfile вызовет сервлет.

Код сервлета:

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
        response.setContentType("APPLICATION/PDF");
        response.setHeader("Content-Disposition", "inline; filename=\""
                            + file_array1[1].getName() + "\"");
    FileInputStream fileInputStream = new FileInputStream(completefolder2 + "\\" + file_array2[1].getName());

    int q;
    while( (q = fileInputStream.read()) != -1 ) {
        out.write(q);
    }
    fileInputStream.close();
    out.close();
    }

Проблема в том, что в сервлете в может быть указан только один файл для отображения на веб-странице.

Как настроить сервлет на открытие выбранного файла из списка?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...