0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ronny
  • 1
  • `src/main/resources/static/files/` is the wrong location for a running application. The `src/main/resources` location is only for resources in the sources of your project. – Mark Rotteveel Jan 21 '22 at 12:52
  • @MarkRotteveel thank you for your fast respone. So you would recommend creating a new direction on the level /src/main that holds the new files? – Ronny Jan 21 '22 at 12:54
  • No, because `src` is a location for the sources of your project, it doesn't exist at runtime. Also, as the name implies the `static` files is for static files, not dynamically generated files. You may want to look at [download a file from Spring boot rest service](https://stackoverflow.com/questions/35680932/download-a-file-from-spring-boot-rest-service) – Mark Rotteveel Jan 21 '22 at 12:54
  • So is there any way I can create the files dynamically and make them downloadable inside the project, so I can still deploy this project somewhere else where I may not have access to other paths outside the object? – Ronny Jan 21 '22 at 13:06
  • If you want to temporary store the books, see https://stackoverflow.com/questions/617414/how-to-create-a-temporary-directory-folder-in-java (but you'll have to account for the fact that they can be gone at any time). If they should be permanent, you'll need to know how you will deploy. For cloud deployments, you will typically using something like AWS S3 or Azure Blob Storage to upload the files to. – Wim Deblauwe Jan 21 '22 at 15:40

0 Answers0