当前位置: 首页>>代码示例>>Java>>正文


Java URLConnection.getContentType方法代码示例

本文整理汇总了Java中java.net.URLConnection.getContentType方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.getContentType方法的具体用法?Java URLConnection.getContentType怎么用?Java URLConnection.getContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLConnection的用法示例。


在下文中一共展示了URLConnection.getContentType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createOkBody

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Creates an OkHttp Response.Body containing the supplied information.
 */
private static ResponseBody createOkBody(final URLConnection urlConnection) throws IOException {
  if (!urlConnection.getDoInput()) {
    return null;
  }

  final BufferedSource body = Okio.buffer(Okio.source(urlConnection.getInputStream()));
  return new ResponseBody() {
    @Override public MediaType contentType() {
      String contentTypeHeader = urlConnection.getContentType();
      return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
    }

    @Override public long contentLength() {
      String s = urlConnection.getHeaderField("Content-Length");
      return stringToLong(s);
    }

    @Override public BufferedSource source() {
      return body;
    }
  };
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:26,代码来源:JavaApiConverter.java

示例2: loadHeaders

import java.net.URLConnection; //导入方法依赖的package包/类
private void loadHeaders( URLConnection connection ) {
    if (HttpUnitOptions.isLoggingHttpHeaders()) {
        System.out.println( "Header:: " + connection.getHeaderField(0) );
    }
    for (int i = 1; true; i++) {
        String headerFieldKey = connection.getHeaderFieldKey( i );
        String headerField = connection.getHeaderField(i);
        if (headerFieldKey == null || headerField == null) break;
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
            System.out.println( "Header:: " + headerFieldKey + ": " + headerField );
        }
        addHeader( headerFieldKey.toUpperCase(), headerField );
    }

    if (connection.getContentType() != null) {
        setContentTypeHeader( connection.getContentType() );
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:19,代码来源:HttpWebResponse.java

示例3: loadHeaders

import java.net.URLConnection; //导入方法依赖的package包/类
private void loadHeaders( URLConnection connection ) {
    if (HttpUnitOptions.isLoggingHttpHeaders()) {
        System.out.println( "Header:: " + connection.getHeaderField(0) );
    }
    for (int i = 1; true; i++) {
        String key = connection.getHeaderFieldKey( i );
        if (key == null) break;
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
            System.out.println( "Header:: " + connection.getHeaderFieldKey( i ) + ": " + connection.getHeaderField(i) );
        }
        addHeader( connection.getHeaderFieldKey( i ).toUpperCase(), connection.getHeaderField( i ) );
    }

    if (connection.getContentType() != null) {
        setContentTypeHeader( connection.getContentType() );
    }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:18,代码来源:HttpWebResponse.java

示例4: initialiseResponse

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Initialise response
 * 
 * @param urlConnection  url connection
 */
protected void initialiseResponse(URLConnection urlConnection)
{
    String type = urlConnection.getContentType();
    if (type != null)
    {
        int encodingIdx = type.lastIndexOf("charset=");
        if (encodingIdx == -1)
        {
            String encoding = urlConnection.getContentEncoding();
            if (encoding != null && encoding.length() > 0)
            {
                type += ";charset=" + encoding;
            }
        }
        
        response.setContentType(type);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:24,代码来源:HTTPProxy.java

示例5: doInBackground

import java.net.URLConnection; //导入方法依赖的package包/类
@Override
protected Boolean doInBackground(Void... voids) {
    String contentType = "";
    URLConnection connection = null;
    try {
        connection = new URL(mStreamUri).openConnection();
        connection.connect();
        contentType = connection.getContentType();
        LogHelper.v(LOG_TAG, "MIME type of stream: " + contentType);
        if (contentType != null && (contentType.contains("application/vnd.apple.mpegurl") || contentType.contains("application/x-mpegurl"))) {
            LogHelper.v(LOG_TAG, "HTTP Live Streaming detected.");
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}
 
开发者ID:sanjaysingh1990,项目名称:Exoplayer2Radio,代码行数:21,代码来源:PlayerService.java

示例6: getMimeType

import java.net.URLConnection; //导入方法依赖的package包/类
private String getMimeType(URLConnection conn, String fileName) {
    String mimeType = null;
    if (fileName.indexOf('.') == -1) {
        mimeType = conn.getContentType();
    }

    return mimeType;
}
 
开发者ID:philipwhiuk,项目名称:q-mail,代码行数:9,代码来源:DownloadImageTask.java

示例7: children

import java.net.URLConnection; //导入方法依赖的package包/类
public String[] children(String f) {
    String fSlash = f.length() > 0 ? f + "/" : ""; // NOI18N
    try {
        URL url = new URL(baseURL, Utilities.uriEncode(fSlash) + "*plain*"); // NOI18N
        URLConnection conn = new ConnectionBuilder().job(job).url(url).timeout(TIMEOUT).connection();
        String contentType = conn.getContentType();
        if (contentType == null || !contentType.startsWith("text/plain")) { // NOI18N
            // Missing workspace.
            LOG.log(Level.FINE, "non-plain dir listing: {0}", url);
            return new String[0];
        }
        java.util.List<String> kids = new ArrayList<String>();
        InputStream is = conn.getInputStream();
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8")); // NOI18N
            String line;
            while ((line = r.readLine()) != null) {
                if (line.endsWith("/")) { // NOI18N
                    String n = line.substring(0, line.length() - 1);
                    kids.add(n);
                } else {
                    kids.add(line);
                    synchronized (nonDirs) {
                        nonDirs.add(fSlash + line);
                    }
                }
            }
        } finally {
            is.close();
        }
        LOG.log(Level.FINE, "children: {0} -> {1}", new Object[] {url, kids});
        return kids.toArray(new String[kids.size()]);
    } catch (IOException x) {
        LOG.log(Level.FINE, "cannot list children of {0} in {1}: {2}", new Object[] {f, baseURL, x});
        return new String[0];
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:38,代码来源:HudsonRemoteFileSystem.java

示例8: User

import java.net.URLConnection; //导入方法依赖的package包/类
public User() throws Exception {
    URLClassLoader ncl = (URLClassLoader)getClass().getClassLoader();
    URL[] urls = ncl.getURLs();
    if (urls.length != 1) throw new IllegalStateException("Weird URLs: " + Arrays.asList(urls));
    URL manual = new URL(urls[0], "org/openide/execution/data/foo.xml");
    URLConnection uc = manual.openConnection();
    uc.connect();
    String ct = uc.getContentType();
    /* May now be a file: URL, in which case content type is hard to control:
    if (!"text/xml".equals(ct)) throw new IllegalStateException("Wrong content type (manual): " + ct);
     */
    URL auto = getClass().getResource("data/foo.xml");
    if (auto == null) throw new IllegalStateException("Could not load data/foo.xml; try uncommenting se.printStackTrace() in MySecurityManager.checkPermission");
    uc = auto.openConnection();
    uc.connect();
    ct = uc.getContentType();
    /* Ditto:
    if (!"text/xml".equals(ct)) throw new IllegalStateException("Wrong content type (auto): " + ct);
     */
    // Ensure this class does *not* have free access to random permissions:
    try {
        System.getProperty("foo");
        throw new IllegalStateException("Was permitted to access sys prop foo");
    } catch (SecurityException se) {
        // good
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:28,代码来源:NbClassLoaderTest.java

示例9: makeData

import java.net.URLConnection; //导入方法依赖的package包/类
@Override
protected Data makeData () throws Exception
{
    final URLConnection connection = this.url.openConnection ();
    connection.connect ();

    final int len = connection.getContentLength ();

    final ByteArrayOutputStream bos = new ByteArrayOutputStream ( len > 0 ? len : 0 );

    final byte[] buffer = new byte[4096];
    try ( InputStream stream = connection.getInputStream () )
    {
        int rc;
        while ( ( rc = stream.read ( buffer ) ) > 0 )
        {
            bos.write ( buffer, 0, rc );
        }
        bos.close ();
    }

    final String encoding = connection.getContentEncoding ();
    final String type = connection.getContentType ();

    logger.debug ( "Content-Encoding: {}", encoding );
    logger.debug ( "Content-Type: {}", type );

    Charset charset = null;

    if ( this.charset != null )
    {
        charset = this.charset;
    }
    else if ( this.probeCharset )
    {
        charset = makeCharsetFromType ( type );
    }

    return new UrlConnectionData ( convert ( buffer, charset ), null );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:41,代码来源:UrlConnectionInput.java

示例10: getCharacterEncoding

import java.net.URLConnection; //导入方法依赖的package包/类
private static String getCharacterEncoding(URLConnection urlConnection) {
    final ParsedContentType pct = new ParsedContentType(
            urlConnection.getContentType());
    final String encoding = pct.getEncoding();
    if(encoding != null) {
        return encoding;
    }
    final String contentType = pct.getContentType();
    if(contentType != null && contentType.startsWith("text/")) {
        return "8859_1";
    }
    else {
        return "utf-8";
    }
}
 
开发者ID:MikaGuraN,项目名称:HL4A,代码行数:16,代码来源:UrlModuleSourceProvider.java

示例11: isSupportedINSFile

import java.net.URLConnection; //导入方法依赖的package包/类
private static boolean isSupportedINSFile(URLConnection paramURLConnection) {
	boolean bool = false;
	String str1 = paramURLConnection.getURL().getFile();
	if ((str1 != null) && (str1.toLowerCase().endsWith(".ins") == true)) {
		bool = true;
	} else if (paramURLConnection != null) {
		String str2 = paramURLConnection.getContentType();
		bool = "application/x-internet-signup".equalsIgnoreCase(str2);
	}
	return bool;
}
 
开发者ID:kmarius,项目名称:xdman,代码行数:12,代码来源:AbstractAutoProxyHandler.java

示例12: contentType

import java.net.URLConnection; //导入方法依赖的package包/类
private static String contentType(URL u) throws Exception {
    URLConnection conn = u.openConnection();
    return conn.getContentType();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:5,代码来源:NbURLStreamHandlerFactoryTest.java

示例13: save

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * 上传文件
 * 
 * @throws IllegalStateException
 * @throws IOException
 */
protected void save() throws IllegalStateException, IOException {

	// 构造URL
	URL url = new URL(fileUrl);

	// 打开连接
	URLConnection con = url.openConnection();

	// 设置请求超时为5s
	con.setConnectTimeout(5 * 1000);

	// 输入流
	InputStream is = con.getInputStream();

	// 1K的数据缓冲
	byte[] bs = new byte[1024];

	// 读取到的数据长度
	int len;

	// 获取文件mime类型
	contentType = con.getContentType();

	// 通过Mime类型获取文件类型
	fileSuffix = ContentTypeUtil.getFileTypeByMimeType(contentType);

	// 路径
	path = UploadUtil.getPath(fileSuffix);

	// 文件名
	filename = UploadUtil.getFileName();

	// 全路径
	filePath = path + filename + "." + fileSuffix;

	// 访问地址
	webURL = UploadUtil.getURL(fileSuffix) + filename + "." + fileSuffix;

	// 输出的文件流
	OutputStream os = new FileOutputStream(filePath);

	// 开始读取
	while ((len = is.read(bs)) != -1) {
		os.write(bs, 0, len);
	}

	// 完毕,关闭所有链接
	os.close();
	is.close();
	
	isUpload = true;
}
 
开发者ID:ZiryLee,项目名称:FileUpload2Spring,代码行数:59,代码来源:DownloadEntityImp.java

示例14: getContentType

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Returns the content type of this URL connection.
 * @return the content type
 */
protected String getContentType(
  URLConnection conn)
{
  return conn.getContentType();
}
 
开发者ID:apache,项目名称:myfaces-trinidad,代码行数:10,代码来源:ResourceLoader.java

示例15: queryResolver

import java.net.URLConnection; //导入方法依赖的package包/类
/**
 * Query an external RFC2483 resolver.
 *
 * @param resolver The URL of the RFC2483 resolver.
 * @param command The command to send the resolver.
 * @param arg1 The first argument to the resolver.
 * @param arg2 The second argument to the resolver, usually null.
 *
 * @return The Resolver constructed.
 */
protected Resolver queryResolver(String resolver,
                                 String command,
                                 String arg1,
                                 String arg2) {
    InputStream iStream = null;
    String RFC2483 = resolver + "?command=" + command
        + "&format=tr9401&uri=" + arg1
        + "&uri2=" + arg2;
    String line = null;

    try {
        URL url = new URL(RFC2483);

        URLConnection urlCon = url.openConnection();

        urlCon.setUseCaches(false);

        Resolver r = (Resolver) newCatalog();

        String cType = urlCon.getContentType();

        // I don't care about the character set or subtype
        if (cType.indexOf(";") > 0) {
            cType = cType.substring(0, cType.indexOf(";"));
        }

        r.parseCatalog(cType, urlCon.getInputStream());

        return r;
    } catch (CatalogException cex) {
      if (cex.getExceptionType() == CatalogException.UNPARSEABLE) {
        catalogManager.debug.message(1, "Unparseable catalog: " + RFC2483);
      } else if (cex.getExceptionType()
                 == CatalogException.UNKNOWN_FORMAT) {
        catalogManager.debug.message(1, "Unknown catalog format: " + RFC2483);
      }
      return null;
    } catch (MalformedURLException mue) {
        catalogManager.debug.message(1, "Malformed resolver URL: " + RFC2483);
        return null;
    } catch (IOException ie) {
        catalogManager.debug.message(1, "I/O Exception opening resolver: " + RFC2483);
        return null;
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:56,代码来源:Resolver.java


注:本文中的java.net.URLConnection.getContentType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。