2010-06-29 134 views

回答

0

由于retrieveFilestoreFile处理输入和输出流,有可能是你写你自己的子类,它可以在或出过传输的字节数一定时间?

1

试试这个:

public class ReportingOutputStream extends OutputStream { 
    public static final String BYTES_PROP = "Bytes"; 
    private FileOutputStream fileStream; 
    private long byteCount = 0L; 
    private long lastByteCount = 0L; 
    private long updateInterval = 1L << 10; 
    private long nextReport = updateInterval; 
    private PropertyChangeSupport changer = new PropertyChangeSupport(this); 

    public ReportingOutputStream(File f) throws IOException { 
     fileStream = new FileOutputStream(f); 
    } 

    public void setUpdateInterval(long bytes) { 
     updateInterval = bytes; 
     nextReport = updateInterval; 
    } 

    @Override 
    public void write(int b) throws IOException { 
     byte[] bytes = { (byte) (b & 0xFF) }; 
     write(bytes, 0, 1); 
    } 

    @Override 
    public void write(byte[] b, int off, int len) throws IOException { 
     fileStream.write(b, off, len); 
     byteCount += len; 
     if (byteCount > nextReport) { 
      changer.firePropertyChange(BYTES_PROP, lastByteCount, byteCount); 
      lastByteCount = byteCount; 
      nextReport += updateInterval; 
     } 
    } 

    @Override 
    public void close() throws IOException { 
     if (fileStream != null) { 
      fileStream.close(); 
      fileStream = null; 
     } 
    } 

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { 
     changer.removePropertyChangeListener(propertyName, listener); 
    } 

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { 
     changer.addPropertyChangeListener(propertyName, listener); 
    } 
} 

创建流后,添加属性更改侦听BYTES_PROP。默认情况下,它会触发每接收到1 KB的处理程序。致电setUpdateInterval更改。