Java Code Examples for javax.net.ssl.HttpsURLConnection#getContentLength()

The following examples show how to use javax.net.ssl.HttpsURLConnection#getContentLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtils.java    From Common with Apache License 2.0 6 votes vote down vote up
/**
 * Return the length of file.
 *
 * @param filePath The path of file.
 * @return the length of file
 */
public static long getFileLength(final String filePath) {
    boolean isURL = filePath.matches("[a-zA-z]+://[^\\s]*");
    if (isURL) {
        try {
            HttpsURLConnection conn = (HttpsURLConnection) new URL(filePath).openConnection();
            conn.setRequestProperty("Accept-Encoding", "identity");
            conn.connect();
            if (conn.getResponseCode() == 200) {
                return conn.getContentLength();
            }
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return getFileLength(new File(filePath));
}
 
Example 2
Source File: FileUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * Return the length of file.
 *
 * @param filePath The path of file.
 * @return the length of file
 */
public static long getFileLength(final String filePath) {
    boolean isURL = filePath.matches("[a-zA-z]+://[^\\s]*");
    if (isURL) {
        try {
            HttpsURLConnection conn = (HttpsURLConnection) new URL(filePath).openConnection();
            conn.setRequestProperty("Accept-Encoding", "identity");
            conn.connect();
            if (conn.getResponseCode() == 200) {
                return conn.getContentLength();
            }
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return getFileLength(getFileByPath(filePath));
}
 
Example 3
Source File: HTTPGet.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
public static String readString(String uri){
    HTTPSTrustManager.allowAllSSL();
    try {
        URL url = new URL(uri);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
        conn.setRequestProperty("Accept-Encoding", "identity");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        int code = conn.getResponseCode();
        if (code == 200) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            try {
                int length = conn.getContentLength();
                if (length < 0) {
                    length = 4096;
                }
                CharArrayBuffer buffer = new CharArrayBuffer(length);
                char[] tmp = new char[1024];

                int l;
                while ((l = reader.read(tmp)) != -1) {
                    buffer.append(tmp, 0, l);
                }
                return buffer.toString();
            }finally {

                reader.close();
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        Log.e("HTTPGet", "readString", e);
    }
    finally {

    }
    return null;
}
 
Example 4
Source File: HttpsURLConnectionWrapper.java    From android-perftracking with MIT License 4 votes vote down vote up
private static long getContentLengthLongCompat(HttpsURLConnection conn) {
  return Build.VERSION.SDK_INT < 24
      ? (long) conn.getContentLength()
      : conn.getContentLengthLong();
}
 
Example 5
Source File: DownloadTask.java    From remixed-dungeon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
    boolean result = false;

    m_listener.DownloadProgress(m_url, 0);

    try {
        URL url = new URL(m_url);
        File file = new File(m_downloadTo);

        HttpsURLConnection ucon = NetCipher.getCompatibleHttpsURLConnection(url);

        ucon.setSSLSocketFactory(new TlsOnlySocketFactory());

        ucon.setReadTimeout(2500);
        ucon.setInstanceFollowRedirects(true);
        ucon.connect();

        int repCode = ucon.getResponseCode();

        if (repCode == HttpURLConnection.HTTP_OK) {
            int bytesTotal = ucon.getContentLength();

            GLog.debug("bytes in file: " + bytesTotal);

            InputStream is = ucon.getInputStream();

            FileOutputStream fos = new FileOutputStream(file);

            byte[] buffer = new byte[16384];
            int count;
            int bytesDownloaded = 0;
            while ((count = is.read(buffer)) != -1) {
                fos.write(buffer, 0, count);
                bytesDownloaded += count;
                m_listener.DownloadProgress(m_url, bytesDownloaded);
            }

            fos.close();

            result = true;
        }

    } catch (Exception e) {
        EventCollector.logException(new ModError("Downloading",e));
    }

     m_listener.DownloadComplete(m_url, result);
}