0

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

  • I have a service on my application that's generating an excel doc, by the dynamic DB data.

     public static void 
     notiSubscribersToExcel(List<NotificationsSubscriber> 
     data) {
     //generating the file dynamically from DB's data
    
     String prefix = "./src/main/resources/static";
     String directoryName = prefix + "/documents/";
     String fileName = directoryName + "subscribers_list.xlsx";
    
     File directory = new File(directoryName);
     if (! directory.exists()){
         directory.mkdir();
         // If you require it to make the entire directory path including parents,
         // use directory.mkdirs(); here instead.
     }
    
     try (OutputStream fileOut = new FileOutputStream(fileName)) {
    
         wb.write(fileOut);
         fileOut.close();
         wb.close();
    
     } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
    

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

  • is there is a way to define a folder on the app structure that will be dynamic? I just want to access the new generated file.

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

9
  • 1
    It's not clear what you really want. You say you want the file dynamically but the file is already generated. Which part would be dynamic?
    – Gaël J
    Aug 12, 2021 at 17:49
  • @GaëlJ I meant that it's generated with other content by the client but I got the old content
    – lingar
    Aug 12, 2021 at 17:52
  • 1
    Which client? Please update your question with more details like a scenario of what is happening and what you'd expect.
    – Gaël J
    Aug 12, 2021 at 17:54
  • 1
    A couple of ideas: 1 - don't write in src/main/resources, the folder would not exist on a production server 2 - don't use static serving of the file but implement a controller that will read the file content and send it back to the caller
    – Gaël J
    Aug 12, 2021 at 18:43
  • 1
    If you want your app to be truly dynamic, then you should dynamically create the Excel doc at run-time using a Java lib such as jexcelapi.sourceforge.net/resources/javadocs/current/docs/jxl/….
    – smac2020
    Aug 13, 2021 at 14:27

1 Answer 1

1

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.

1
  • 1
    Cool! man, exactly what I needed. I already succeed to handle the part of the server side, but I was stuck with how to make manually navigation to to the link. Your window.location, is the Brilliant simplicity. please consider to post your answer here stackoverflow.com/questions/35206589/… because you solve the problem without all this exaggerated workarounds.
    – lingar
    Aug 15, 2021 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.