use of io.vertx.ext.web.FileUpload in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
// Enable multipart form data parsing
router.route().handler(BodyHandler.create().setUploadsDirectory("uploads"));
router.route("/").handler(routingContext -> {
routingContext.response().putHeader("content-type", "text/html").end("<form action=\"/form\" method=\"post\" enctype=\"multipart/form-data\">\n" + " <div>\n" + " <label for=\"name\">Select a file:</label>\n" + " <input type=\"file\" name=\"file\" />\n" + " </div>\n" + " <div class=\"button\">\n" + " <button type=\"submit\">Send</button>\n" + " </div>" + "</form>");
});
// handle the form
router.post("/form").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().setChunked(true);
for (FileUpload f : ctx.fileUploads()) {
System.out.println("f");
ctx.response().write("Filename: " + f.fileName());
ctx.response().write("\n");
ctx.response().write("Size: " + f.size());
}
ctx.response().end();
});
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
use of io.vertx.ext.web.FileUpload in project incubator-servicecomb-java-chassis by apache.
the class VertxServerRequestToHttpServletRequest method getPart.
@Override
public Part getPart(String name) throws IOException, ServletException {
Optional<FileUpload> upload = context.fileUploads().stream().filter(fileUpload -> fileUpload.name().equals(name)).findFirst();
if (!upload.isPresent()) {
LOGGER.error("No such file with name: {}.", name);
return null;
}
final FileUpload fileUpload = upload.get();
return new FileUploadPart(fileUpload);
}
use of io.vertx.ext.web.FileUpload in project parser-excel-elasticsearch by codingchili.
the class Website method setRouterAPI.
/**
* Adds the upload route to the given router
*
* @param router the upload route is added to the given router
*/
private void setRouterAPI(Router router) {
router.route("/api/upload").handler(context -> {
Iterator<FileUpload> iterator = context.fileUploads().iterator();
if (iterator.hasNext()) {
MultiMap params = context.request().params();
logger.info("Receiving uploaded file with request id " + params.get(UPLOAD_ID));
FileUpload upload = context.fileUploads().iterator().next();
vertx.fileSystem().readFile(upload.uploadedFileName(), file -> {
parse(file.result(), params, upload.fileName(), Future.<Integer>future().setHandler(result -> {
if (result.succeeded()) {
String index = context.request().params().get(INDEX);
logger.info(String.format("Imported file '%s' successfully into '%s'.", upload.fileName(), index));
context.put(INDEX, index);
context.put(FILE, upload.fileName());
context.put(IMPORTED, result.result());
context.reroute(DONE);
} else {
context.put(MESSAGE, traceToText(result.cause()));
logger.log(Level.SEVERE, String.format("Failed to parse file '%s'.", upload.fileName()), result.cause());
context.reroute(ERROR);
}
}));
});
} else {
context.put(MESSAGE, NO_FILE_WAS_UPLOADED);
context.reroute(ERROR);
}
});
}
use of io.vertx.ext.web.FileUpload in project vertx-web by vert-x3.
the class BodyHandlerTest method testFileUpload.
private void testFileUpload(String uploadsDir, int size) throws Exception {
String name = "somename";
String fileName = "somefile.dat";
String contentType = "application/octet-stream";
Buffer fileData = TestUtils.randomBuffer(size);
router.route().handler(rc -> {
Set<FileUpload> fileUploads = rc.fileUploads();
assertNotNull(fileUploads);
assertEquals(1, fileUploads.size());
FileUpload upload = fileUploads.iterator().next();
assertEquals(name, upload.name());
assertEquals(fileName, upload.fileName());
assertEquals(contentType, upload.contentType());
assertEquals("binary", upload.contentTransferEncoding());
assertEquals(fileData.length(), upload.size());
String uploadedFileName = upload.uploadedFileName();
assertTrue(uploadedFileName.startsWith(uploadsDir + File.separator));
Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName);
assertEquals(fileData, uploaded);
// the data is upload as HTML form, so the body should be empty
Buffer rawBody = rc.getBody();
assertEquals(0, rawBody.length());
rc.response().end();
});
sendFileUploadRequest(fileData, 200, "OK");
}
use of io.vertx.ext.web.FileUpload in project java-chassis by ServiceComb.
the class VertxServerRequestToHttpServletRequest method getPart.
@Override
public Part getPart(String name) {
Optional<FileUpload> upload = context.fileUploads().stream().filter(fileUpload -> fileUpload.name().equals(name)).findFirst();
if (!upload.isPresent()) {
LOGGER.debug("No such file with name: {}.", name);
return null;
}
final FileUpload fileUpload = upload.get();
return new FileUploadPart(fileUpload);
}
Aggregations