java - BOS/BIS 取得进展

标签 java stream bufferedinputstream bufferedoutputstream

我有一个带有 BufferedInputStream 和 BufferedOutputStream 的上传,现在我想获得此上传的百分比进度。

如何获得这个?

     BufferedInputStream bis = null;
     BufferedOutputStream bos = null;
     try
     {
        URL url = new URL( sb.toString() );
        URLConnection urlc = url.openConnection();

        bos = new BufferedOutputStream( urlc.getOutputStream() );
        bis = new BufferedInputStream( new FileInputStream( source ) );

        int i;
        int progress = 0;
        while ((i = bis.read()) != -1)
        {
           bos.write( i );
           //how to get the progress here?!

        }
     }
     finally
     {
        if (bis != null)
           try
           {
              bis.close();
           }
           catch (IOException ioe)
           {
              ioe.printStackTrace();
           }
        if (bos != null)
           try
           {
              bos.close();
           }
           catch (IOException ioe)
           {
              ioe.printStackTrace();
           }

     }

最佳答案

没问题。您可以使用 CountingInputStreamCountingOutputStream 包装器来实现这些目的。请参阅Apache Commons IO 2.5 library .

关于java - BOS/BIS 取得进展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286751/

相关文章:

c++ - 从流中读取以初始化只读变量

java - 如何终止 BufferedInputStream .read() 调用

java - 更改来自 Websphere 的默认 axis2 Web 服务调用?

scala - 与 Scala 中的简单正则表达式匹配的字符串流

c++ - 确保 C++ 流中的字节顺序

java - 在 Java 中寻找 BufferedInputStream 的最佳大小

java - 在Java中播放时将mp3文件写入磁盘

java - 从 Java ResultSet 获取 java.sql.RowId

java - 如何在Expression类中使用math.pow()进行计算?

java - SwingUtilities.getLocationOnScreen() 的问题;