java.net.URLConnection.getContentEncoding()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(283)

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

URLConnection.getContentEncoding介绍

[英]Returns the content encoding type specified by the response header field content-encoding or null if this field is not set.
[中]返回响应头字段content encoding指定的内容编码类型,如果未设置此字段,则返回null。

代码示例

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

/**
 * Returns the encoding used to transmit the response body over the network.
 * This is null or "identity" if the content was not encoded, or "gzip" if
 * the body was gzip compressed. Most callers will be more interested in the
 * {@link #getContentType() content type}, which may also include the
 * content's character encoding.
 */
@Override public String getContentEncoding() {
  return super.getContentEncoding(); // overridden for Javadoc only
}

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

URL url = new URL("http://www.example.com/");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
System.out.println(body);

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

URLConnection connection = url.openConnection();
InputStream input = connection.getInputStream();

if ("gzip".equals(connection.getContentEncoding())) {
  input = new GZIPInputStream(input);
}

// ...

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

URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
if ("gzip".equals(connection.getContentEncoding())) {
 stream = new GZIPInputStream(stream));
}
InputSource is = new InputSource(stream);

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

String contentEncoding = con.getContentEncoding() != null ? con.getContentEncoding() : "UTF-8";
  return IOUtils.toString(is, contentEncoding); //Apache Commons IO
} catch (Exception e) {

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

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail
HttpURLConnection.setFollowRedirects(true);
// allow both GZip and Deflate (ZLib) encodings
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
String encoding = conn.getContentEncoding();
InputStream inStr = null;

// create the appropriate stream wrapper based on
// the encoding type
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
  inStr = new GZIPInputStream(conn.getInputStream());
} else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
  inStr = new InflaterInputStream(conn.getInputStream(),
   new Inflater(true));
} else {
  inStr = conn.getInputStream();
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * TODO(jwagenleitner): remove or fix in future release
 *
 * According to the spec getContentEncoding() returns the Content-Encoding
 * HTTP Header which typically carries values such as 'gzip' or 'deflate'
 * and is not the character set encoding. For compatibility in 2.4.x,
 * this behavior is retained but should be removed or fixed (parse
 * charset from Content-Type header) in future releases.
 *
 * see GROOVY-8056 and https://github.com/apache/groovy/pull/500
 */
private static String getContentEncoding(URL url) throws IOException {
  URLConnection urlConnection = url.openConnection();
  String encoding = urlConnection.getContentEncoding();
  try {
    IOGroovyMethods.closeQuietly(urlConnection.getInputStream());
  } catch (IOException ignore) {
    // For compatibility, ignore exceptions from getInputStream() call
  }
  return encoding;
}

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

String encoding = conn.getContentEncoding();

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

InputStream input = inputStream;
try {
  if ("gzip".equals(connection.getContentEncoding())) {

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

private int pump(OutputStream output, URLConnection connection) throws IOException {
  final int dataLength;
  final CounterInputStream counterInputStream = new CounterInputStream(
      connection.getInputStream());
  InputStream input = counterInputStream;
  try {
    if ("gzip".equals(connection.getContentEncoding())) {
      input = new GZIPInputStream(input);
    }
    InputOutput.pump(input, output);
  } finally {
    try {
      input.close();
    } finally {
      close(connection);
      dataLength = counterInputStream.getDataLength();
    }
  }
  return dataLength;
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Get the class of the scriptName in question, so that you can instantiate
 * Groovy objects with caching and reloading.
 *
 * @param scriptName resource name pointing to the script
 * @return the loaded scriptName as a compiled class
 * @throws ResourceException if there is a problem accessing the script
 * @throws ScriptException   if there is a problem parsing the script
 */
public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException {
  URLConnection conn = rc.getResourceConnection(scriptName);
  String path = conn.getURL().toExternalForm();
  ScriptCacheEntry entry = scriptCache.get(path);
  Class clazz = null;
  if (entry != null) clazz = entry.scriptClass;
  try {
    if (isSourceNewer(entry)) {
      try {
        String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : config.getSourceEncoding();
        String content = IOGroovyMethods.getText(conn.getInputStream(), encoding);
        clazz = groovyLoader.parseClass(content, path);
      } catch (IOException e) {
        throw new ResourceException(e);
      }
    }
  } finally {
    forceClose(conn);
  }
  return clazz;
}

代码示例来源:origin: org.apache.ant/ant

&& GZIP_CONTENT_ENCODING.equals(connection.getContentEncoding())) {
is = new GZIPInputStream(is);

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Override
public String getEncoding() {
  return con.getContentEncoding();
}

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

public SimpleHTTPResponse(final URLConnection connection) throws IOException {
  this.connection = connection;
  InputStream inputStream = connection.getInputStream();
  final String contentEncoding = connection.getContentEncoding();
  if (contentEncoding != null && connection.getContentEncoding().indexOf("gzip") != -1) {
    inputStream = new GZIPInputStream(inputStream);
  }
  responseStream = inputStream;
}

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

private static InputStream getInputStream(URLConnection urlConnection) throws IOException {
  if ("gzip".equals(urlConnection.getContentEncoding())) {
    return new GZIPInputStream(urlConnection.getInputStream());
  }
  return urlConnection.getInputStream();
}

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

URL url = new URL("http://www.example.com/");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();  // ** WRONG: should use "con.getContentType()" instead but it returns something like "text/html; charset=UTF-8" so this value must be parsed to extract the actual encoding
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
System.out.println(body);

代码示例来源:origin: kohlschutter/boilerpipe

final String encoding = conn.getContentEncoding();
if (encoding != null) {
 if ("gzip".equalsIgnoreCase(encoding)) {

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

String encoding = connection.getContentEncoding();
if (encoding != null) {
  metadata.set(Metadata.CONTENT_ENCODING, encoding);

代码示例来源:origin: MobiVM/robovm

/**
 * Returns the encoding used to transmit the response body over the network.
 * This is null or "identity" if the content was not encoded, or "gzip" if
 * the body was gzip compressed. Most callers will be more interested in the
 * {@link #getContentType() content type}, which may also include the
 * content's character encoding.
 */
@Override public String getContentEncoding() {
  return super.getContentEncoding(); // overridden for Javadoc only
}

代码示例来源:origin: org.apache.hadoop/hadoop-yarn-client

private JSONObject getJSONObject(URLConnection conn)
  throws IOException, JSONException {
 try(InputStream in = conn.getInputStream()) {
  String encoding = conn.getContentEncoding();
  encoding = encoding == null ? "UTF-8" : encoding;
  String body = IOUtils.toString(in, encoding);
  JSONObject obj = new JSONObject(body);
  JSONObject clusterInfo = obj.getJSONObject("clusterInfo");
  return clusterInfo;
 }
}

相关文章

URLConnection类方法