首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

下传监听类

2012-12-28 
上传监听类package com.huawei.hedex.uploadimport java.io.Serializable/** * 上传监听类,实现OutputSt

上传监听类
package com.huawei.hedex.upload;

import java.io.Serializable;

/**
* 上传监听类,实现OutputStreamListener接口
* @author j65181
* @version V1.0, 2007-8-4
* @see
*/
public class UploadListener implements OutputStreamListener
{
    /**
     * 注释内容
     */
    private static final long serialVersionUID = 4780612136871889921L;
   
    // 保存状态的内部类对象
    private FileUploadStats fileUploadStats = new FileUploadStats();
   
    /**
     * 构造方法,传入文件流总大小
     * @param totalSize
     */
    public UploadListener(long totalSize)
    {
        fileUploadStats.setTotalSize(totalSize);
    }
   
    public void start()
    {
        // 设置当前状态为开始
        fileUploadStats.setCurrentStatus("start");
    }
   
    public void bytesRead(int byteCount)
    {
        // 将已读取的数据保存到状态对象中
        fileUploadStats.incrementBytesRead(byteCount);
        // 设置当前的状态为读取过程中
        fileUploadStats.setCurrentStatus("reading");
    }
   
    public void error(String s)
    {
        // 设置当前状态为出错
        fileUploadStats.setCurrentStatus("error");
    }
   
    public void done()
    {
        // 设置当前已读取数据等于总数据大小
        fileUploadStats.setBytesRead(fileUploadStats.getTotalSize());
        // 设置当前状态为完成
        fileUploadStats.setCurrentStatus("done");
    }
   
    public FileUploadStats getFileUploadStats()
    {
        // 返回当前状态对象
        return fileUploadStats;
    }
   
    /**
     * 用来保存上传文件的状态
     * @author j65181
     * @version V1.0, 2007-8-4
     * @see
     */
    public static class FileUploadStats implements  Serializable
    {
        /**
         * 注释内容
         */
        private static final long serialVersionUID = -6689000695807341372L;

        //总数据的大小
        private long totalSize = 0;
       
        //已读数据大小
        private long bytesRead = 0;
       
        //开始读取的时间
        private long startTime = System.currentTimeMillis();
       
        //默认的状态
        private String currentStatus = "none";
       
        //获得已经上传得时间
        public long getElapsedTimeInSeconds()
        {
            return (System.currentTimeMillis() - startTime) / 1000;
        }
       
        public void incrementBytesRead(int byteCount)
        {
            this.bytesRead += byteCount;
        }
       
        /**
         * @return the totalSize
         */
        public long getTotalSize()
        {
            return totalSize;
        }
       
        /**
         * @param totalSize the totalSize to set
         */
        public void setTotalSize(long totalSize)
        {
            this.totalSize = totalSize;
        }
       
        /**
         * @return the currentStatus
         */
        public String getCurrentStatus()
        {
            return currentStatus;
        }
       
        /**
         * @param currentStatus the currentStatus to set
         */
        public void setCurrentStatus(String currentStatus)
        {
            this.currentStatus = currentStatus;
        }
       
        /**
         * @return the bytesRead
         */
        public long getBytesRead()
        {
            return bytesRead;
        }
       
        /**
         * @param bytesRead the bytesRead to set
         */
        public void setBytesRead(long bytesRead)
        {
            this.bytesRead = bytesRead;
        }
    }
}

热点排行