读取网络流获取文件大小
来源:互联网 发布:2016中国人工智能大会 编辑:程序博客网 时间:2024/04/30 21:24
package uxsino;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
//http://www.runoob.com/wp-content/themes/runoob/assets/img/newlogo.png
//http://test.pi-brand.com/jeesite/userfiles/1/images/cms/product/2016/12/product_logo_0.png
public class Main {
public static int getStrByUrlConnection(String spec) {
int result = 0;
try {
URL url = new URL(spec);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
InputStream inputStream = urlConnection.getInputStream();
result = stream2Str(inputStream);
} catch (IOException e) {
return 0;
}
return result;
}
private static int stream2Str(InputStream inputStream) {
byte[] buf = new byte[1024];
int len = -1;
int size = 0;
try {
while ((len = inputStream.read(buf)) != -1) {
size += len;
}
} catch (IOException e) {
return 0;
}
System.out.println(formetFileSize(size));
return size;
}
public static String formetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
public static void main(String[] args) {
getStrByUrlConnection("http://down.liangchan.net/EXCEL2003.zip");
}
}
package uxsino;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
public class News {
public static void downloadNet(String path) throws MalformedURLException {
// 下载网络文件
long bytesum = 0;
long byteread = 0;
URL url = new URL(path);
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
byte[] buffer = new byte[1024];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
}
System.out.println(formetFileSize(bytesum));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws MalformedURLException {
downloadNet("http://down.liangchan.net/EXCEL2003.zip");
}
public static String formetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
}
0 0
- 读取网络流获取文件大小
- java获取网络文件大小
- 读取文件大小 获取文件长度
- 读取文件大小 获取文件长度
- android 网络下载获取文件大小
- SDK获取文件大小 读取文件内容
- linux下C++读取文件,获取文件大小。
- 根据传入的地址获取网络文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 获取文件大小
- 深入分析VMware虚拟机的三种组网模式
- Java简单枚举的实现
- 简单实现一个人脸检测器(HOG+SVM实现人脸检测)
- include和merge 布局使减少布局重用
- 程序设计实验Ⅰ期末模拟 解题报告
- 读取网络流获取文件大小
- 【数据库】数据库并发操作(二)数据库并发控制机制
- PHP---XML--simpleXML
- java 注解与反射
- Ubuntu64位编译32位程序
- Linux操作系统支持常用的文件系统有哪些?
- 【数据库】数据库并发操作(一)数据库并发操作带来的问题
- AndroidStudio2.2+环境下的JNI环境搭建
- Eclipse 中 XML 警告 No grammar constraints (DTD or XML Schema) 解决方法