Whats the best approach to upload a file and serve it on Web

277 views
Skip to first unread message

Niraj Chauhan

unread,
Sep 13, 2017, 8:21:40 AM9/13/17
to vert.x

I have a situation where uploaded files need to be served on the web. So I have a route which uploads images to a static folder and when I try browsing this file, it gives me 404. But if I restart my application and access the same URL, it works.

Upload folder structure:
── src
 
├── main
 
  └── resources
 
      └── webroot
 
          └── static
 
              ├── css
 
              ├── js
 
              └── uploads


My code for body handler:

router.route().handler(BodyHandler.create().setUploadsDirectory("src/main/resources/webroot/static/uploads"));
router
.route().handler(StaticHandler.create());

So the file is getting uploaded, but I have to restart the application. Also, I am running the app from Idea intelliJ.

Is this a correct way to do?

Niraj Chauhan

unread,
Sep 14, 2017, 1:39:08 AM9/14/17
to vert.x
Also tried with shadowJar, still the same problem.

Thomas SEGISMONT

unread,
Sep 14, 2017, 4:32:47 AM9/14/17
to ve...@googlegroups.com
We'd need to see a full reproducer since the code is not enough to explain how it would even work: uploaded files are stored with a random uuid name, how can you browse to it?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/e6f7d01e-8e13-4761-a8f1-e916d1d700ea%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Niraj Chauhan

unread,
Sep 14, 2017, 5:29:01 AM9/14/17
to vert.x
Hi,

My code:

router.route().handler(BodyHandler.create().setUploadsDirectory("src/main/resources/webroot/static/images"));
router
.route().handler(StaticHandler.create());
router
.post("/images/upload").handler(routingContext -> {
   
List<FileUpload> fileUploads = new ArrayList<>(routingContext.fileUploads());
   
FileUpload f = fileUploads.get(0);
   
String extension = acceptedMimeTypes.get(f.contentType());
       
// f.uploadedFileName() is src/main/resources/webroot/static/images/random-number
    vertx
.fileSystem().moveBlocking(f.uploadedFileName(), f.uploadedFileName() + ".png");
    routingContext
.response().end(getNewFileName(f.uploadedFileName() + extension)); // random-number.png
});

So I have linked my uploads folder to Bodyhandler, that folder it self is also my static resource.

So my browser receives "random-number.png", and it looks for http://localhost:3000/static/images/random-number.png and this returns me 404. But the file is present, now if I restart my IntelliJ application and try to access the same file, it works.

Is this is a right way to do it?


On Thursday, 14 September 2017 14:02:47 UTC+5:30, Thomas Segismont wrote:
We'd need to see a full reproducer since the code is not enough to explain how it would even work: uploaded files are stored with a random uuid name, how can you browse to it?
2017-09-14 7:39 GMT+02:00 Niraj Chauhan <nirajm...@gmail.com>:
Also tried with shadowJar, still the same problem.

On Wednesday, 13 September 2017 17:51:40 UTC+5:30, Niraj Chauhan wrote:

I have a situation where uploaded files need to be served on the web. So I have a route which uploads images to a static folder and when I try browsing this file, it gives me 404. But if I restart my application and access the same URL, it works.

Upload folder structure:
── src
 
├── main
 
  └── resources
 
      └── webroot
 
          └── static
 
              ├── css
 
              ├── js
 
              └── uploads


My code for body handler:

router.route().handler(BodyHandler.create().setUploadsDirectory("src/main/resources/webroot/static/uploads"));
router
.route().handler(StaticHandler.create());

So the file is getting uploaded, but I have to restart the application. Also, I am running the app from Idea intelliJ.

Is this a correct way to do?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Paulo Lopes

unread,
Sep 14, 2017, 6:31:15 AM9/14/17
to vert.x
The issue is that you're uploading to the source code location while the application is either running from the jar class path (shadow jar) or from the target directory.

You should upload to a external place say: /tmp/upload and serve files from there, also be sure to check the static handler options for caching properties.

Niraj Chauhan

unread,
Sep 14, 2017, 8:07:00 AM9/14/17
to vert.x
When I try changing the webroot path, it gives me error as

java.lang.IllegalArgumentException: root cannot start with '/'

Code:
router.route().handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("/tmp/uploads"));

Paulo Lopes

unread,
Sep 14, 2017, 11:13:37 AM9/14/17
to vert.x
Hi,

That is a built in fools proof check so users do not expose the whole file system to the internet by accident. To allow it you need first to call 'setAllowRootFileSystemAccess(true)'

Niraj Chauhan

unread,
Sep 14, 2017, 1:34:15 PM9/14/17
to vert.x
Thanks, this worked. Instead of using setAllowRootFileSystemAccess(), I just created a folder in the project root and it worked.

router.route().handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("assets/webroot/"));
Reply all
Reply to author
Forward
0 new messages