类org.springframework.http.codec.multipart.FormFieldPart源码实例Demo

下面列出了怎么用org.springframework.http.codec.multipart.FormFieldPart的API类实例代码及写法,或者点击链接到github查看源代码。


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();
			});
}
 

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();
			});
}
 

@PostMapping("/requestPart")
void requestPart(
		@RequestPart FormFieldPart fieldPart,
		@RequestPart("fileParts") FilePart fileParts,
		@RequestPart("fileParts") Mono<FilePart> filePartsMono,
		@RequestPart("fileParts") Flux<FilePart> filePartsFlux,
		@RequestPart("jsonPart") Person person,
		@RequestPart("jsonPart") Mono<Person> personMono) {

	assertEquals("fieldValue", fieldPart.value());
	assertEquals("fileParts:foo.txt", partDescription(fileParts));
	assertEquals("Jason", person.getName());

	StepVerifier.create(partFluxDescription(filePartsFlux))
			.consumeNextWith(content -> assertEquals("[fileParts:foo.txt,fileParts:logo.png]", content))
			.verifyComplete();

	StepVerifier.create(filePartsMono)
			.consumeNextWith(filePart -> assertEquals("fileParts:foo.txt", partDescription(filePart)))
			.verifyComplete();

	StepVerifier.create(personMono)
			.consumeNextWith(p -> assertEquals("Jason", p.getName()))
			.verifyComplete();
}
 
源代码4 项目: influx-proxy   文件: InfluxProxyHandler.java

private static void addBindValue(Map<String, Object> params, String key, List<?> values) {
    if (!CollectionUtils.isEmpty(values)) {
        values = values.stream()
                .map(value -> value instanceof FormFieldPart ? ((FormFieldPart) value).value() : value)
                .collect(Collectors.toList());
        params.put(key, values.size() == 1 ? values.get(0) : values);
    }
}
 

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();
			});
}
 

@PostMapping("/requestPart")
void requestPart(@RequestPart FormFieldPart fieldPart,
		@RequestPart("fileParts") FilePart fileParts,
		@RequestPart("jsonPart") Mono<Person> personMono) {

	assertEquals("fieldValue", fieldPart.value());
	assertEquals("fileParts:foo.txt", partDescription(fileParts));

	StepVerifier.create(personMono)
			.consumeNextWith(p -> assertEquals("Jason", p.getName()))
			.verifyComplete();
}
 

private static void addBindValue(Map<String, Object> params, String key, List<?> values) {
	if (!CollectionUtils.isEmpty(values)) {
		values = values.stream()
				.map(value -> value instanceof FormFieldPart ? ((FormFieldPart) value).value() : value)
				.collect(Collectors.toList());
		params.put(key, values.size() == 1 ? values.get(0) : values);
	}
}
 

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();
			});
}
 

private static void addBindValue(Map<String, Object> params, String key, List<?> values) {
	if (!CollectionUtils.isEmpty(values)) {
		values = values.stream()
				.map(value -> value instanceof FormFieldPart ? ((FormFieldPart) value).value() : value)
				.collect(Collectors.toList());
		params.put(key, values.size() == 1 ? values.get(0) : values);
	}
}
 

private MultiValueMap<String, String> multi(MultiValueMap<String, Part> body) {
	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	for (String key : body.keySet()) {
		for (Part part : body.get(key)) {
			if (part instanceof FormFieldPart) {
				FormFieldPart form = (FormFieldPart) part;
				map.add(key, form.value());
			}
		}
	}
	return map;
}
 

private void assertBarPart(Part part) {
	assertEquals("barPart", part.name());
	assertTrue(part instanceof FormFieldPart);
	assertEquals("bar", ((FormFieldPart) part).value());
}
 

private void assertBarPart(Part part) {
	assertEquals("barPart", part.name());
	assertTrue(part instanceof FormFieldPart);
	assertEquals("bar", ((FormFieldPart) part).value());
}
 
 类所在包
 同包方法