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

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

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

  1. import java.io.IOException;
  2. import java.net.URL;
  3. import java.net.URLConnection;
  4. public class TestUrlOpener {
  5. public static void main(String[] args) throws IOException {
  6. URL url = new URL("http://localhost:8080/foobar");
  7. URLConnection hc = url.openConnection();
  8. 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");
  9. System.out.println(hc.getContentType());
  10. }
  11. }

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

  1. /**
  2. * Returns the value of the URL content-type header field.
  3. * It calls the URL's <code>URLConnection.getContentType</code> method
  4. * after retrieving a URLConnection object.
  5. * <i>Note: this method attempts to call the <code>openConnection</code>
  6. * method on the URL. If this method fails, or if a content type is not
  7. * returned from the URLConnection, getContentType returns
  8. * "application/octet-stream" as the content type.</i>
  9. *
  10. * @return the content type.
  11. */
  12. public String getContentType() {
  13. String type = null;
  14. try {
  15. if (url_conn == null)
  16. url_conn = url.openConnection();
  17. } catch (IOException e) { }
  18. if (url_conn != null)
  19. type = url_conn.getContentType();
  20. if (type == null)
  21. type = "application/octet-stream";
  22. return type;
  23. }

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

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

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

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

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

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

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

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

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

  1. URL url = new URL(urlname);
  2. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  3. connection.setRequestMethod("HEAD");
  4. connection.connect();
  5. String contentType = connection.getContentType();

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

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

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

  1. public static String getContentType(Path path) throws IOException {
  2. String type;
  3. URL u = path.toUri().toURL();
  4. URLConnection uc = u.openConnection();
  5. type = uc.getContentType();
  6. return type;
  7. }

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

  1. doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
  2. doLenientDetection( conn.getContentType(), ex );
  3. else if ( conn.getContentType() != null )
  4. doHttpStream( conn.getInputStream(), conn.getContentType(), lenient );
  5. doLenientDetection( conn.getContentType(), ex );
  6. doRawStream( conn.getInputStream(), lenient );

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

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

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

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

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

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

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

  1. /**
  2. * Returns the value of the URL content-type header field.
  3. * It calls the URL's <code>URLConnection.getContentType</code> method
  4. * after retrieving a URLConnection object.
  5. * <i>Note: this method attempts to call the <code>openConnection</code>
  6. * method on the URL. If this method fails, or if a content type is not
  7. * returned from the URLConnection, getContentType returns
  8. * "application/octet-stream" as the content type.</i>
  9. *
  10. * @return the content type.
  11. */
  12. public String getContentType() {
  13. String type = null;
  14. try {
  15. if (url_conn == null)
  16. url_conn = url.openConnection();
  17. } catch (IOException e) { }
  18. if (url_conn != null)
  19. type = url_conn.getContentType();
  20. if (type == null)
  21. type = "application/octet-stream";
  22. return type;
  23. }

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

  1. /**
  2. * Returns an object representing the content of the resource this {@code
  3. * URLConnection} is connected to. First, it attempts to get the content
  4. * type from the method {@code getContentType()} which looks at the response
  5. * header field "Content-Type". If none is found it will guess the content
  6. * type from the filename extension. If that fails the stream itself will be
  7. * used to guess the content type.
  8. *
  9. * @return the content representing object.
  10. * @throws IOException
  11. * if an error occurs obtaining the content.
  12. */
  13. public Object getContent() throws java.io.IOException {
  14. if (!connected) {
  15. connect();
  16. }
  17. if ((contentType = getContentType()) == null) {
  18. if ((contentType = guessContentTypeFromName(url.getFile())) == null) {
  19. contentType = guessContentTypeFromStream(getInputStream());
  20. }
  21. }
  22. if (contentType != null) {
  23. return getContentHandler(contentType).getContent(this);
  24. }
  25. return null;
  26. }

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

  1. public Downloader(String path) throws IOException {
  2. int len = 0;
  3. URL url = new URL(path);
  4. URLConnection connectUrl = url.openConnection();
  5. System.out.println(len = connectUrl.getContentLength());
  6. System.out.println(connectUrl.getContentType());
  7. InputStream input = connectUrl.getInputStream();
  8. int i = len;
  9. int c = 0;
  10. System.out.println("=== Content ===");
  11. while (((c = input.read()) != -1) && (--i > 0)) {
  12. System.out.print((char) c);
  13. }
  14. input.close();
  15. }

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

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

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

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

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

  1. public URLStream( URL url ) throws IOException {
  2. this.url = url;
  3. this.conn = this.url.openConnection();
  4. contentType = conn.getContentType();
  5. name = url.toExternalForm();
  6. size = new Long( conn.getContentLength() );
  7. sourceInfo = "url";
  8. }

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

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

相关文章

URLConnection类方法