@Test // SPR-16545 public void transferTo() { ServerHttpRequest request = generateMultipartRequest(); ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class); MultiValueMap<String, Part> parts = this.reader.readMono(elementType, request, emptyMap()).block(); assertNotNull(parts); FilePart part = (FilePart) parts.getFirst("fooPart"); assertNotNull(part); File dest = new File(System.getProperty("java.io.tmpdir") + "/" + part.filename()); part.transferTo(dest).block(Duration.ofSeconds(5)); assertTrue(dest.exists()); assertEquals(12, dest.length()); assertTrue(dest.delete()); }
private static String partDescription(Part part) { return part instanceof FilePart ? part.name() + ":" + ((FilePart) part).filename() : part.name(); }
/** * Convenience method to copy the content of the file in this part to the * given destination file. If the destination file already exists, it will * be truncated first. * <p>The default implementation delegates to {@link #transferTo(Path)}. * @param dest the target file * @return completion {@code Mono} with the result of the file transfer, * possibly {@link IllegalStateException} if the part isn't a file * @see #transferTo(Path) */ default Mono<Void> transferTo(File dest) { return transferTo(dest.toPath()); }
assertTrue(part instanceof FilePart); FilePart filePart = (FilePart) part; assertEquals("a.txt", filePart.filename()); assertEquals(MediaType.TEXT_PLAIN, filePart.headers().getContentType()); }) .consumeNextWith(part -> { assertTrue(part instanceof FilePart); FilePart filePart = (FilePart) part; assertEquals("a.html", filePart.filename()); assertEquals(MediaType.TEXT_HTML, filePart.headers().getContentType()); }) .expectComplete()
@Test public void testMultipart() throws Exception { MultipartBean bean = new MultipartBean(); WebExchangeDataBinder binder = new WebExchangeDataBinder(bean); MultiValueMap<String, Object> data = new LinkedMultiValueMap<>(); data.add("name", "bar"); data.add("someList", "123"); data.add("someList", "abc"); data.add("someArray", "dec"); data.add("someArray", "456"); data.add("part", new ClassPathResource("org/springframework/http/codec/multipart/foo.txt")); data.add("somePartList", new ClassPathResource("org/springframework/http/codec/multipart/foo.txt")); data.add("somePartList", new ClassPathResource("org/springframework/http/server/reactive/spring.png")); binder.bind(exchangeMultipart(data)).block(Duration.ofMillis(5000)); assertEquals("bar", bean.getName()); assertEquals(Arrays.asList("123", "abc"), bean.getSomeList()); assertArrayEquals(new String[] {"dec", "456"}, bean.getSomeArray()); assertEquals("foo.txt", bean.getPart().filename()); assertEquals(2, bean.getSomePartList().size()); assertEquals("foo.txt", bean.getSomePartList().get(0).filename()); assertEquals("spring.png", bean.getSomePartList().get(1).filename()); }
/** * Convenience method to copy the content of the file in this part to the * given destination file. If the destination file already exists, it will * be truncated first. * <p>The default implementation delegates to {@link #transferTo(Path)}. * @param dest the target file * @return completion {@code Mono} with the result of the file transfer, * possibly {@link IllegalStateException} if the part isn't a file * @see #transferTo(Path) */ default Mono<Void> transferTo(File dest) { return transferTo(dest.toPath()); }
File verticesFile = new File(ClientUtils.tempDirectory(), verticesFilePart.filename()); verticesFilePart.transferTo(verticesFile); File edgesFile = new File(ClientUtils.tempDirectory(), edgesFilePart.filename()); edgesFilePart.transferTo(edgesFile);
public Mono<ServerResponse> parts(ServerRequest request) { return request.body(BodyExtractors.toParts()).collectList() .flatMap(parts -> { try { assertEquals(2, parts.size()); assertEquals("foo.txt", ((FilePart) parts.get(0)).filename()); assertEquals("bar", ((FormFieldPart) parts.get(1)).value()); } catch(Exception e) { return Mono.error(e); } return ServerResponse.ok().build(); }); } }
/** * Convenience method to copy the content of the file in this part to the * given destination file. If the destination file already exists, it will * be truncated first. * <p>The default implementation delegates to {@link #transferTo(Path)}. * @param dest the target file * @return completion {@code Mono} with the result of the file transfer, * possibly {@link IllegalStateException} if the part isn't a file * @see #transferTo(Path) */ default Mono<Void> transferTo(File dest) { return transferTo(dest.toPath()); }
public Mono<ServerResponse> multipartData(ServerRequest request) { return request .body(BodyExtractors.toMultipartData()) .flatMap(map -> { Map<String, Part> parts = map.toSingleValueMap(); try { assertEquals(2, parts.size()); assertEquals("foo.txt", ((FilePart) parts.get("fooPart")).filename()); assertEquals("bar", ((FormFieldPart) parts.get("barPart")).value()); } catch(Exception e) { return Mono.error(e); } return ServerResponse.ok().build(); }); }
@Test // SPR-16402 public void singleSubscriberWithResource() throws IOException { UnicastProcessor<Resource> processor = UnicastProcessor.create(); Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); Mono.just(logo).subscribe(processor); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.asyncPart("logo", processor, Resource.class); Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build()); Map<String, Object> hints = Collections.emptyMap(); this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(); MultiValueMap<String, Part> requestParts = parse(hints); assertEquals(1, requestParts.size()); Part part = requestParts.getFirst("logo"); assertEquals("logo", part.name()); assertTrue(part instanceof FilePart); assertEquals("logo.jpg", ((FilePart) part).filename()); assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType()); assertEquals(logo.getFile().length(), part.headers().getContentLength()); }
@Test // SPR-16376 public void customContentDisposition() throws IOException { Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); Flux<DataBuffer> buffers = DataBufferUtils.read(logo, new DefaultDataBufferFactory(), 1024); long contentLength = logo.contentLength(); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("resource", logo) .headers(h -> h.setContentDispositionFormData("resource", "spring.jpg")); bodyBuilder.asyncPart("buffers", buffers, DataBuffer.class) .headers(h -> { h.setContentDispositionFormData("buffers", "buffers.jpg"); h.setContentType(MediaType.IMAGE_JPEG); h.setContentLength(contentLength); }); MultiValueMap<String, HttpEntity<?>> multipartData = bodyBuilder.build(); Map<String, Object> hints = Collections.emptyMap(); this.writer.write(Mono.just(multipartData), null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(); MultiValueMap<String, Part> requestParts = parse(hints); assertEquals(2, requestParts.size()); Part part = requestParts.getFirst("resource"); assertTrue(part instanceof FilePart); assertEquals("spring.jpg", ((FilePart) part).filename()); assertEquals(logo.getFile().length(), part.headers().getContentLength()); part = requestParts.getFirst("buffers"); assertTrue(part instanceof FilePart); assertEquals("buffers.jpg", ((FilePart) part).filename()); assertEquals(logo.getFile().length(), part.headers().getContentLength()); }
private void assertFooPart(Part part) { assertEquals("fooPart", part.name()); assertTrue(part instanceof FilePart); assertEquals("foo.txt", ((FilePart) part).filename()); StepVerifier.create(DataBufferUtils.join(part.content())) .consumeNextWith(buffer -> { assertEquals(12, buffer.readableByteCount()); byte[] byteContent = new byte[12]; buffer.read(byteContent); assertEquals("Lorem Ipsum.", new String(byteContent)); }) .verifyComplete(); }
@Test public void resolveParts() { ServerHttpRequest request = generateMultipartRequest(); ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class); MultiValueMap<String, Part> parts = this.reader.readMono(elementType, request, emptyMap()).block(); assertEquals(2, parts.size()); assertTrue(parts.containsKey("fooPart")); Part part = parts.getFirst("fooPart"); assertTrue(part instanceof FilePart); assertEquals("fooPart", part.name()); assertEquals("foo.txt", ((FilePart) part).filename()); DataBuffer buffer = DataBufferUtils.join(part.content()).block(); assertEquals(12, buffer.readableByteCount()); byte[] byteContent = new byte[12]; buffer.read(byteContent); assertEquals("Lorem Ipsum.", new String(byteContent)); assertTrue(parts.containsKey("barPart")); part = parts.getFirst("barPart"); assertTrue(part instanceof FormFieldPart); assertEquals("barPart", part.name()); assertEquals("bar", ((FormFieldPart) part).value()); }
assertTrue(part instanceof FilePart); assertEquals("logo", part.name()); assertEquals("logo.jpg", ((FilePart) part).filename()); assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType()); assertEquals(logo.getFile().length(), part.headers().getContentLength()); assertTrue(part instanceof FilePart); assertEquals("utf8", part.name()); assertEquals("Hall\u00F6le.jpg", ((FilePart) part).filename()); assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType()); assertEquals(utf8.getFile().length(), part.headers().getContentLength());
private OperationRequestPart createOperationRequestPart(Part part) { ByteArrayOutputStream content = readPartBodyContent(part); return new OperationRequestPartFactory().create(part.name(), (part instanceof FilePart) ? ((FilePart) part).filename() : null, content.toByteArray(), part.headers()); }