连接JProgressBar与下载过程

连接JProgressBar与下载过程

本文介绍了连接JProgressBar与下载过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有以下代码。我不能让它工作。



我不得不提到网址正在重定向。我的意思是 url = http://www.thissite.com 并重定向到 http://www.othersite.com 。但是我想要使用初始的 url

  public void download(String address,String localFileName,JProgressBar progress){
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;

try {

URL url = new URL(address);



//打开本地文件系统上目标文件的输出流
out = new BufferedOutputStream(new FileOutputStream(/ MusicDownloads /+ localFileName) );
conn = url.openConnection();

in = conn.getInputStream();

int length = conn.getContentLength(); //找出文件多久,任何好的网络服务器应该提供这个信息
int current = 0;
progress.setMaximum(length); //我们要得到这么多字节
progress.setValue(0); //我们已经得到0个字节到目前为止

//获取数据
byte [] buffer = new byte [1024];
int numRead = 0;

while((numRead = in.read(buffer))!= -1){
current = 0;
out.write(buffer,current,numRead);

current + = numRead; //我们已经进步了一点,所以更新当前

progress.setValue(current); //告诉进度我们有多远


}
//完成!只需要清理
} catch(异常异常){
exception.printStackTrace();
} finally {
try {
if(in!= null){
in.close();
}
if(out!= null){
out.close();
}
} catch(IOException ioe){
//不应该发生,如果你不是
//愚弄,可能会添加一些日志记录;)

}
}


解决方案

使用。


I have the below code. I can't make it work.

I have to mention that the URL is redirecting. I mean that url = http://www.thissite.com and redirects to http://www.othersite.com. But I want to get it work with the initial url.

 public void download(String address, String localFileName, JProgressBar progress ) {
    OutputStream out = null;
    URLConnection conn = null;
    InputStream in = null;

    try {

          URL url = new URL(address);



        // Open an output stream to the destination file on our local filesystem
        out = new BufferedOutputStream(new FileOutputStream("/MusicDownloads/"+localFileName));
        conn = url.openConnection();

        in = conn.getInputStream();

        int length = conn.getContentLength(); //find out how long the file is, any good webserver should provide this info
         int current = 0;
          progress.setMaximum(length); //we're going to get this many bytes
          progress.setValue(0); //we've gotten 0 bytes so far

        // Get the data
        byte[] buffer = new byte[1024];
        int numRead = 0;

        while ((numRead = in.read(buffer)) != -1) {
            current=0;
            out.write(buffer, current, numRead);

              current += numRead; //we've progressed a little so update current

            progress.setValue(current); //tell progress how far we are


        }
        // Done! Just clean up and get out
    } catch (Exception exception) {
        exception.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException ioe) {
            // Shouldn't happen, maybe add some logging here if you are not
            // fooling around ;)
        }
    }
}
解决方案

Use a ProgressMonitorInputStream instead.

这篇关于连接JProgressBar与下载过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:52