Search code examples
javavert.xvertx-verticle

Vertx use uploaded images in html


I am able to upload files at the default location.

router.route().handler(
  BodyHandler.create()
    .setBodyLimit(500 * KB)
);    
router.post("/admin/add").handler(adminActions::createNewItem);

This body handler puts file in file-uploads. This is the code that uploads files:

public void createNewItem(RoutingContext context) {
    var request = context.request();
    var type = context.request().getFormAttribute("type");

    // other code...    

    for(var f : context.fileUploads()) {
      System.out.println(f.fileName());
    }            
  }

These are the files I have uploaded.

enter image description here


The file-uploads folder is at the root of my project, it's been automatically created. How do I reach the content of that folder to show the images in my html pages?

It's weird also, they have no extension...


Solution

  • Vert.x can serve static content with the static handler:

    router.route("/static/*").handler(StaticHandler.create());
    

    The static handler webroot can be configured to point to your file uploads directory.

    Yet I would recommend to consider security carefully if you are working on a public service with private data: if you expose the content of this directory without any protection, malicious users could try to download arbitrary content.