Skip to content

Instantly share code, notes, and snippets.

Embed
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;")) {
return Arrays.stream(header.split(";"))
.map(item -> item.trim())
.filter(item -> item.startsWith("filename="))
.findFirst()
.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"));
}
}).orElse(HttpResponse.BodyProcessor.asFile(path));
};
}
}
@skrb

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.