lingar
lingar

Reputation: 603

Can static content on spring-boot-web application be dynamic (refreshed)?

I am still searching around this subject, but I cannot find a simple solution, and I don't sure it doesn't exist.

Part 1

Part 2

I want to access it from the browser, so when I call it will get downloaded. I know that for the static content, all I need to do is to call to the file, from the browser like that:

http://localhost:8080/documents/myfile.xlsx

Example

After I would be able to do it, all I need is to create link to this url from my client app.

The problem - Currently if I call to the file as above, it will download only the file which have been there in the compiling stage, but if I am generating a new files after the app is running the content won't be available.

It seems that the content is (as it's called) "static" and cannot be changed after startup.

So my question is

BTW I found this answer and others which doing configuration methods, or web services, but I don't want all this. And I have tried some of them, but the result is the same.

FYI I don't bundle my client app with the server app, I run them from different hosts

Upvotes: 0

Views: 666

Answers (1)

smac2020
smac2020

Reputation: 10714

The problem is to download the file with the dynamic content from a Spring app.

This can be solved with Spring BOOT. Here is the solution as shown in this illustration - when i click Download report, my app generates a dynamic Excel report and its downloaded to the browser:

enter image description here

From a JS, make a get request to a Spring Controller:

function DownloadReport(e){

     //Post the values to the controller
     window.location="../report" ;
}

Here is the Spring Controller GET Method with /report:

 @RequestMapping(value = ["/report"], method = [RequestMethod.GET])
    @ResponseBody
    fun report(request: HttpServletRequest, response: HttpServletResponse) {

       
        // Call exportExcel to generate an EXCEL doc with data using jxl.Workbook
        val excelData = excel.exportExcel(myList)
        try {

            // Download the report.
            val reportName  = "ExcelReport.xls"
            response.contentType  = "application/vnd.ms-excel"
            response.setHeader("Content-disposition", "attachment; filename=$reportName")
            org.apache.commons.io.IOUtils.copy(excelData, response.outputStream)
            response.flushBuffer()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

This code is implemented in Kotlin - but you can implement it as easily in Java too.

Upvotes: 1

Related Questions