downlaod data from http with RandomAccessFile(muli Thread)
来源:互联网 发布:java学生信息录入系统 编辑:程序博客网 时间:2024/06/14 14:59
/**
*
* @author yw04009 Bill, Wu
*
*/
public class TestDownLoad {
public void testOneThread(String filePath, String url) throws IOException {
File file = new File(filePath + getFileExtName(url));
FileWriter fWriter = new FileWriter(file);
URL ul = new URL(url);
URLConnection conn = ul.openConnection();
conn.setConnectTimeout(2000);
InputStream in = conn.getInputStream();
int temp = 0;
while ((temp = in.read()) != -1) {
fWriter.write(temp);
}
fWriter.close();
in.close();
}
public String getFileExtName(String path) {
return path.substring(path.lastIndexOf("."));
}
public void testMoreThread(String filePath, String url, int tnum) throws IOException {
final File file = new File(filePath + getFileExtName(url));
RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
final URL ul = new URL(url);
HttpURLConnection conn = (HttpURLConnection) ul.openConnection();
conn.setConnectTimeout(2000);
conn.setRequestMethod("GET");
int len = conn.getContentLength();
accessFile.setLength(len);
accessFile.close();
final int block = (len + tnum - 1) / tnum; //the size for every thread download
for (int i = 0; i < tnum; i++) {
final int a = i;
new Thread(new Runnable() {
int start = block * a; //start place
int end = block * (a + 1) - 1; //end place
public void run() {
// TODO Auto-generated method stub
HttpURLConnection conn2 = null;
RandomAccessFile accessFile2 = null;
InputStream in = null;
try {
conn2 = (HttpURLConnection) ul.openConnection();
conn2.setConnectTimeout(2000);
conn2.setRequestMethod("GET");
conn2.setRequestProperty("Range", "bytes=" + start
+ "-" + end + "");//
in = conn2.getInputStream();
byte[] data = new byte[1024];
int len = 0;
accessFile2 = new RandomAccessFile(file, "rwd");
accessFile2.seek(start);
while ((len = in.read(data)) != -1) {
accessFile2.write(data, 0, len);
}
System.out.println("This Thread Download is Compled .. ");
} catch (IOException e) {
}
}
}).start();
}
}
public static void main(String[] args) throws IOException {
TestDownLoad test = new TestDownLoad();
String path = "http://bill.com/test.txt";
//test.testOneThread("C:\\code\\test1", path);
//test.testMoreThread("C:\\code\\test2", path, 3); }
}
- downlaod data from http with RandomAccessFile(muli Thread)
- HTC G3 cannot downlaod from market
- Loading asset data with source data from an external source
- Get Post Data From Http Request
- [PHP] Http API with JSON data
- InfluxDB - Writing Data with the HTTP API
- Export Data from GridView to Excel, Word, HTML with C#
- Dump data into MySQL from Excel with PHP
- How to read binary data from HDFS with Thrift?
- extjs load data from mysql with php example
- Qt15 Update a data from Sqlite database with pushbutton
- QT16 delete data from sqlite database with pushbutton
- Thread-safe caching object with file and HTTP implementations
- Getting Data From the Web (URLConnection via http)
- Thread-specific data
- Thread-specific data
- Got fatal error 1236 from master when reading data from binary log: 'A slave with the same server_uu
- CWaveFile -- a Class for Working with and Representing Data from WAVEs
- Linux Kernel and Android 休眠与唤醒(中文版)
- ActiveMQ极限性能参数测试结果
- 调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配
- 重写equal,== !=
- The Collection Interfaces in Java
- downlaod data from http with RandomAccessFile(muli Thread)
- 防编译代码
- 数据结构学习(十三)——插入排序
- boost bind使用指南
- 在命令行中的替换和截取字符串
- 错误Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.
- myeclipse6.5设置的debug不能被删除
- 委托
- 总帐与明细帐