Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
进度条时间计算+;JAVA_Java_Swing - Fatal编程技术网

进度条时间计算+;JAVA

进度条时间计算+;JAVA,java,swing,Java,Swing,我正在将文件从客户端传输到服务器。我不知道转车需要多长时间。但我的UI将保持不变,不会向用户透露任何信息。我需要保持这样一个进度条,它应该是进度,直到文件上传。我怎样才能做到这一点 我在.net中有点意识到这种情况。但是我们如何在java中做到这一点呢?如中所示,您可以指定不确定模式,直到您有足够的数据来衡量进度或下载结束。具体的实现取决于传输是如何进行的。理想情况下,发送方首先提供长度,但也可以在数据累积时动态计算速率 如中所示,您可以指定不确定模式,直到有足够的数据来衡量进度或下载结束。具体

我正在将文件从客户端传输到服务器。我不知道转车需要多长时间。但我的UI将保持不变,不会向用户透露任何信息。我需要保持这样一个进度条,它应该是进度,直到文件上传。我怎样才能做到这一点

我在.net中有点意识到这种情况。但是我们如何在java中做到这一点呢?

如中所示,您可以指定不确定模式,直到您有足够的数据来衡量进度或下载结束。具体的实现取决于传输是如何进行的。理想情况下,发送方首先提供长度,但也可以在数据累积时动态计算速率

如中所示,您可以指定不确定模式,直到有足够的数据来衡量进度或下载结束。具体的实现取决于传输是如何进行的。理想情况下,发送方首先提供长度,但也可以在数据累积时动态计算速率

对于真正“不确定”的行为,答案是正确的。为什么您认为您的文件传输属于这一类?难道你没有在互联网上下载过一个带有进度条的文件吗?你能想象没有这个吗

请参见下面的示例,该示例在以下问题的答案中提供

对于真正“不确定”的行为,答案是正确的。为什么您认为您的文件传输属于这一类?难道你没有在互联网上下载过一个带有进度条的文件吗?你能想象没有这个吗

请参见下面的示例,该示例在以下问题的答案中提供

URLConnection的
getContentLength()
可以帮助您获得文件的大小。
URLConnection的
getContentLength()
可以帮助您获得文件的大小。下面是一个相关的using
SwingWorker
。下面是一个相关的using
SwingWorker
public OutputStream loadFile(URL remoteFile, JProgressBar progress) throws IOException
{
    URLConnection connection = remoteFile.openConnection(); //connect to remote file
    InputStream inputStream = connection.getInputStream(); //get stream to read file

    int length = connection.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

    ByteArrayOutputStream out = new ByteArrayOutputStream(); //create our output steam to build the file here

    byte[] buffer = new byte[1024];
    int bytesRead = 0;

    while((bytesRead = inputStream.read(buffer)) != -1) //keep filling the buffer until we get to the end of the file 
    {   
        out.write(buffer, current, bytesRead); //write the buffer to the file offset = current, length = bytesRead
        current += bytesRead; //we've progressed a little so update current
        progress.setValue(current); //tell progress how far we are
    }
    inputStream.close(); //close our stream

    return out;
}