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

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

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

URLConnection.getContentType介绍

[英]Returns the MIME-type of the content specified by the response header field content-type or null if type is unknown.
[中]返回响应头字段content type指定的内容的MIME类型,如果类型未知,则返回null。

代码示例

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

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class TestUrlOpener {

  public static void main(String[] args) throws IOException {
    URL url = new URL("http://localhost:8080/foobar");
    URLConnection hc = url.openConnection();
    hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

    System.out.println(hc.getContentType());
  }

}

代码示例来源:origin: javax.activation/activation

/**
 * Returns the value of the URL content-type header field.
 * It calls the URL's <code>URLConnection.getContentType</code> method
 * after retrieving a URLConnection object.
 * <i>Note: this method attempts to call the <code>openConnection</code>
 * method on the URL. If this method fails, or if a content type is not
 * returned from the URLConnection, getContentType returns
 * "application/octet-stream" as the content type.</i>
 *
 * @return the content type.
 */
public String getContentType() {
String type = null;
try {
  if (url_conn == null)
  url_conn = url.openConnection();
} catch (IOException e) { }

if (url_conn != null)
  type = url_conn.getContentType();
if (type == null)
  type = "application/octet-stream";

return type;
}

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

URL url = new URL("http://stackoverflow.com/questions/1381617");
URLConnection con = url.openConnection();
Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(con.getContentType());
/* If Content-Type doesn't match this pre-conception, choose default and 
 * hope for the best. */
String charset = m.matches() ? m.group(1) : "ISO-8859-1";
Reader r = new InputStreamReader(con.getInputStream(), charset);
StringBuilder buf = new StringBuilder();
while (true) {
 int ch = r.read();
 if (ch < 0)
  break;
 buf.append((char) ch);
}
String str = buf.toString();

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

final URLConnection conn = url.openConnection();
final String ct = conn.getContentType();
InputStream in = conn.getInputStream();

代码示例来源:origin: commons-io/commons-io

this.defaultEncoding = defaultEncoding;
final boolean lenient = true;
final String contentType = conn.getContentType();
final InputStream is = conn.getInputStream();
final BOMInputStream bom = new BOMInputStream(new BufferedInputStream(is, BUFFER_SIZE), false, BOMS);
final BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES);

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

URL url = new URL(imagePath);
URLConnection urlConnection = url.openConnection();
inputStream = urlConnection.getInputStream();
contentType = urlConnection.getContentType();

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

URL url = new URL(urlname);
HttpURLConnection connection = (HttpURLConnection)  url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();

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

URLConnection connection = url.openConnection();
String type = connection.getContentType();
if (type != null) {
  metadata.set(Metadata.CONTENT_TYPE, type);
    new BufferedInputStream(connection.getInputStream()),
    new TemporaryResources(), length);

代码示例来源:origin: SeanDragon/protools

public static String getContentType(Path path) throws IOException {
  String type;
  URL u = path.toUri().toURL();
  URLConnection uc = u.openConnection();
  type = uc.getContentType();
  return type;
}

代码示例来源:origin: org.codehaus.plexus/plexus-utils

doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
    doLenientDetection( conn.getContentType(), ex );
else if ( conn.getContentType() != null )
    doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
    doLenientDetection( conn.getContentType(), ex );
    doRawStream( conn.getInputStream(), lenient );

代码示例来源:origin: pentaho/pentaho-kettle

server = new URL( urlToUse );
URLConnection connection = server.openConnection();
input = connection.getInputStream();
Date date = new Date( connection.getLastModified() );
logBasic( BaseMessages.getString( PKG, "JobHTTP.Log.ReplayInfo", connection.getContentType(), date ) );

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

if (sampleURL != null) {
  try {
    URL u = new URL(sampleURL);
    writer.println("Checking access to " + u);
    URLConnection c = u.openConnection();
    writer.println("Content type: " + c.getContentType());
    writer.println("Content length: " + c.getContentLength());
  } catch (Throwable e) {

代码示例来源:origin: webx/citrus

URLConnection connection = resource.getURL().openConnection();
String contentType = connection.getContentType();
istream = connection.getInputStream();

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * Returns the value of the URL content-type header field.
 * It calls the URL's <code>URLConnection.getContentType</code> method
 * after retrieving a URLConnection object.
 * <i>Note: this method attempts to call the <code>openConnection</code>
 * method on the URL. If this method fails, or if a content type is not
 * returned from the URLConnection, getContentType returns
 * "application/octet-stream" as the content type.</i>
 *
 * @return the content type.
 */
public String getContentType() {
String type = null;
try {
  if (url_conn == null)
  url_conn = url.openConnection();
} catch (IOException e) { }

if (url_conn != null)
  type = url_conn.getContentType();
if (type == null)
  type = "application/octet-stream";

return type;
}

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

/**
 * Returns an object representing the content of the resource this {@code
 * URLConnection} is connected to. First, it attempts to get the content
 * type from the method {@code getContentType()} which looks at the response
 * header field "Content-Type". If none is found it will guess the content
 * type from the filename extension. If that fails the stream itself will be
 * used to guess the content type.
 *
 * @return the content representing object.
 * @throws IOException
 *             if an error occurs obtaining the content.
 */
public Object getContent() throws java.io.IOException {
  if (!connected) {
    connect();
  }
  if ((contentType = getContentType()) == null) {
    if ((contentType = guessContentTypeFromName(url.getFile())) == null) {
      contentType = guessContentTypeFromStream(getInputStream());
    }
  }
  if (contentType != null) {
    return getContentHandler(contentType).getContent(this);
  }
  return null;
}

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

public Downloader(String path) throws IOException {
  int len = 0;
  URL url = new URL(path);
  URLConnection connectUrl = url.openConnection();
  System.out.println(len = connectUrl.getContentLength());
  System.out.println(connectUrl.getContentType());

  InputStream input = connectUrl.getInputStream();
  int i = len;
  int c = 0;
  System.out.println("=== Content ==="); 
  while (((c = input.read()) != -1) && (--i > 0)) {
    System.out.print((char) c);
  }
  input.close(); 
}

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

break;
URL url = new URL(base, plugin);
try {
  URLConnection connection = url.openConnection();
  String contentType = connection.getContentType();
  DetectorFactoryCollection.jawsDebugMessage("contentType : " + contentType);
  if (connection instanceof HttpURLConnection) {

代码示例来源:origin: webx/citrus

URLConnection connection = resource.getURL().openConnection();
String contentType = connection.getContentType();
istream = connection.getInputStream();

代码示例来源:origin: org.apache.solr/solr-common

public URLStream( URL url ) throws IOException {
 this.url = url; 
 this.conn = this.url.openConnection();
 
 contentType = conn.getContentType();
 name = url.toExternalForm();
 size = new Long( conn.getContentLength() );
 sourceInfo = "url";
}

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

if ((contentType = getContentType()) == null) {
  if ((contentType = guessContentTypeFromName(url.getFile())) == null) {
    contentType = guessContentTypeFromStream(getInputStream());

相关文章

URLConnection类方法