HttpPostMultipartRequestDecoder 관련 문의드립니다.

42 views
Skip to first unread message

mr_doo2

unread,
May 5, 2022, 9:19:56 PM5/5/22
to Netty Korean User Group
안녕하세요.
netty를 사용해서 HTTP API 요청을 수신하는 서버를 개발하던 중 파일 수신과 HttpPostMultipartRequestDecoder 사용 방법 관련해서 질문드립니다.

API 요청에서 파일과 함께 요청하는 경우 multipart/form-data 형식으로 수신하고 있는데요.
요청이 들어왔을 때 일반 필드의 경우 값을 저장하고, 첨부된 File의 경우 지정된 경로에 File을 저장하려 하고 있습니다.

관련된 메소드가 있는지 찾아본 결과 HttpPostMultipartRequestDecoder를 알게 되었는데요. 사용해보려고 하니  예상대로 동작하지 않아 어려움을 겪고 있습니다.
잘못된 부분이나 사용방법에 대해 조언 부탁드립니다.

로그를 통해 확인해본 결과,
multipartRequestDecoder.isMultipart() 는 true이지만 offer(httpContent) 이후
multipartRequestDecoder.currentPartialHttpData() 결과가 null로 나오는 상황입니다.


제가 작성했던 코드 첨부드립니다.
    
    private HttpRequest httpRequest;
    private StringBuilder apiRequest = new StringBuilder();

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object msg) throws IOException {
        if (msg instanceof HttpRequest) {
            httpRequest = (HttpRequest) msg;
            apiRequest.setLength(0);

            if (HttpUtil.is100ContinueExpected(httpRequest)) {
                write100ContinueResponse(channelHandlerContext);
            }

            apiRequest.append(gson.toJson(headerRequest));
            apiRequest.append(HTTP_REQUEST_DELIMITER);
        }
        apiRequest.append(HttpRequestUtils.evaluateDecoderResult(httpRequest));

        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;

            apiRequest.append(HttpRequestUtils.getBodyOfApiRequest(httpContent));
            apiRequest.append(HttpRequestUtils.evaluateDecoderResult(httpRequest));

            if (msg instanceof LastHttpContent) {
                LastHttpContent trailer = (LastHttpContent) msg;

                apiRequest.append(HttpRequestUtils.prepareLastResponse(trailer));

                // TODO Use HttpPostRequestDecoder
                HttpPostMultipartRequestDecoder multipartRequestDecoder = new HttpPostMultipartRequestDecoder(
                        new DefaultHttpDataFactory(false), httpRequest);
                multipartRequestDecoder.setDiscardThreshold(10 * 1024 * 1024);

                logs.info("multipartRequestDecoder : {}", multipartRequestDecoder.isMultipart());
                logs.info("multipartRequestDecoder : {}", multipartRequestDecoder.offer(httpContent));
                logs.info("multipartRequestDecoder : {}", multipartRequestDecoder.currentPartialHttpData());

                FullHttpResponse httpResponse = getApiResponse(channelHandlerContext, apiRequest.toString());

                writeResponse(channelHandlerContext, httpResponse);
                }
            }
        }


netty.PNG
Reply all
Reply to author
Forward
0 new messages