org.simpleframework.http.Response.setContentLength()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(210)

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

Response.setContentLength介绍

[英]This should be used when the size of the message body is known. This ensures that Persistent HTTP (PHTTP) connections can be maintained for both HTTP/1.0 and HTTP/1.1 clients. If the length of the output is not known HTTP/1.0 clients will require a connection close, which reduces performance (see RFC 2616).

This removes any previous Content-Length headers from the message header. This will then set the appropriate Content-Length header with the correct length. If a the Connection header is set with the close token then the semantics of the connection are such that the server will close it once the output stream or request is closed.
[中]当消息正文的大小已知时,应使用此选项。这确保了HTTP/1.0和HTTP/1.1客户端都可以保持持久HTTP(PHTTP)连接。如果输出的长度未知,HTTP/1.0客户端将需要关闭连接,这会降低性能(请参阅RFC 2616)。
这将从消息头中删除任何以前的内容长度头。然后,这将使用正确的长度设置适当的内容长度标题。如果使用close标记设置了连接头,那么连接的语义是,一旦输出流或请求关闭,服务器就会关闭它。

代码示例

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

@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength,
                         final ContainerResponse context) throws ContainerException {
  final javax.ws.rs.core.Response.StatusType statusInfo = context.getStatusInfo();
  final int code = statusInfo.getStatusCode();
  final String reason = statusInfo.getReasonPhrase() == null
      ? Status.getDescription(code)
      : statusInfo.getReasonPhrase();
  response.setCode(code);
  response.setDescription(reason);
  if (contentLength != -1) {
    response.setContentLength(contentLength);
  }
  for (final Map.Entry<String, List<String>> e : context.getStringHeaders().entrySet()) {
    for (final String value : e.getValue()) {
      response.addValue(e.getKey(), value);
    }
  }
  try {
    return response.getOutputStream();
  } catch (final IOException ioe) {
    throw new ContainerException("Error during writing out the response headers.", ioe);
  }
}

代码示例来源:origin: CodeStory/fluent-http

@Override
public void setContentLength(long length) {
 response.setContentLength(length);
}

代码示例来源:origin: org.simpleframework/simple

/**
* This should be used when the size of the message body is known. For 
* performance reasons this should be used so the length of the output
* is known. This ensures that Persistent HTTP (PHTTP) connections 
* can be maintained for both HTTP/1.0 and HTTP/1.1 clients. If the
* length of the output is not known HTTP/1.0 clients will require a
* connection close, which reduces performance (see RFC 2616).
* <p>
* This removes any previous Content-Length headers from the message 
* header. This will then set the appropriate Content-Length header with
* the correct length. If a the Connection header is set with the close
* token then the semantics of the connection are such that the server
* will close it once the <code>OutputStream.close</code> is used.
*
* @param length this is the length of the HTTP message body
*/ 
public void setContentLength(long length) {
 response.setContentLength(length);
}

代码示例来源:origin: org.simpleframework/simple-http

/**
* This should be used when the size of the message body is known. For 
* performance reasons this should be used so the length of the output
* is known. This ensures that Persistent HTTP (PHTTP) connections 
* can be maintained for both HTTP/1.0 and HTTP/1.1 clients. If the
* length of the output is not known HTTP/1.0 clients will require a
* connection close, which reduces performance (see RFC 2616).
* <p>
* This removes any previous Content-Length headers from the message 
* header. This will then set the appropriate Content-Length header with
* the correct length. If a the Connection header is set with the close
* token then the semantics of the connection are such that the server
* will close it once the <code>OutputStream.close</code> is used.
*
* @param length this is the length of the HTTP message body
*/ 
public void setContentLength(long length) {
 response.setContentLength(length);
}

代码示例来源:origin: ngallagher/simpleframework

/**
* This should be used when the size of the message body is known. For 
* performance reasons this should be used so the length of the output
* is known. This ensures that Persistent HTTP (PHTTP) connections 
* can be maintained for both HTTP/1.0 and HTTP/1.1 clients. If the
* length of the output is not known HTTP/1.0 clients will require a
* connection close, which reduces performance (see RFC 2616).
* <p>
* This removes any previous Content-Length headers from the message 
* header. This will then set the appropriate Content-Length header with
* the correct length. If a the Connection header is set with the close
* token then the semantics of the connection are such that the server
* will close it once the <code>OutputStream.close</code> is used.
*
* @param length this is the length of the HTTP message body
*/ 
public void setContentLength(long length) {
 response.setContentLength(length);
}

代码示例来源:origin: miltonio/milton2

@Override
  public void setContentLengthHeader(Long totalLength) {
    if( totalLength != null ) {
      int i = (int) totalLength.longValue();
      baseResponse.setContentLength(i);		
    }
//        String s = totalLength==null ? null : totalLength.toString();        
//        setResponseHeader( Header.CONTENT_LENGTH,s);
  }

代码示例来源:origin: opendedup/sdfs

private void downloadFile(File f, Request request, Response response) throws IOException {
  if (f.exists()) {
    response.setContentType("application/octet-stream");
    response.addValue("Server", "SDFS Management Server");
    response.setContentLength(f.length());
    InputStream in = new FileInputStream(f);
    OutputStream out = response.getOutputStream();
    byte[] buf = new byte[32768];
    int len;
    while ((len = in.read(buf)) > 0) {
      out.write(buf, 0, len);
    }
    out.flush();
    in.close();
    out.close();
  } else {
    response.setCode(404);
    PrintStream body = response.getPrintStream();
    body.println("could not find " + f.getPath());
    body.close();
    SDFSLogger.getLog().warn("unable to find " + f.getPath());
  }
}

代码示例来源:origin: com.sun.jersey.contribs/jersey-simple-server

public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse cResponse) throws IOException {
  int code = cResponse.getStatus();
  String text = Status.getDescription(code);
  String method = request.getMethod();
  response.setCode(code);
  response.setDescription(text);
  if (!method.equalsIgnoreCase("HEAD") && contentLength != -1 && contentLength < Integer.MAX_VALUE) {
    response.setContentLength((int) contentLength);
  }
  for (Map.Entry<String, List<Object>> e : cResponse.getHttpHeaders().entrySet()) {
    for (Object value : e.getValue()) {
      response.setValue(e.getKey(), ContainerResponse.getHeaderValue(value));
    }
  }
  return response.getOutputStream();
}

代码示例来源:origin: opendedup/sdfs

rsp.setContentLength(rb.length);
rsp.getOutputStream().write(rb);
rsp.getOutputStream().flush();

代码示例来源:origin: opendedup/sdfs

rsp.setDate("Date", time);
  rsp.setDate("Last-Modified", time);
  rsp.setContentLength(k.length);
  OutputStream out = rsp.getOutputStream();
  IOUtils.write(k, out);
rsp.setContentLength(rb.length);
rsp.getOutputStream().write(rb);
rsp.getOutputStream().flush();

代码示例来源:origin: opendedup/sdfs

rsp.setContentType("text/xml");
byte[] rb = rsString.getBytes();
rsp.setContentLength(rb.length);
try {
  rsp.getOutputStream().write(rb);

代码示例来源:origin: org.glassfish.jersey.containers/jersey-container-simple-http

@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength,
                         final ContainerResponse context) throws ContainerException {
  final javax.ws.rs.core.Response.StatusType statusInfo = context.getStatusInfo();
  final int code = statusInfo.getStatusCode();
  final String reason = statusInfo.getReasonPhrase() == null
      ? Status.getDescription(code)
      : statusInfo.getReasonPhrase();
  response.setCode(code);
  response.setDescription(reason);
  if (contentLength != -1) {
    response.setContentLength(contentLength);
  }
  for (final Map.Entry<String, List<String>> e : context.getStringHeaders().entrySet()) {
    for (final String value : e.getValue()) {
      response.addValue(e.getKey(), value);
    }
  }
  try {
    return response.getOutputStream();
  } catch (final IOException ioe) {
    throw new ContainerException("Error during writing out the response headers.", ioe);
  }
}

代码示例来源:origin: opendedup/sdfs

response.setContentLength(rb.length);
response.getOutputStream().write(rb);
response.getOutputStream().flush();
  response.setContentLength(rb.length);
  response.getOutputStream().write(rb);
  response.getOutputStream().flush();
  } else {
    MetaFileStore.getMF(path).sync();
    response.setContentLength(f.length());
    this.downloadFile(f, request, response);
  response.setContentLength(f.length());
  this.downloadFile(f, request, response);
} else if (request.getTarget().startsWith(BLOCK_PATH)) {
  response.setDate("Date", time);
  response.setDate("Last-Modified", time);
  response.setContentLength(data.length);
  response.getByteChannel().write(ByteBuffer.wrap(data));
  response.getByteChannel().close();
  response.setDate("Date", time);
  response.setDate("Last-Modified", time);
  response.setContentLength(rslt.length);
  response.getByteChannel().write(ByteBuffer.wrap(rslt));
  response.getByteChannel().close();

代码示例来源:origin: org.restlet/org.restlet.ext.simple

@Override
  public void writeResponseHead(org.restlet.data.Response restletResponse)
      throws IOException {
    // this.response.clear();
    for (final Parameter header : getResponseHeaders()) {
      this.response.add(header.getName(), header.getValue());
    }

    // Set the status
    this.response.setCode(getStatusCode());
    this.response.setText(getReasonPhrase());

    // Is this really required
    if (restletResponse.getEntity() == null) {
      this.response.setContentLength(0);
    }
  }
}

相关文章