0

I made some simple file write and download with java spring, but I encountered some strange problems.

private void makeFile () throws IOException {
   long time = new Date().getTime() / 1000;
   String filename = "compress" + time + ".txt";
   // File convFile = new File("/static/upload/"+filename);
   try (Writer writer = new BufferedWriter(new OutputStreamWriter(
      new FileOutputStream("./src/main/resources/static/storage/" + filename), "utf-8"))) {
      writer.write( this.code + "#" + this.probabilities.toString());
      this.filename = filename;
   }
}

Now when I run this by sending post request to page. It returns me new page with:

<a th:href="@{'./storage/'+${link}}" target="_blank" download>Download File</a>

where the link is just model attribute for filename

Now, this doesn't work until I switch back to the intellij editor. It doesn't work because the file doesn't exist and it appears just after I switch back to the editor. I don't know why is that happening. It may be hard to understand this, I could record it if you don't understand what I mean.

I added video. https://youtu.be/FbaUq77L55s

  • Possible duplicate of https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers – Varun Jain Feb 13 '20 at 13:51
  • @VarunJain why is this duplicate to that? I wasn't asking how to download file, but why is it appearing in folder when I open editor? Could you explain why –  Feb 13 '20 at 13:53
  • Just go through the provided link and cross-check if you missed anything. – Varun Jain Feb 13 '20 at 13:55
  • @VarunJain I added video https://youtu.be/FbaUq77L55s –  Feb 13 '20 at 14:06
  • You've to switch to your IDE then only the file is getting downloaded. Is that so? – Varun Jain Feb 13 '20 at 14:09
  • It is written in directory after I switch back to IDE, why is that? –  Feb 13 '20 at 14:10
  • Can you please build your project and then try to execute the same and see what is happening? – Varun Jain Feb 13 '20 at 14:11
  • Now I get other problem, the system cannot get path for writing file at '.\src\main\resources\static\storage\compress1581603528.txt'. –  Feb 13 '20 at 14:20
  • This occurs after building and executing your project. If it is so then this is a genuine case where you'll not have a "src" directory in it after building your project. – Varun Jain Feb 14 '20 at 05:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207815/discussion-between-varun-jain-and-deep-seeker). – Varun Jain Feb 14 '20 at 05:56

0 Answers0