Hi guys,
I'm trying to export some data from JIRA into a file and after that I want to redirect the user to the file's download URL.
So what I need to achieve is:
Have anyone already done something similar? Or what should I be looking for to accomplish this?
Thanks!
Community moderators have prevented the ability to post new answers.
Do you really need to export it into a file ? If not you can directly pass the file contents to the user.
Here's a sample code for what you've described (should work with a few modifications):
JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome.class); File file = new File(jiraHome.getHome(), "myfile.txt"); // Write the contents to the file
....... // Read the file and write it to the response HttpServletResponse response = getHttpResponse(); // set to binary type for example response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); // forces download String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", file.getName()); response.setHeader(headerKey, headerValue); FileInputStream fileInputStream = new FileInputStream(file); ServletOutputStream outputStream = response.getOutputStream(); try { IOUtils.copy(fileInputStream, outputStream); } finally { fileInputStream.close(); outputStream.close(); }
You can use this code from a Servlet or Webwork action.
If you really do not need to save the file to JIRA_HOME you can directly write the generated contents to the output stream.
Hi Boris,
Thanks for your quick reply! The code does what I intended. :)
However, I have a small issue. I'm using it from a Webwork action and I get the following exception: "java.lang.IllegalStateException: getOutputStream() has already been called for this response". I believe that this is happening because I'm returning a SUCCESS (and by doing so redirecting the browser to my success view) string after executing the code that you've sent me.
Do you know what should be the work arround for this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It seems I was wrong when I said that you can use the code from webwork action.
Just create a servlet for downloading the file.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Or you might also try to get the response by calling ServletActionContext.getResponse(), this seems to be the right way of getting the output stream.
Also then you may return NONE instead SUCCESS.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.