How to serve static file with Javalin

610 views Asked by At

Building a web app with Javalin. How do I display a PDF from my staticfiles folder in the app.post function?

I can't use .getResourceAsStream because it has images, and the function couldn't parse it. I can serve html files using ctx.html but can't find similar for PDF. I can find it manually by typing the file name into the address bar but want to serve it automatically.

1

There are 1 answers

0
Maksim Klyuev On

You can add directory with static files to your Javalin app like this:

        Javalin app = Javalin.create(javalinConfig -> {
            javalinConfig.staticFiles.add("/static");//add() accepts path to directory with static files 
        }).start(7070);

Now you can simply redirect in the app.post function:

        app.post("/", ctx -> ctx.redirect("/fileName.pdf"));

"/fileName.pdf" - path to your file in "static" folder