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