HttpURLConnection connection = null; try { URL url = new URL(sUrl[0]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); int fileLength = connection.getContentLength(); input = connection.getInputStream(); output = new FileOutputStream("/sdcard/file_name.extension"); while ((count = input.read(data)) != -1) {
private void init() throws IOException { if(is == null && length == 0) { URLConnection con = url.openConnection(); is = con.getInputStream(); length = con.getContentLength(); } }
@Override public long contentLength() throws IOException { URL url = getURL(); if (ResourceUtils.isFileURL(url)) { // Proceed with file system resolution... return getFile().length(); } else { // Try a URL connection content-length header... URLConnection con = url.openConnection(); customizeConnection(con); return con.getContentLength(); } }
String s = System.getProperty("org.bytedeco.javacpp.cachedir.nosubdir", "false").toLowerCase(); boolean noSubdir = s.equals("true") || s.equals("t") || s.equals(""); URLConnection urlConnection = resourceURL.openConnection(); if (urlConnection instanceof JarURLConnection) { JarFile jarFile = ((JarURLConnection)urlConnection).getJarFile(); size = urlConnection.getContentLength(); timestamp = urlConnection.getLastModified(); if (!noSubdir) { logger.debug("Locking " + cacheDir + " to create symbolic link"); lockChannel = new FileOutputStream(lockFile).getChannel(); lock = lockChannel.lock(); if ((!file.exists() || !Files.isSymbolicLink(path) || !Files.readSymbolicLink(path).equals(targetPath)) logger.debug("Locking " + cacheDir + " to create symbolic link"); lockChannel = new FileOutputStream(lockFile).getChannel(); lock = lockChannel.lock(); if ((!file.exists() || !Files.isSymbolicLink(path) || !Files.readSymbolicLink(path).equals(urlPath)) logger.debug("Locking " + cacheDir + " before extracting"); lockChannel = new FileOutputStream(lockFile).getChannel(); lock = lockChannel.lock();
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) { e.printStackTrace(writer);
ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); try { URL url = new URL(urlToDownload); URLConnection connection = url.openConnection(); connection.connect(); int fileLength = connection.getContentLength(); InputStream input = new BufferedInputStream(connection.getInputStream()); OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk"); while ((count = input.read(data)) != -1) { total += count;
private int tryGetFileSize(URL url) { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); conn.getInputStream(); return conn.getContentLength(); } catch (IOException e) { return -1; } finally { conn.disconnect(); } }
public long contentLength() throws IOException { URL url = this.getURL(); if (ResourceUtils.isFileURL(url)) { return this.getFile().length(); } else { URLConnection con = url.openConnection(); ResourceUtils.useCachesIfNecessary(con); if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).setRequestMethod("HEAD"); } return (long) con.getContentLength(); } }
URL url = new URL("http://server.com/file.mp3"); URLConnection urlConnection = url.openConnection(); urlConnection.connect(); int file_size = urlConnection.getContentLength();
private FileInformation readFileInformation(URL url) { FileInformation fileInformation = new FileInformation(); URLConnection urlConnection; try { if (url.getProtocol().startsWith("http")) { urlConnection = url.openConnection(); ((HttpURLConnection) urlConnection).setRequestMethod("HEAD"); try (InputStream inputStream = urlConnection.getInputStream()) { fileInformation.length = urlConnection.getContentLength(); fileInformation.lastModified = urlConnection.getLastModified(); } } else { if (url.getProtocol().startsWith("file")) { File file = new File(url.getFile()); if (file.exists()) { fileInformation = new FileInformation(file); } } } } catch (IOException e) { if (LOG.isLoggable(Level.WARNING)) LOG.log(Level.WARNING, "Failed to obtain age and length of URL " + url, e); } return fileInformation; }
final URLConnection connection = this.url.openConnection(); if (connection.getContentLength() >= 0) { return true;
public static boolean hasInternetAccess(Context context) { if (isNetworkAvailable(context)) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204") .openConnection()); urlc.setRequestProperty("User-Agent", "Android"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); urlc.connect(); return (urlc.getResponseCode() == 204 && urlc.getContentLength() == 0); } catch (IOException e) { Log.e(TAG, "Error checking internet connection", e); } } else { Log.d(TAG, "No network available!"); } return false; }
@Override protected Class<?> findClass(final String name) throws ClassNotFoundException { final String path = name.replace('.', '/').concat(".class"); final URL resource = super.getResource(path); if (resource == null) { throw new ClassNotFoundException(name); } try { final URLConnection uc = resource.openConnection(); final int len = uc.getContentLength(); final InputStream in = new BufferedInputStream(uc.getInputStream()); final byte[] bytecode = new byte[len]; try { IOUtils.readFully(in, bytecode); } finally { Closer.closeSilently(in); } return defineClass(name, bytecode, 0, bytecode.length); } catch (final IOException e) { Throwables.rethrow(e); return null; // unreachable } } }
@Override public long contentLength() throws IOException { URL url = getURL(); if (ResourceUtils.isFileURL(url)) { // Proceed with file system resolution... return getFile().length(); } // Try a URL connection content-length header... URLConnection con = url.openConnection(); useCachesIfNecessary(con); if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).setRequestMethod("HEAD"); } return con.getContentLength(); }
URL url = new URL("http", server, port, "/" + classname.replace('.', '/') + ".class"); URLConnection con = url.openConnection(); con.connect(); int size = con.getContentLength(); InputStream s = con.getInputStream(); if (size <= 0) b = readStream(s); int len = 0; do { int n = s.read(b, len, size - len); if (n < 0) { s.close();
private int getFileSize(URL url) { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); conn.getInputStream(); return conn.getContentLength(); } catch (IOException e) { return -1; } finally { conn.disconnect(); } }
} else { URLConnection con = url.openConnection(); customizeConnection(con); HttpURLConnection httpCon = if (con.getContentLength() >= 0) { return true;
String path = "/sdcard/YourApp.apk"; try { URL url = new URL(sUrl[0]); URLConnection connection = url.openConnection(); connection.connect(); int fileLength = connection.getContentLength(); OutputStream output = new FileOutputStream(path); while ((count = input.read(data)) != -1) { total += count; publishProgress((int) (total * 100 / fileLength));
/** * Helper method to read the contentLength of a given URL, automatically * closing the InputStream that is opened as a side effect. */ private int getContentLength(URL url) throws IOException { URLConnection conn = url.openConnection(); try { return conn.getContentLength(); } finally { Utility.close(conn.getInputStream()); } }
return this.getFile().exists(); } else { URLConnection con = ex.openConnection(); ResourceUtils.useCachesIfNecessary(con); HttpURLConnection httpCon = con instanceof HttpURLConnection ? (HttpURLConnection) con : null; if (con.getContentLength() >= 0) { return true; } else if (httpCon != null) {