@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); } }
@Override public String fileName() { return upload.filename(); }
Buffer tot = Buffer.buffer(); assertEquals("file", upload.name()); assertEquals("tmp-0.txt", upload.filename()); assertEquals("image/gif", upload.contentType()); String uploadedFileName;
/** * @return the filename which was used when upload the file. */ public String filename() { String ret = delegate.filename(); return ret; }
/** * The name of the file. * * @return the file name */ @Override public String name() { return upload.filename(); }
/** * @return the filename which was used when upload the file. */ public String filename() { String ret = delegate.filename(); return ret; }
@Override public String fileName() { return upload.filename(); }
/** * Creates a temporary file. * * @return a new Temp File from getDiskFilename(), default prefix, postfix and baseDirectory */ static synchronized File tempFile(HttpServerFileUpload upload) { String newpostfix; String diskFilename = new File(upload.filename()).getName(); newpostfix = '_' + diskFilename; File tmpFile; try { if (baseDirectory == null) { // create a temporary file tmpFile = File.createTempFile(prefix, newpostfix); } else { tmpFile = File.createTempFile(prefix, newpostfix, new File( baseDirectory)); } if (deleteOnExitTemporaryFile) { tmpFile.deleteOnExit(); } return tmpFile; } catch (IOException e) { // Really bad, can't create the tmp file. throw new IllegalStateException(e); } }
Buffer tot = Buffer.buffer(); assertEquals("file", upload.name()); assertEquals("tmp-0.txt", upload.filename()); assertEquals("image/gif", upload.contentType()); String uploadedFileName;
public YokeFileUpload(@NotNull final Vertx vertx, @NotNull final HttpServerFileUpload fileUpload, @NotNull String uploadDir) { this.fileSystem = vertx.fileSystem(); this.filename = fileUpload.filename(); this.name = fileUpload.name(); this.contentType = fileUpload.contentType(); this.contentTransferEncoding = fileUpload.contentTransferEncoding(); this.charset = fileUpload.charset(); this.size = fileUpload.size(); if (!uploadDir.endsWith(File.separator)) { uploadDir += File.separator; } this.path = uploadDir + UUID.randomUUID().toString(); }
.error("Cannot read the uploaded item {} ({})", upload.name(), upload.filename(), event)) .endHandler(event -> delegate.close()) .handler(
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(); }