Tabnine Logo
URLConnection.getContentType
Code IndexAdd Tabnine to your IDE (free)

How to use
getContentType
method
in
java.net.URLConnection

Best Java code snippets using java.net.URLConnection.getContentType (Showing top 20 results out of 2,421)

Refine searchRefine arrow

  • URLConnection.getInputStream
  • URL.openConnection
  • URL.<init>
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());
java.netURLConnectiongetContentType

Javadoc

Returns the MIME-type of the content specified by the response header field content-type or null if type is unknown.

Popular methods of URLConnection

  • getInputStream
    Returns an input stream that reads from this open connection. A SocketTimeoutException can be thrown
  • setUseCaches
    Sets the value of the useCaches field of thisURLConnection to the specified value. Some protocols do
  • connect
    Opens a communications link to the resource referenced by this URL, if such a connection has not alr
  • setRequestProperty
    Sets the general request property. If a property with the key already exists, overwrite its value wi
  • getOutputStream
    Returns an output stream that writes to this connection.
  • getLastModified
    Returns the value of the last-modified header field. The result is the number of milliseconds since
  • setConnectTimeout
    Sets a specified timeout value, in milliseconds, to be used when opening a communications link to th
  • setDoOutput
    Sets the value of the doOutput field for thisURLConnection to the specified value. A URL connection
  • getContentLength
    Returns the value of the content-length header field.Note: #getContentLengthLong()should be preferre
  • setReadTimeout
    Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeou
  • getHeaderField
    Returns the value of the named header field. If called on a connection that sets the same header mul
  • setDoInput
    Sets the value of the doInput field for thisURLConnection to the specified value. A URL connection c
  • getHeaderField,
  • setDoInput,
  • addRequestProperty,
  • getURL,
  • getContentEncoding,
  • guessContentTypeFromName,
  • setDefaultUseCaches,
  • getFileNameMap,
  • getContent

Popular in Java

  • Running tasks concurrently on multiple threads
  • onRequestPermissionsResult (Fragment)
  • startActivity (Activity)
  • getSystemService (Context)
  • Pointer (com.sun.jna)
    An abstraction for a native pointer data type. A Pointer instance represents, on the Java side, a na
  • FileWriter (java.io)
    A specialized Writer that writes to a file in the file system. All write requests made by calling me
  • Path (java.nio.file)
  • List (java.util)
    An ordered collection (also known as a sequence). The user of this interface has precise control ove
  • Set (java.util)
    A Set is a data structure which does not allow duplicate elements.
  • CountDownLatch (java.util.concurrent)
    A synchronization aid that allows one or more threads to wait until a set of operations being perfor
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now