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

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

本文整理了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

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

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

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

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

  1. URLConnection connection = url.openConnection();
  2. InputStream input = connection.getInputStream();
  3. if ("gzip".equals(connection.getContentEncoding())) {
  4. input = new GZIPInputStream(input);
  5. }
  6. // ...

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

  1. URLConnection connection = url.openConnection();
  2. InputStream stream = connection.getInputStream();
  3. if ("gzip".equals(connection.getContentEncoding())) {
  4. stream = new GZIPInputStream(stream));
  5. }
  6. InputSource is = new InputSource(stream);

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

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

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

  1. URL url = new URL(urlStr);
  2. HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail
  3. HttpURLConnection.setFollowRedirects(true);
  4. // allow both GZip and Deflate (ZLib) encodings
  5. conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
  6. String encoding = conn.getContentEncoding();
  7. InputStream inStr = null;
  8. // create the appropriate stream wrapper based on
  9. // the encoding type
  10. if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
  11. inStr = new GZIPInputStream(conn.getInputStream());
  12. } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
  13. inStr = new InflaterInputStream(conn.getInputStream(),
  14. new Inflater(true));
  15. } else {
  16. inStr = conn.getInputStream();
  17. }

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

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

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

  1. String encoding = conn.getContentEncoding();

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

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

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

  1. private int pump(OutputStream output, URLConnection connection) throws IOException {
  2. final int dataLength;
  3. final CounterInputStream counterInputStream = new CounterInputStream(
  4. connection.getInputStream());
  5. InputStream input = counterInputStream;
  6. try {
  7. if ("gzip".equals(connection.getContentEncoding())) {
  8. input = new GZIPInputStream(input);
  9. }
  10. InputOutput.pump(input, output);
  11. } finally {
  12. try {
  13. input.close();
  14. } finally {
  15. close(connection);
  16. dataLength = counterInputStream.getDataLength();
  17. }
  18. }
  19. return dataLength;
  20. }

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

  1. /**
  2. * Get the class of the scriptName in question, so that you can instantiate
  3. * Groovy objects with caching and reloading.
  4. *
  5. * @param scriptName resource name pointing to the script
  6. * @return the loaded scriptName as a compiled class
  7. * @throws ResourceException if there is a problem accessing the script
  8. * @throws ScriptException if there is a problem parsing the script
  9. */
  10. public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException {
  11. URLConnection conn = rc.getResourceConnection(scriptName);
  12. String path = conn.getURL().toExternalForm();
  13. ScriptCacheEntry entry = scriptCache.get(path);
  14. Class clazz = null;
  15. if (entry != null) clazz = entry.scriptClass;
  16. try {
  17. if (isSourceNewer(entry)) {
  18. try {
  19. String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : config.getSourceEncoding();
  20. String content = IOGroovyMethods.getText(conn.getInputStream(), encoding);
  21. clazz = groovyLoader.parseClass(content, path);
  22. } catch (IOException e) {
  23. throw new ResourceException(e);
  24. }
  25. }
  26. } finally {
  27. forceClose(conn);
  28. }
  29. return clazz;
  30. }

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

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

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

  1. @Override
  2. public String getEncoding() {
  3. return con.getContentEncoding();
  4. }

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

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

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

  1. private static InputStream getInputStream(URLConnection urlConnection) throws IOException {
  2. if ("gzip".equals(urlConnection.getContentEncoding())) {
  3. return new GZIPInputStream(urlConnection.getInputStream());
  4. }
  5. return urlConnection.getInputStream();
  6. }

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

  1. URL url = new URL("http://www.example.com/");
  2. URLConnection con = url.openConnection();
  3. InputStream in = con.getInputStream();
  4. 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
  5. encoding = encoding == null ? "UTF-8" : encoding;
  6. String body = IOUtils.toString(in, encoding);
  7. System.out.println(body);

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

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

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

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

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

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

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

  1. private JSONObject getJSONObject(URLConnection conn)
  2. throws IOException, JSONException {
  3. try(InputStream in = conn.getInputStream()) {
  4. String encoding = conn.getContentEncoding();
  5. encoding = encoding == null ? "UTF-8" : encoding;
  6. String body = IOUtils.toString(in, encoding);
  7. JSONObject obj = new JSONObject(body);
  8. JSONObject clusterInfo = obj.getJSONObject("clusterInfo");
  9. return clusterInfo;
  10. }
  11. }

相关文章

URLConnection类方法