本文整理了Java中java.net.URLConnection.getContentLength()
方法的一些代码示例,展示了URLConnection.getContentLength()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.getContentLength()
方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:getContentLength
[英]Returns the content length in bytes specified by the response header field content-length or -1 if this field is not set.
[中]返回响应头字段content length指定的内容长度(字节),如果未设置此字段,则返回-1。
代码示例来源:origin: stackoverflow.com
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) {
代码示例来源:origin: wiztools/rest-client
private void init() throws IOException {
if(is == null && length == 0) {
URLConnection con = url.openConnection();
is = con.getInputStream();
length = con.getContentLength();
}
}
代码示例来源:origin: perwendel/spark
@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();
}
}
代码示例来源:origin: bytedeco/javacpp
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();
代码示例来源: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) {
e.printStackTrace(writer);
代码示例来源:origin: stackoverflow.com
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;
代码示例来源:origin: stackoverflow.com
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();
}
}
代码示例来源:origin: deeplearning4j/nd4j
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();
}
}
代码示例来源:origin: stackoverflow.com
URL url = new URL("http://server.com/file.mp3");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
代码示例来源:origin: chewiebug/GCViewer
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;
}
代码示例来源:origin: thymeleaf/thymeleaf
final URLConnection connection = this.url.openConnection();
if (connection.getContentLength() >= 0) {
return true;
代码示例来源:origin: stackoverflow.com
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;
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@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
}
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
@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();
}
代码示例来源:origin: redisson/redisson
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();
代码示例来源:origin: stackoverflow.com
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();
}
}
代码示例来源:origin: perwendel/spark
} else {
URLConnection con = url.openConnection();
customizeConnection(con);
HttpURLConnection httpCon =
if (con.getContentLength() >= 0) {
return true;
代码示例来源:origin: stackoverflow.com
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));
代码示例来源:origin: com.google.gwt/gwt-servlet
/**
* 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());
}
}
代码示例来源:origin: deeplearning4j/nd4j
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) {
内容来源于网络,如有侵权,请联系作者删除!