Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
HTTP/2 Client Sample
public class HttpResponseUtils {
public static HttpResponse.BodyHandler<Path> createHandler(Path path) {
return (status, headers) -> {
return headers.firstValue("Content-Disposition")
.map(header -> {
if (header.startsWith("attachment;")) {
.map(item -> item.trim())
.filter(item -> item.startsWith("filename="))
.map(f -> {
int index = f.indexOf("=") + 1;
Path newPath = path.getParent().resolve(f.substring(index));
return HttpResponse.BodyProcessor.asFile(newPath);
}).orElseThrow(() -> new UncheckedIOException(new IOException("No Filename")));
} else if (header.startsWith("inline")){
return HttpResponse.BodyProcessor.asFile(path);
} else {
throw new UncheckedIOException(new IOException("No Attachment"));

This comment has been minimized.

Copy link
Owner Author

@skrb skrb commented Aug 19, 2017

HTTPのヘッダのContent-Dispositionがattachmentかinlineかを判別してダウンロードする HttpResponse.BodyHandler を作成するためのメソッド

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment