58,454
社区成员
发帖
与我相关
我的任务
分享
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(new HttpPost(""));
HttpEntity entity = resp.getEntity();
int length = (int)entity.getContentLength();//这个就是下载的文件(不单指文件)大小
InputStream is = entity.getContent();
ProgressBarThread pbt = new ProgressBarThread(length);//创建进度条
new Thread(pbt).start();//开启线程,刷新进度条
byte[] buf = new byte[1024];
int size = 0;
FileOutputStream fos = new FileOutputStream(new File(""));
while((size=is.read(buf))!=-1){
fos.write(buf,0,size);
pbt.updateProgress(size);//写完一次,更新进度条
}
pbt.finish();//文件读取完成,关闭进度条
fos.flush();
fos.close();
resp.abort();
package test;
import java.util.ArrayList;
public class ProgressBarThread implements Runnable{
private ArrayList<Integer> proList = new ArrayList<Integer>();
private int progress;//当前进度
private int totalSize;//总大小
private boolean run = true;
public ProgressBarThread(int totalSize){
this.totalSize = totalSize;
//TODO 创建进度条
}
/**
* @param progress 进度
*/
public void updateProgress(int progress){
synchronized (this.proList) {
if(this.run){
this.proList.add(progress);
this.proList.notify();
}
}
}
public void finish(){
this.run = false;
//关闭进度条
}
@Override
public void run() {
synchronized (this.proList) {
try {
while (this.run) {
if(this.proList.size()==0){
this.proList.wait();
}
synchronized (proList) {
this.progress += this.proList.remove(0);
//TODO 更新进度条
System.err.println("当前进度:"+(this.progress/this.totalSize*100)+"%");
}
}
System.err.println("下载完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try {
File file = new File("源文件");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("文件保存路径");
ProgressBarThread pbt = new ProgressBarThread((int)file.length());//创建进度条
new Thread(pbt).start();//开启线程,刷新进度条
byte[] buf = new byte[1024];
int size = 0;
while((size = fis.read(buf))>-1){//循环读取
fos.write(buf, 0, size);
pbt.updateProgress(size);//写完一次,更新进度条
}
pbt.finish();//文件读取完成,关闭进度条
fos.flush();
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}