0

I have created spring boot web application where I am listing all files from a folder and its nested folders and showing it in a jsp page. The code snippet from my jsp is as shown below.

<div>
    <c:if test="${not empty lists}">
        <ul>
            <c:forEach var="listValue" items="${lists}">
                <li>

                    <a
                    href='${pageContext.request.contextPath}/logFilesList/download/<c:out value="${listValue}"  />'>${listValue}</a>
            </c:forEach>
        </ul>

    </c:if>
</div>

Here lists is Arraylist of Strings contains file.getabsolutepath() which is sent from my controller and I am displaying as hyperlink so that when user clicks on it, the file should get downloaded.

My controller method responsible for downloading the files is as follows

@RequestMapping("/logFilesList/download/{fileName:.+}")
    public void downloadLogFiles(HttpServletRequest request, HttpServletResponse response,
            @PathVariable("fileName") String fileName) throws IOException {
// Logic to download the file
}

If i send file.getName() i.e. file name then when user clicks on the link, it reaching above controller method and i am able to download it. But if i send file.getabsolutepath(), then it is not even reaching to above controller method.

I am using file.getabsolutepath() to list all files from a folder and its nested folder.

Please suggest how to send file.getabsolutepath() to controller method as a path parameter.

I already tried suggestions mentioned in below stack overflow link Send file path as @PathVariable in Spring MVC but it does not helped me.

Update

If I replace the backward slash with other character then it works. But I am not supposed to replace the character in the file path. So I tried to send encoded URL instead of replacing with other character.

My Url is appearing as follows http://localhost:8080/download?fileName=C%3A%2FElasticDemoLogs%2Ftest-configuration-jpa.log

But even in this case also, it is not reaching my controller.

Any suggestions are appreciated. Thank you

Update-2

Able to resolve the issue.
Here is what I have done - Using Javascript sent file path to a request param and
- defined controller method as follows

@GetMapping(produces = MediaType.TEXT_PLAIN_VALUE)
    public @ResponseBody void downloadLogFiles(@RequestParam("path") String path, HttpServletResponse response)
            throws IOException { }

Hope it helps!

  • The forward slashes in the file path will bypass your request mapping. You would need to escape the slashes or change your approach. – Compass Oct 15 '18 at 14:33
  • Hi, Thank you. what would be the best approach? I need to download files from folder and its nested folder as well. – kotresh t m Oct 15 '18 at 15:56
  • In Spring Boot, you can expose directories as static. https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot This is similar to how you expose a directory as static in tomcat. – Compass Oct 15 '18 at 15:59
  • Hi, If I understand correctly, by exposing file location, i can display one static resource, is it correct? In my case , i need to list/ show all log files present in particular location and once user clicks on it, it should get downloaded. can this be done? correct me if i am wrong. – kotresh t m Oct 15 '18 at 16:25
  • You can make entire folder structures static resources. – Compass Oct 15 '18 at 16:51
  • Thank you again for replay, I have added below method to spring boot application main class which implements WebMvcConfigurer @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/test/**") .addResourceLocations( "file:///C:/Kotresh/work/Q-MATE/elastic-demo-logs/") .setCachePeriod(0); } . But when i try to access http://localhost:8080/test/ it gives me 404 error. If i specify file name present under above folder then it display the file. I will not download file. – kotresh t m Oct 15 '18 at 17:26

0 Answers0