@Override public void start() throws Exception { vertx.createHttpServer().requestHandler(req -> { if (req.uri().equals("/")) { // Serve the index page req.response().sendFile("index.html"); } else if (req.uri().startsWith("/form")) { req.setExpectMultipart(true); req.uploadHandler(upload -> { upload.exceptionHandler(cause -> { req.response().setChunked(true).end("Upload failed"); }); upload.endHandler(v -> { req.response().setChunked(true).end("Successfully uploaded to " + upload.filename()); }); // FIXME - Potential security exploit! In a real system you must check this filename // to make sure you're not saving to a place where you don't want! // Or better still, just use Vert.x-Web which controls the upload area. upload.streamToFileSystem(upload.filename()); }); } else { req.response().setStatusCode(404); req.response().end(); } }).listen(8080); } }
upload.streamToFileSystem(uploadedFileName); upload.endHandler(v -> { if (streamToDisk) { Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName);
context.fail(t); }); upload.endHandler(v -> uploadEnded());
public io.vertx.rxjava.core.http.HttpServerFileUpload endHandler(Handler<Void> endHandler) { delegate.endHandler(endHandler); return this; }
public io.vertx.rxjava.core.http.HttpServerFileUpload endHandler(Handler<Void> endHandler) { delegate.endHandler(endHandler); return this; }
context.fail(t); }); upload.endHandler(v -> uploadEnded()); });
upload.streamToFileSystem(uploadedFileName); upload.endHandler(v -> { if (streamToDisk) { Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName);
BHandler(RoutingContext context) { this.context = context; Set<FileUpload> fileUploads = context.fileUploads(); final String contentType = context.request().getHeader(HttpHeaders.CONTENT_TYPE); isMultipart = contentType != null && contentType.contains("multipart/form-data"); isUrlEncoded = contentType != null && contentType.contains("application/x-www-form-urlencoded"); if (isMultipart || isUrlEncoded) { makeUploadDir(context.vertx().fileSystem()); context.request().setExpectMultipart(true); context.request().uploadHandler(upload -> { // *** cse begin *** if (uploadsDir == null) { failed = true; CommonExceptionData data = new CommonExceptionData("not support file upload."); throw new ErrorDataDecoderException(ExceptionFactory.createConsumerException(data)); } // *** cse end *** // we actually upload to a file with a generated filename uploadCount.incrementAndGet(); String uploadedFileName = new File(uploadsDir, UUID.randomUUID().toString()).getPath(); upload.streamToFileSystem(uploadedFileName); FileUploadImpl fileUpload = new FileUploadImpl(uploadedFileName, upload); fileUploads.add(fileUpload); upload.exceptionHandler(context::fail); upload.endHandler(v -> uploadEnded()); }); } context.request().exceptionHandler(context::fail); }
context.fail(t); }); upload.endHandler(v -> uploadEnded());
context.fail(t); }); upload.endHandler(v -> uploadEnded()); });
.endHandler(event -> delegate.close()) .handler( event -> {
private void testFileUploadFormMultipart(int size) throws Exception { Buffer content = Buffer.buffer(TestUtils.randomAlphaString(size)); vertx.fileSystem().writeFileBlocking(testFile.getPath(), content); server.requestHandler(req -> { req.setExpectMultipart(true); req.uploadHandler(upload -> { Buffer fileBuffer = Buffer.buffer(); assertEquals("file", upload.name()); assertEquals("test.txt", upload.filename()); assertEquals("text/plain", upload.contentType()); upload.handler(fileBuffer::appendBuffer); upload.endHandler(v -> assertEquals(content, fileBuffer)); }); req.endHandler(v -> { assertEquals("vert.x", req.getFormAttribute("toolkit")); assertEquals("jvm", req.getFormAttribute("runtime")); req.response().end(); }); }); startServer(); MultipartForm form = MultipartForm.create() .attribute("toolkit", "vert.x") .attribute("runtime", "jvm") .textFileUpload("file", testFile.getName(), testFile.getPath(), "text/plain"); HttpRequest<Buffer> builder = client.post("somepath"); builder.sendMultipartForm(form, onSuccess(resp -> complete())); await(); }