0

I am using java and struts. I have a scenario where there is 'Download' link in the page. After clicking on this link the control goes to the Action class, where I have String content which I need to write to a .txt file and then download that txt file.

Eventually whenever we click on the download link, we should be able to download a txt file having content a particular string.

I used below piece of code,

FileOutputStream outputStream = new FileOutputStream(fileNameWithDirectory);
outputStream.write(fileContentString.getBytes());
outputStream.close();
ActionForward forward = new ActionForward("doc/" + filename);
forward.setName(filename);
forward.setRedirect(true);
return forward;

Also I tried with FileWriter in place of FileOutputStream like,

FileWriter fileWriter = new FileWriter(fileNameWithDirectory);
fileWriter.write(fileContentString);
fileWriter.flush();
fileWriter.close();

But always instead of downloading the txt file, the control opens a new window where the String content is written. Please suggest me, how would I able to download that .txt file.

1
  • What are you using on your server? Servlets?
    – f1sh
    May 29, 2019 at 9:41

3 Answers 3

0

You should add Content-Disposition: attachment to say browser, that it should download the file, not to open it.

See more details here

Also Struts has DownloadAction, you may use it as well.

2
  • Content-Disposition: attachment is working and I got download option, but after downloading the .txt file, No content is written there, it's downloading Blank.
    – Jerry
    May 29, 2019 at 11:29
  • @Prosenjit could you update your question with changed code?
    – DDovzhenko
    May 29, 2019 at 11:35
0

You don't need to write the file and then redirect to it. You can set a http response header called Content-Disposition and then print your data into the http response body.

use it like this

response.addHeader("Content-Disposition", "Content-Disposition: attachment; filename=\""+filename+"\"");

of course this depends on which technology stack you're using.

2
  • Content-Disposition: attachment is working and I got download option, but after downloading the .txt file, No content is written there, it's downloading Blank.
    – Jerry
    May 29, 2019 at 11:29
  • What code are you using to write the content to the http response body?
    – f1sh
    May 29, 2019 at 12:01
0

Convert your text file to stream. and set content type as you wanted to download. response.setContentType("application/pdf");

try {
      // get your file as InputStream
      InputStream is = ...;

      // copy it to response's OutputStream
      org.apache.commons.io.IOUtils.copy(is, convertedTextFiletoStream);

    } catch (IOException ex) {
      log.info("Error writing file to output stream. Filename was '{}'", fileName, ex);
      throw new RuntimeException("IOError writing file to output stream");
    }

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.