I have a Java project which is used as a component in a webapp. This java code writes an xls file in a specific folder. I want to provide a download functionality for this file which should be triggered as soon as file writing is done.
The problem is – without a server environment, how can write a download functionality?
I have a Java project which is used as a component in a webapp.
Share
Don’t write to file in a specific folder. Just write to the HTTP response body immediately. The downloading job should just be done in the webapp’s code. I assume that you’re using Servlets. If you set the HTTP response
Content-Dispositionheader toattachment, then the browser will pop a Save as dialogue. If you also set theContent-Typeheader, then the browser will understand what to do with it (e.g. it will then be able to ask Do you want to open it in Excel or to save? and so on).If the API of that Java project is well designed, then you should have a method something like this:
This way you can call it in the servlet as follows after setting the aforementioned headers:
Even more, it could easily be reused/tested in a plain vanilla Java application like follows: