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()); } }
/** * 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; }
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();
final URLConnection conn = url.openConnection(); final String ct = conn.getContentType(); InputStream in = conn.getInputStream();
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);
URL url = new URL(imagePath); URLConnection urlConnection = url.openConnection(); inputStream = urlConnection.getInputStream(); contentType = urlConnection.getContentType();
URL url = new URL(urlname); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("HEAD"); connection.connect(); String contentType = connection.getContentType();
URLConnection connection = url.openConnection(); String type = connection.getContentType(); if (type != null) { metadata.set(Metadata.CONTENT_TYPE, type); new BufferedInputStream(connection.getInputStream()), new TemporaryResources(), length);
public static String getContentType(Path path) throws IOException { String type; URL u = path.toUri().toURL(); URLConnection uc = u.openConnection(); type = uc.getContentType(); return type; }
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 );
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 ) );
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) {
URLConnection connection = resource.getURL().openConnection(); String contentType = connection.getContentType(); istream = connection.getInputStream();
/** * 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; }
/** * 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; }
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(); }
break; URL url = new URL(base, plugin); try { URLConnection connection = url.openConnection(); String contentType = connection.getContentType(); DetectorFactoryCollection.jawsDebugMessage("contentType : " + contentType); if (connection instanceof HttpURLConnection) {
URLConnection connection = resource.getURL().openConnection(); String contentType = connection.getContentType(); istream = connection.getInputStream();
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"; }
if ((contentType = getContentType()) == null) { if ((contentType = guessContentTypeFromName(url.getFile())) == null) { contentType = guessContentTypeFromStream(getInputStream());