【问题标题】:BOS/BIS getting progressBOS/BIS 取得进展
【发布时间】:2013-04-23 14:20:38
【问题描述】:

我有一个 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();
           }

     }

【问题讨论】:

    标签: java stream bufferedinputstream bufferedoutputstream


    【解决方案1】:

    没问题。您可以将CountingInputStreamCountingOutputStream 包装器用于这些目的。见Apache Commons IO 2.5 library

    【讨论】:

    • 有没有办法使用 java 中包含的公共库来做到这一点?
    • 嗯,实际上你可以自己实现这些简单的包装器,看看它们是如何构造的(Commons 库是开源的)。但是在标准 Java 类中没有像 Counint*Stream 这样的现成类。
    猜你喜欢
    • 2016-09-21
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多