I am using Spring Boot to build a library application and also want to make a PDF of a book downloadable. At the start of the application I use a setup method where I save books in my database and create PDF files for each book.
My problem is that when I want to download one of those files of my application, it says that there is no file.
When I rebuild my application in IntelliJ everything seems to work and the files are downloadable.
So this seems to be a problem with Spring Boot not recognizing dynamic files? here you can see the created files of the PDFs
My HTML tag to fetch the file:
<a th:if="${user.ownedBooks.contains(book)}" th:href="@{'/files/' + ${book.getID()} + '.pdf'}" th:download="${book.getID()} + '.pdf'">Download</a>
Creating the files:
public void createFile() {
String path = "src/main/resources/static/files/" + isbn + ".pdf";
File f = new File(path);
File directory = new File("src/main/resources/static/files/");
if (! directory.exists()){
directory.mkdir();
}
if (!f.exists()) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(path));
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
}
document.open();
try {
document.add(new Paragraph(description));
} catch (DocumentException e) {
e.printStackTrace();
}
document.close();
}
}
Also my main:
@SpringBootApplication
public class LibraryApplication implements ApplicationRunner{
@Autowired
private BookService bookService;
@Autowired
private AuthorService authorService;
@Autowired
private UserService userService;
public static void main(String[] args) {
SpringApplication.run(LibraryApplication.class, args);
}
@Override
public void run(ApplicationArguments args){
Scrape scrape = new Scrape(bookService, authorService, userService);
scrape.setup();
try {
userService.getUserByUsername("admin@othr.de");
} catch (Exception ex) {
User admin = new User();
admin.setName("john");
admin.setPassword("secret");
admin.setAccountType(AccountType.ADMIN);
userService.registerUser(admin);
}
try {
userService.getUserByUsername("maxi@muster.de");
} catch (Exception ex) {
User normalo = new User();
normalo.setName("Maxi");
normalo.setPassword("secret");
normalo.setAccountType(AccountType.STANDARD);
userService.registerUser(normalo);
}
}
}
My Scrape Class uses the annotation @Component as well.
So after I've done setup and I restart or rebuild the application it works. Can anyone help me with a solution for that?