當前位置: 首頁>>代碼示例>>Java>>正文


Java DefaultHttpRequest.setDecoderResult方法代碼示例

本文整理匯總了Java中io.netty.handler.codec.http.DefaultHttpRequest.setDecoderResult方法的典型用法代碼示例。如果您正苦於以下問題:Java DefaultHttpRequest.setDecoderResult方法的具體用法?Java DefaultHttpRequest.setDecoderResult怎麽用?Java DefaultHttpRequest.setDecoderResult使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.netty.handler.codec.http.DefaultHttpRequest的用法示例。


在下文中一共展示了DefaultHttpRequest.setDecoderResult方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testBinaryStreamUpload

import io.netty.handler.codec.http.DefaultHttpRequest; //導入方法依賴的package包/類
private static void testBinaryStreamUpload(boolean withSpace) throws Exception {
    // Boundary starts here with '=' to check against issue https://github.com/netty/netty/issues/3004
    final String boundary = "=dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
    final String contentTypeValue;
    if (withSpace) {
        contentTypeValue = "multipart/form-data; boundary=" + boundary;
    } else {
        contentTypeValue = "multipart/form-data;boundary=" + boundary;
    }
    final DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "http://localhost");

    req.setDecoderResult(DecoderResult.SUCCESS);
    req.headers().add(HttpHeaders.Names.CONTENT_TYPE, contentTypeValue);
    req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);

    // Force to use memory-based data.
    final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);

    for (String data : Arrays.asList("", "\r", "\r\r", "\r\r\r")) {
        final String body =
                "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" +
                "Content-Type: image/gif\r\n" +
                "\r\n" +
                data + "\r\n" +
                "--" + boundary + "--\r\n";

        // Create decoder instance to test.
        final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);

        decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8))));
        decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)));

        // Validate it's enough chunks to decode upload.
        assertTrue(decoder.hasNext());

        // Decode binary upload.
        MemoryFileUpload upload = (MemoryFileUpload) decoder.next();

        // Validate data has been parsed correctly as it was passed into request.
        assertEquals("Invalid decoded data [data=" + data.replaceAll("\r", "\\\\r") + ", upload=" + upload + ']',
            data, upload.getString(CharsetUtil.UTF_8));
        upload.release();
        decoder.destroy();
    }
}
 
開發者ID:wuyinxian124,項目名稱:netty4.0.27Learn,代碼行數:48,代碼來源:HttpPostRequestDecoderTest.java

示例2: testBinaryStreamUpload

import io.netty.handler.codec.http.DefaultHttpRequest; //導入方法依賴的package包/類
private static void testBinaryStreamUpload(boolean withSpace) throws Exception {
    final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
    final String contentTypeValue;
    if (withSpace) {
        contentTypeValue = "multipart/form-data; boundary=" + boundary;
    } else {
        contentTypeValue = "multipart/form-data;boundary=" + boundary;
    }
    final DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "http://localhost");

    req.setDecoderResult(DecoderResult.SUCCESS);
    req.headers().add(HttpHeaders.Names.CONTENT_TYPE, contentTypeValue);
    req.headers().add(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);

    // Force to use memory-based data.
    final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);

    for (String data : Arrays.asList("", "\r", "\r\r", "\r\r\r")) {
        final String body =
                "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" +
                "Content-Type: image/gif\r\n" +
                "\r\n" +
                data + "\r\n" +
                "--" + boundary + "--\r\n";

        // Create decoder instance to test.
        final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);

        decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8))));
        decoder.offer(releaseLater(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)));

        // Validate it's enough chunks to decode upload.
        assertTrue(decoder.hasNext());

        // Decode binary upload.
        MemoryFileUpload upload = (MemoryFileUpload) decoder.next();

        // Validate data has been parsed correctly as it was passed into request.
        assertEquals("Invalid decoded data [data=" + data.replaceAll("\r", "\\\\r") + ", upload=" + upload + ']',
            data, upload.getString(CharsetUtil.UTF_8));
        upload.release();
        decoder.destroy();
    }
}
 
開發者ID:kyle-liu,項目名稱:netty4study,代碼行數:47,代碼來源:HttpPostRequestDecoderTest.java


注:本文中的io.netty.handler.codec.http.DefaultHttpRequest.setDecoderResult方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。