From fd2c935ae1668a1e44110fb76f6ba8ab2339c7a6 Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Wed, 12 Feb 2025 16:55:35 +0900 Subject: [PATCH] =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cokr/xit/foundation/web/WebClient.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/cokr/xit/foundation/web/WebClient.java b/src/main/java/cokr/xit/foundation/web/WebClient.java index 0ae6bb9..71f96ac 100644 --- a/src/main/java/cokr/xit/foundation/web/WebClient.java +++ b/src/main/java/cokr/xit/foundation/web/WebClient.java @@ -268,6 +268,32 @@ public class WebClient extends AbstractComponent { } } + /**데이터 및 파일의 업로드 요청을 전송한다. + * @param 요청의 body 핸들러 유형 + * @param configurer 업로드 요청의 설정자 + * @return http 응답 + */ + @SuppressWarnings("unchecked") + public HttpResponse upload(Consumer configurer) { + Request req = new Request().multipart(); + req.charset = charset(); + configurer.accept(req); + HttpRequest hreq = req.post(); + + try { + if (req.download) + return (HttpResponse)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), req.downloadHandler); + else + return (HttpResponse)handleRequest(req.async, hreq, HttpResponse.BodyHandlers.ofString(), req.textHandler); + } catch (Throwable e) { + if (req.async) { + req.errorHandler.accept(rootCause(e)); + return null; + } else + throw runtimeException(e); + } + } + /**응답 유형이 다운로드한 파일일 경우, 해당 파일을 지정한 경로로 저장한다. * @param inputStream 다운로드한 파일 * @param path 저장할 경로 @@ -432,6 +458,13 @@ public class WebClient extends AbstractComponent { return contentType(ContentType.JSON); } + /**content-type 헤더를 멀티파트로 설정한다. + * @return 현재 Request + */ + public Request multipart() { + return contentType(ContentType.MULTIPART); + } + private JSON json() { return ifEmpty(json, () -> json = new JSON()); }