Getting 404 when accessing static file from ktor server

33 Views Asked by At

My files are stored under uploads folder next to my fat jar server and files are getting saved there properly but while accessing these files over a cloud server http://{IP_ADDRESS}:8085/uploads/image1.png, I'm getting 404. This works perfectly fine on my localhost.

//Directory structure

root@localhost:~/ktor# ls
build my-server.jar  uploads

//Backend code

fun Application.configureRouting() {

    routing {
        staticRoutes()
    }
}


fun Route.staticRoutes() {
    val projectRoot = File(System.getProperty("user.dir"))
    staticFiles("/uploads/", File("${projectRoot}\\uploads"))
}

Not sure what am I missing, any clue?

1

There are 1 best solutions below

0
Aleksei Tirman On

The problem is in the backslash path delimiter ("${projectRoot}\\uploads"). The following code works as expected:

fun Route.staticRoutes() {
    val projectRoot = File(System.getProperty("user.dir"))
    staticFiles("/uploads", projectRoot.resolve("uploads"))
}