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!