java.net.HttpURLConnection.setChunkedStreamingMode()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(728)

本文整理了Java中java.net.HttpURLConnection.setChunkedStreamingMode()方法的一些代码示例,展示了HttpURLConnection.setChunkedStreamingMode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.setChunkedStreamingMode()方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:setChunkedStreamingMode

HttpURLConnection.setChunkedStreamingMode介绍

[英]Stream a request body whose length is not known in advance. Old HTTP/1.0 only servers may not support this mode.

When HTTP chunked encoding is used, the stream is divided into chunks, each prefixed with a header containing the chunk's size. Setting a large chunk length requires a large internal buffer, potentially wasting memory. Setting a small chunk length increases the number of bytes that must be transmitted because of the header on every chunk. Most caller should use 0 to get the system default.
[中]流一个长度事先未知的请求主体。旧的仅HTTP/1.0服务器可能不支持此模式。
当使用HTTP分块编码时,流被分为多个块,每个块前面都有一个包含块大小的头。设置较大的块长度需要较大的内部缓冲区,这可能会浪费内存。设置小数据块长度会增加必须传输的字节数,因为每个数据块上都有标头。大多数调用方应使用0来获取系统默认值。

代码示例

代码示例来源:origin: square/okhttp

@Override public void setChunkedStreamingMode(int chunkLength) {
  delegate.setChunkedStreamingMode(chunkLength);
 }
}

代码示例来源:origin: prestodb/presto

@Override public void setChunkedStreamingMode(int chunkLength) {
  delegate.setChunkedStreamingMode(chunkLength);
 }
}

代码示例来源:origin: looly/hutool

/**
 * 采用流方式上传数据,无需本地缓存数据。<br>
 * HttpUrlConnection默认是将所有数据读到本地缓存,然后再发送给服务器,这样上传大文件时就会导致内存溢出。
 * 
 * @param blockSize 块大小(bytes数)
 * @return this
 */
public HttpConnection setChunkedStreamingMode(int blockSize) {
  conn.setChunkedStreamingMode(blockSize);
  return this;
}

代码示例来源:origin: looly/hutool

/**
 * 采用流方式上传数据,无需本地缓存数据。<br>
 * HttpUrlConnection默认是将所有数据读到本地缓存,然后再发送给服务器,这样上传大文件时就会导致内存溢出。
 * 
 * @param blockSize 块大小(bytes数)
 * @return this
 */
public HttpConnection setChunkedStreamingMode(int blockSize) {
  conn.setChunkedStreamingMode(blockSize);
  return this;
}

代码示例来源:origin: facebook/facebook-android-sdk

private static HttpURLConnection createConnection(URL url) throws IOException {
  HttpURLConnection connection;
  connection = (HttpURLConnection) url.openConnection();
  connection.setRequestProperty(USER_AGENT_HEADER, getUserAgent());
  connection.setRequestProperty(ACCEPT_LANGUAGE_HEADER, Locale.getDefault().toString());
  connection.setChunkedStreamingMode(0);
  return connection;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
  if (this.body == null) {
    if (this.outputStreaming) {
      long contentLength = headers.getContentLength();
      if (contentLength >= 0) {
        this.connection.setFixedLengthStreamingMode(contentLength);
      }
      else {
        this.connection.setChunkedStreamingMode(this.chunkSize);
      }
    }
    SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
    this.connection.connect();
    this.body = this.connection.getOutputStream();
  }
  return StreamUtils.nonClosing(this.body);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
  if (this.body == null) {
    if (this.outputStreaming) {
      long contentLength = headers.getContentLength();
      if (contentLength >= 0) {
        this.connection.setFixedLengthStreamingMode(contentLength);
      }
      else {
        this.connection.setChunkedStreamingMode(this.chunkSize);
      }
    }
    SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
    this.connection.connect();
    this.body = this.connection.getOutputStream();
  }
  return StreamUtils.nonClosing(this.body);
}

代码示例来源:origin: gotev/android-upload-service

@Override
public HttpConnection setTotalBodyBytes(long totalBodyBytes, boolean isFixedLengthStreamingMode) {
  if (isFixedLengthStreamingMode) {
    if (android.os.Build.VERSION.SDK_INT >= 19) {
      mConnection.setFixedLengthStreamingMode(totalBodyBytes);
    } else {
      if (totalBodyBytes > Integer.MAX_VALUE)
        throw new RuntimeException("You need Android API version 19 or newer to "
            + "upload more than 2GB in a single request using "
            + "fixed size content length. Try switching to "
            + "chunked mode instead, but make sure your server side supports it!");
      mConnection.setFixedLengthStreamingMode((int) totalBodyBytes);
    }
  } else {
    mConnection.setChunkedStreamingMode(0);
  }
  return this;
}

代码示例来源:origin: jenkinsci/jenkins

con.setDoOutput(true); // request POST
con.setRequestMethod("POST");
con.setChunkedStreamingMode(0);
con.setRequestProperty("Content-type","application/octet-stream");
con.addRequestProperty("Session", uuid.toString());

代码示例来源:origin: Alluxio/alluxio

/**
  * Swift HTTP PUT request.
  *
  * @param access JOSS access object
  * @param objectName name of the object to create
  * @return SwiftOutputStream that will be used to upload data to Swift
  */
 public static SwiftOutputStream put(Access access, String objectName) throws IOException {
  LOG.debug("PUT method, object : {}", objectName);
  URL url = new URL(access.getPublicURL() + "/" + objectName);
  URLConnection connection = url.openConnection();
  if (!(connection instanceof HttpURLConnection)) {
   throw new IOException("Connection is not an instance of HTTP URL Connection");
  }

  HttpURLConnection httpCon = (HttpURLConnection) connection;
  httpCon.setRequestMethod("PUT");
  httpCon.addRequestProperty("X-Auth-Token", access.getToken());
  httpCon.addRequestProperty("Content-Type", "binary/octet-stream");
  httpCon.setDoInput(true);
  httpCon.setRequestProperty("Connection", "close");
  httpCon.setReadTimeout(HTTP_READ_TIMEOUT);
  httpCon.setRequestProperty("Transfer-Encoding", "chunked");
  httpCon.setDoOutput(true);
  httpCon.setChunkedStreamingMode(HTTP_CHUNK_STREAMING);
  httpCon.connect();
  return new SwiftOutputStream(httpCon);
 }
}

代码示例来源:origin: org.springframework/spring-web

@Override
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
  if (this.body == null) {
    if (this.outputStreaming) {
      long contentLength = headers.getContentLength();
      if (contentLength >= 0) {
        this.connection.setFixedLengthStreamingMode(contentLength);
      }
      else {
        this.connection.setChunkedStreamingMode(this.chunkSize);
      }
    }
    SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
    this.connection.connect();
    this.body = this.connection.getOutputStream();
  }
  return StreamUtils.nonClosing(this.body);
}

代码示例来源:origin: org.springframework/spring-web

@Override
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
  if (this.body == null) {
    if (this.outputStreaming) {
      long contentLength = headers.getContentLength();
      if (contentLength >= 0) {
        this.connection.setFixedLengthStreamingMode(contentLength);
      }
      else {
        this.connection.setChunkedStreamingMode(this.chunkSize);
      }
    }
    SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers);
    this.connection.connect();
    this.body = this.connection.getOutputStream();
  }
  return StreamUtils.nonClosing(this.body);
}

代码示例来源:origin: jphp-group/jphp

@Signature
public void setChunkedStreamingMode(int chunklen) throws IOException {
  ((HttpURLConnection)getWrappedObject()).setChunkedStreamingMode(chunklen);
}

代码示例来源:origin: apache/activemq

public URL uploadStream(ActiveMQBlobMessage message, InputStream fis) throws JMSException, IOException {
  URL url = createMessageURL(message);
  HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("PUT");
  connection.setDoOutput(true);
  // use chunked mode or otherwise URLConnection loads everything into
  // memory
  // (chunked mode not supported before JRE 1.5)
  connection.setChunkedStreamingMode(transferPolicy.getBufferSize());
  try(OutputStream os = connection.getOutputStream()) {
    byte[] buf = new byte[transferPolicy.getBufferSize()];
    for (int c = fis.read(buf); c != -1; c = fis.read(buf)) {
      os.write(buf, 0, c);
      os.flush();
    }
  } catch (IOException error) {
    throw new IOException("PUT failed to: " + url, error);
  }
  if (!isSuccessfulCode(connection.getResponseCode())) {
    throw new IOException("PUT to " + url + " was not successful: " + connection.getResponseCode() + " "
               + connection.getResponseMessage());
  }
  return url;
}

代码示例来源:origin: stackoverflow.com

connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024);

代码示例来源:origin: yanzhenjie/NoHttp

connection.setFixedLengthStreamingMode(contentLength);
else
  connection.setChunkedStreamingMode(256 * 1024);
headers.set(Headers.HEAD_KEY_CONTENT_LENGTH, Long.toString(contentLength));

代码示例来源:origin: jersey/jersey

uc.setChunkedStreamingMode(chunkSize);

代码示例来源:origin: jersey/jersey

uc.setChunkedStreamingMode(chunkSize);

代码示例来源:origin: org.glassfish.jersey.core/jersey-client

uc.setChunkedStreamingMode(chunkSize);

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/** {@inheritDoc} */
@Override
public void setChunkedStreamingMode(int chunklen) {
  wrappedUrlConnection.setChunkedStreamingMode(chunklen);
}

相关文章

HttpURLConnection类方法