本文整理匯總了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;
}
};
}
示例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() );
}
}
示例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() );
}
}
示例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);
}
}
示例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;
}
}
示例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;
}
示例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];
}
}
示例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
}
}
示例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 );
}
示例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";
}
}
示例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;
}
示例12: contentType
import java.net.URLConnection; //導入方法依賴的package包/類
private static String contentType(URL u) throws Exception {
URLConnection conn = u.openConnection();
return conn.getContentType();
}
示例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;
}
示例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();
}
示例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;
}
}