
下載下傳完成之後:
難點在于:在讀取輸入流的時候,實時更新進度條。
我自己定了一個規則:
(1) 若輸入流的大小小于1024,則位元組數組的長度就是輸入流的大小。
<!--[if !supportlists]-->(1) (2) <!--[endif]-->擷取檔案的大小,并平分為100等份,得到商1;
<!--[if !supportlists]-->(2) (3)<!--[endif]-->拿每一等份(商1)和1024比較:若小于等于1024,則位元組數組大小為商1;
若大于1024,則除以1024,得到商2,則位元組數組大小為商1/商2
<!--[if !supportlists]-->(3)
規定:100%時的顔色為藍色:copyprogressbar.setforeground(color.blue);
核心代碼:
/***
* 在實際使用過程中,應該放線上程中
* @param in
* @param sourcesize
* : 輸入流的長度,即要讀取的位元組個數
* @param targfile
*/
public static void progress(jprogressbar copyprogressbar, inputstream in,
long sourcesize, file targfile) {
fileoutputstream target = null;
try {
int bytesarrleng = 0;
if (sourcesize < systemhwutil.buff_size_1024) {//
bytesarrleng = (int) sourcesize;
} else {
long shangone = sourcesize / systemhwutil.number_100;
if (shangone == 0) {// sourcesize<100
shangone = shangone + 1;
}
if (shangone <= systemhwutil.buff_size_1024) {
bytesarrleng = (int) shangone;
} else {
long shangtwo = shangone / systemhwutil.buff_size_1024;
if (shangone % systemhwutil.buff_size_1024 != 0) {
shangtwo = shangtwo + 1l;
}
bytesarrleng = (int) (shangone / shangtwo);
}
system.out.println("位元組數組的長度是:" + bytesarrleng);
target = new fileoutputstream(targfile);
byte[] buffer = new byte[bytesarrleng];
int bytenum;
long hasreadbyte = 0l;// 已經讀取的位元組個數
float result;
int progresssize = 0;
while ((bytenum = in.read(buffer)) != systemhwutil.negative_one) {
target.write(buffer, 0, bytenum);
hasreadbyte = hasreadbyte + bytenum;
result = (float) ((double) hasreadbyte / sourcesize);
progresssize = math.round(result * systemhwutil.number_100);
// copyprogressbar.setstring(progresssize + "%");
// copyprogressbar.setvalue(progresssize);
updateprogress(copyprogressbar, progresssize);
if (progresssize < systemhwutil.number_100) {
progresssize = systemhwutil.number_100;
copyprogressbar.setforeground(color.blue);
// system.out
// .println("[systemutil:copyfile]:file copy successfully!");
// resulttextarea.settext();
} catch (exception e) {
e.printstacktrace();
// copyfilebtn.setenabled(true);
} finally {
if (in != null) {
try {
in.close();
} catch (ioexception e) {
e.printstacktrace();
in = null;
if (target != null) {
target.close();
target = null;
}
}
/***
* 更新進度條上得進度數字
* @param copyprogressbar
* @param progresssize
private static void updateprogress(jprogressbar copyprogressbar, int progresssize) {
copyprogressbar.setstring(progresssize + "%");
copyprogressbar.setvalue(progresssize);
使用進度條的代碼:
new thread(new runnable() {
@override
public void run() {
try {
httpsocketutil.setdetail(true);
// httpsocketutil.httprequest(urltf.gettext(), null,
// null, true, selectedfile, -1, -1);
httpurlconnection huc = httpsocketutil
.beforeconnect(httpsenderapp.geturltf()
.gettext(), null, null, null, null,
systemhwutil.negative_one,
systemhwutil.negative_one);
inputstream in = huc.getinputstream();
int contentlength;
string sizeheadkey = null;
if (valuewidget.isnullorempty(sizeheadkey)) {// 若header中沒有size
contentlength = huc.getcontentlength();
} else {
contentlength = integer.parseint(huc
.getheaderfield(sizeheadkey));
}
componentutil.progress(
httpsenderapp.getcopyprogressbar(), in,
contentlength, selectedfile);
guiutil23.infodialog("下載下傳成功!size:"
+ fileutils.formatfilesize2(selectedfile
.length()));
} catch (exception e) {
guiutil23.errordialog(e.getmessage());
e.printstacktrace();
}
}).start();
依賴的jar:io0007-find_progess-0.0.7.2-snapshot.jar
demo:http-sender
注意:項目使用maven 建構