我上传了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();
}
}
答案 0 :(得分:1)
没问题。您可以使用CountingInputStream
和CountingOutputStream
包装器来实现这些目的。请参阅Apache Commons IO 2.5 library。