我試圖製作一個下載器,以便我可以自動更新我的程序。以下是到目前爲止我的代碼:如何判斷在InputStream中循環有多遠Java中的循環
public void applyUpdate(final CharSequence ver)
{
java.io.InputStream is;
java.io.BufferedWriter bw;
try
{
String s, ver;
alertOf(s);
updateDialogProgressBar.setIndeterminate(true);//This is a javax.swing.JProgressBar which is configured beforehand, and is displayed to the user in an update dialog
is = latestUpdURL.openStream();//This is a java.net.URL which is configured beforehand, and contains the path to a replacement JAR file
bw = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(System.getProperty("user.dir") + java.io.File.separatorChar + TITLE + ver + ".jar")));//Creates a new buffered writer which writes to a file adjacent to the JAR being run, whose name is the title of the application, then a space, then the version number of the update, then ".jar"
updateDialogProgressBar.setValue(0);
//updateDialogProgressBar.setMaximum(totalSize);//This is where I would input the total number of bytes in the target file
updateDialogProgressBar.setIndeterminate(false);
{
for (int i, prog=0; (i = is.read()) != -1; prog++)
{
bw.write(i);
updateDialogProgressBar.setValue(prog);
}
bw.close();
is.close();
}
}
catch (Throwable t)
{
//Alert the user of a problem
}
}
正如你所看到的,我只是試圖讓一個進度條下載器,但我不知道怎麼跟目標文件的總大小。 如何知道在文件下載完成之前要下載多少字節?
你被允許添加的文件大小爲流中的價值?像流的第一對夫婦字符是大小? – SuperTron
我不確定你的意思是...做JAR文件這樣做嗎? – Supuhstar
@Supuhstar跟進SuperTron的評論,當你開始接收文件(下載)時,你有辦法檢查文件大小嗎?流的前幾個字節是否包含這些信息? – abhinav