基于

this文章,我建议您编写一个下载类,它可以轻松更新进度条.

这是Download类:

import java.io.*;

import java.net.*;

import java.util.*;

// This class downloads a file from a URL.

class Download extends Observable implements Runnable {

// Max size of download buffer.

private static final int MAX_BUFFER_SIZE = 1024;

// These are the status names.

public static final String STATUSES[] = {"Downloading",

"Paused", "Complete", "Cancelled", "Error"};

// These are the status codes.

public static final int DOWNLOADING = 0;

public static final int PAUSED = 1;

public static final int COMPLETE = 2;

public static final int CANCELLED = 3;

public static final int ERROR = 4;

private URL url; // download URL

private int size; // size of download in bytes

private int downloaded; // number of bytes downloaded

private int status; // current status of download

// Constructor for Download.

public Download(URL url) {

this.url = url;

size = -1;

downloaded = 0;

status = DOWNLOADING;

// Begin the download.

download();

}

// Get this download's URL.

public String getUrl() {

return url.toString();

}

// Get this download's size.

public int getSize() {

return size;

}

// Get this download's progress.

public float getProgress() {

return ((float) downloaded / size) * 100;

}

// Get this download's status.

public int getStatus() {

return status;

}

// Pause this download.

public void pause() {

status = PAUSED;

stateChanged();

}

// Resume this download.

public void resume() {

status = DOWNLOADING;

stateChanged();

download();

}

// Cancel this download.

public void cancel() {

status = CANCELLED;

stateChanged();

}

// Mark this download as having an error.

private void error() {

status = ERROR;

stateChanged();

}

// Start or resume downloading.

private void download() {

Thread thread = new Thread(this);

thread.start();

}

// Get file name portion of URL.

private String getFileName(URL url) {

String fileName = url.getFile();

return fileName.substring(fileName.lastIndexOf('/') + 1);

}

// Download file.

public void run() {

RandomAccessFile file = null;

InputStream stream = null;

try {

// Open connection to URL.

HttpURLConnection connection =

(HttpURLConnection) url.openConnection();

// Specify what portion of file to download.

connection.setRequestProperty("Range",

"bytes=" + downloaded + "-");

// Connect to server.

connection.connect();

// Make sure response code is in the 200 range.

if (connection.getResponseCode() / 100 != 2) {

error();

}

// Check for valid content length.

int contentLength = connection.getContentLength();

if (contentLength < 1) {

error();

}

/* Set the size for this download if it

hasn't been already set. */

if (size == -1) {

size = contentLength;

stateChanged();

}

// Open file and seek to the end of it.

file = new RandomAccessFile(getFileName(url), "rw");

file.seek(downloaded);

stream = connection.getInputStream();

while (status == DOWNLOADING) {

/* Size buffer according to how much of the

file is left to download. */

byte buffer[];

if (size - downloaded > MAX_BUFFER_SIZE) {

buffer = new byte[MAX_BUFFER_SIZE];

} else {

buffer = new byte[size - downloaded];

}

// Read from server into buffer.

int read = stream.read(buffer);

if (read == -1)

break;

// Write buffer to file.

file.write(buffer, 0, read);

downloaded += read;

stateChanged();

}

/* Change status to complete if this point was

reached because downloading has finished. */

if (status == DOWNLOADING) {

status = COMPLETE;

stateChanged();

}

} catch (Exception e) {

error();

} finally {

// Close file.

if (file != null) {

try {

file.close();

} catch (Exception e) {}

}

// Close connection to server.

if (stream != null) {

try {

stream.close();

} catch (Exception e) {}

}

}

}

// Notify observers that this download's status has changed.

private void stateChanged() {

setChanged();

notifyObservers();

}

}

如您所见,这个Download类有一些特定的字段,如大小和下载.

在其他一些方法中你可以写:

JProgressBar j = new JProgressBar(0,download.getSize());

在此之后你可以启动一个新的Thread,它会以一定的间隔更新你的进度条,就像每10毫秒一样

j.setValue(download.getDownloaded());

希望这可以帮到你.

java无法下载更新文件,java – 在更新JProgressBar的同时下载文件相关推荐

  1. 下载ug时java更新未完成_UG导入导出Dwg,Dxf文件提示:转换未完成,网上说的方法都试了一遍没用怎么办?...

    这是因为导不出DWG/DXF文件的原因有多种,你找到的解决方案可能并不是你所在的问题,也有可能是你没有按解决方案中的做到位. 你可以按以下几个原因排查: 1.高版本NX软件,在导入DWG\DXF的时候 ...

  2. Java adt 国内更新_Adt-Bundle的下载与Android版本的更新

    Adt-Bundle的下载与Android版本的更新制作人:傅佩佩 关于Adt-Bundle 1.什么是ADT-Bundle ADT- Bundle包含了Eclipse.ADT插件和SDK Tools ...

  3. java json 修改字段_JSON文件-Java:编辑/更新字段值

    我的工作流程中有一些JSONObject,并且通过将它们写入json文件来存储相同的JSONObject. 我想要一种有效的方式来更新json文件, 仅 更新 需要的字段, 以及更新的JSONObje ...

  4. java 上传文件及预览_SpringBoot上传下载文件及在线预览

    SpringBoot上传下载文件及在线预览 今天大概就说说如何使用SpringBoot进行上传和下载以及在线预览文件 本篇主要介绍上传下载的功能,对于界面就简单一点,大致如下: 一.老规矩还是先看看小 ...

  5. 明翰Java教学系列之认识Java篇V1.3(持续更新)

    文章目录 传送门 前言 什么是Java? Java之父 `Java的应用场景` Java部分特点 Java工作机制 JDK(Java Development Kit) JRE(Java Runtime ...

  6. Java工作面试必问(持续更新)

    SSM  1. GET和POST请求的区别? ① GET(获取&检索)从服务器上获取数据. ① POST(创建&更新)向服务器传送数据. 误区:不是获取数据只能用GET,只是通常用GE ...

  7. java开发中遇到的Bug(持续更新中)

    1.Error creating bean with name 'XXX': Injection of autowired dependencies: 错误原因:实体类中没有@Component(&q ...

  8. java撤销删除_[Java教程]利用npm安装/删除/发布/更新/撤销发布包

    [Java教程]利用npm安装/删除/发布/更新/撤销发布包 0 2017-06-10 00:00:32 一.什么是npm? npm是javascript的包管理工具,是前端模块化下的一个标志性产物 ...

  9. 超全面Java全栈知识体系,定期更新(最近更新:2022.8.2)...

    前言 最新Java全栈知识系统,定期更新..... 本套知识体系涵盖六大块: 一.前端基础 涵盖HTML5.CSS3.JavaScript.jQuery.Layui.Bootstrap等: 二.Jav ...

最新文章

  1. Javascript字符串及数组赋值区别
  2. safari java插件故障_safari flash插件故障怎么办 mac safari flash插件故障解决方法
  3. VC 2.5 与 ESX 3.0.1 之间存在接口问题
  4. centos中python报错 SyntaxError: Non-ASCII character ‘\xe7‘ in file car-training.py on line 7, but no en
  5. Job for network.service failed because the control process exited with error code问题
  6. Mono制作安装包让C#桌面应用程序脱离net framework步骤
  7. boost::type_erasure模块实现类型安全的 printf的测试程序
  8. python机器学习入门(Day1:Numpy)
  9. 10 个实战及面试常用 Shell 脚本编写
  10. magento php 所需模块,magento博客 - Magento2 创建基本模块
  11. 一段超级好的..漂浮广告代码..精一下..
  12. Toml :设置策略配置文件
  13. java实现飞机大战小游戏(源码+注释)
  14. vcf通讯录转excel
  15. Simplify3D打印参数设置
  16. 软件测试工程师需要学习什么内容
  17. DATE_FORMAT函数用法
  18. 量化交易之vn.py篇 - 同步持仓发单逻辑(非净头寸 净头寸)
  19. librtmp h265 推流
  20. 清风数学建模---插值算法

热门文章

  1. 无监督学习KMeans学习笔记和实例
  2. EasybossT8“走入”阿联酋
  3. 用MQTT.fx模拟温度设备联调阿里云IOT物联网平台
  4. 【逆向】【Android微信】获取微信聊天记录
  5. Mac OS X 背后的故事(四)——政客的跨界
  6. 在你的DLL中,如何导入(导出)一个类,使得其他项目可以调用该类?
  7. 零基础如何学好软件测试?
  8. Git 工具的下载安装
  9. 一般向量空间的基变换_向量几何--3-D空间中的基变换与坐标变换【转】 - 回到未来 - C++博客...
  10. [书]x86汇编语言:从实模式到保护模式 -- 第17章 中断、任务切换、分页机制、平坦模型