你好,游客 登录 注册 搜索
背景:
阅读新闻

基于RMI服务传输大文件的完整解决方案

[日期:2016-02-26] 来源:Linux社区  作者:商兵兵 [字体:大 中 小]

2. 基于RMI服务下载大文件

2.1. 设计思路

下载大文件分为两个阶段,分别为“CS握手阶段”和“文件内容下载更新阶段”。

CS握手阶段,又可称之为客户端与服务端之间的基本信息交换阶段。服务端读取待下载的文件,获取文件长度,生成具有唯一性的FileKey信息,并将文件长度、FileKey信息传输到客户端,此FileKey作为客户端与服务端之间数据传输通道的唯一标示。这些信息数据类型均为Java基本数据类型,因此都是可序列化的。此接口方法就称之为“握手接口”,通过CS握手,客户端能确定两件事:哪个文件要下载、这个文件的尺寸,而这两件刚好是完成文件下载的基础。

然后就是“文件内容下载更新阶段”。服务端打开文件后,每次读取一定量的文件内容,譬如每次读取(1024 * 1024 * 2)byte的数据,并将此批数据通过“下载更新”接口方法传输到客户端,由客户端写入输出流中,同时检查文件内容是否已经传输完毕,如果已经传输完毕,则执行持久化flush操作在客户端生成文件;客户端每接收一批数据后,就更新“已下载数据总量”标示值,并与文件总长度进行比对计算,从而得到文件下载进度,实时告知用户。综上所述,客户端循环获取服务端传输过来的文件内容,直到服务端文件内容读取传输完毕为止。

2.2. 功能设计

2.2.1 文件下载相关状态信息的管理

大文件下载的过程中,在服务端,最重要的是文件下载过程相关状态信息的精确管理,譬如,文件总长度、已下载字节总数等等。而且要保证在整个下载过程中数据的实时更新和绝对不能丢失,并且在文件下载完毕后及时清除这些信息,以避免服务端累计过多失效的状态数据。

鉴于此,我们设计了一个类RFileDownloadTransfer来实现上述功能。代码如下:

/**
 * Description: 文件下载过程相关状态信息封装类。<br>
 * Copyright: Copyright (c) 2016<br>
 * Company: 河南电力科学研究院智能电网所<br>
 * @author shangbingbing 2016-01-01编写
 * @version 1.0
 */
public class RFileDownloadTransfer implements Serializable {
    private static final long serialVersionUID = 1L;
    private String fileKey;
    //服务端待下载文件路径
    private String srcFilePath;
    //待下载文件尺寸
    private int fileLength = 0;
    private InputStream inputStream = null;
    //已经传输文件内容字节总数
    private int transferByteCount = 0;
    //服务端待下载文件是否已经读取完毕
    private boolean isReadFinish = false;
   
    public RFileDownloadTransfer(String srcFilePath) {
        this.fileKey = UUID.randomUUID().toString();
        this.srcFilePath = srcFilePath;
        File srcFile = new File(srcFilePath);
        this.fileLength = (int)srcFile.length();
        try {
            this.inputStream = new FileInputStream(srcFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    public String getFileKey() {
        return fileKey;
    }
    public String getSrcFilePath() {
        return srcFilePath;
    }
    public int getFileLength() {
        return fileLength;
    }
    public boolean isReadFinish() {
        return isReadFinish;
    }
    public byte[] readBytes() {
        try {
            if(this.inputStream == null) {
                return null;
            }
            byte[] buffer = new byte[1024 * 1024 * 5];
            this.inputStream.read(buffer);
            this.transferByteCount += buffer.length;
            if(this.transferByteCount >= this.fileLength) {
                this.isReadFinish = true;
                this.transferByteCount = this.fileLength;
            }
            return buffer;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
   
    public void closeInputStream() {
        if(this.inputStream != null) {
            try {
                this.inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

然后,在RMI服务接口方法实现类中构建一个线程安全的集合,用来存储管理各个大文件的传输过程,代码如下:

/**
* 下载文件状态监视器
*/
private Hashtable<String,RFileDownloadTransfer> downloadFileStatusMonitor = new Hashtable<String,RFileDownloadTransfer>();

2.2.2 CS握接口设计

CS握手接口名称为startDownloadFile,主要功能就是传输交换文件基本信息,构建文件下载过程状态控制对象。其在接口实现类中的代码如下所示:

@Override
public Map<String, String> startDownloadFile(String srcFilePath) throws RemoteException {
    RFileDownloadTransfer fileTransfer = new RFileDownloadTransfer(srcFilePath);
    if(this.downloadFileStatusMonitor.containsKey(fileTransfer.getFileKey())) {
        this.downloadFileStatusMonitor.remove(fileTransfer.getFileKey());
    }
    this.downloadFileStatusMonitor.put(fileTransfer.getFileKey(), fileTransfer);
    Map<String,String> fileInfoList = new HashMap<String,String>();
    fileInfoList.put("fileLength", String.valueOf(fileTransfer.getFileLength()));
    fileInfoList.put("fileKey", fileTransfer.getFileKey());
    return fileInfoList;
}

2.2.3 文件内容下载更新接口设计

文件内容下载更新接口名称为updateDownloadProgress,主要功能是接收客户端传输过来的文件内容byte[]信息。其在接口实现类中的代码如下所示:

@Override
public byte[] updateDownloadProgress(String fileKey) throws RemoteException {
    if(this.downloadFileStatusMonitor.containsKey(fileKey)) {
        RFileDownloadTransfer fileTransfer = this.downloadFileStatusMonitor.get(fileKey);
        byte[] bytes = fileTransfer.readBytes();
        if(fileTransfer.isReadFinish()) {
            fileTransfer.closeInputStream();
            this.downloadFileStatusMonitor.remove(fileKey);
        }
        return bytes;
    }
    return null;
}

2.2.4 客户端设计

客户端的主要功能是创建磁盘文件,逐批次获取文件内容byte[]信息,并将其写入输出流中,同时进行传输进度的提醒,并最终生成完整的文件。下面是笔者本人采用swing开发的测试代码,采用JProgressBar进行进度的实时提醒。

Map<String,String> fileInfoList = getFileManageService().startDownloadFile(remoteFilePath);
int fileLength = Integer.valueOf(fileInfoList.get("fileLength"));
String fileKey = fileInfoList.get("fileKey");
int transferByteCount = 0;
                   
progressBar.setMinimum(0);
progressBar.setMaximum(100);
                   
OutputStream out = new FileOutputStream(localFilePath);
while(true) {
    if(transferByteCount >= fileLength) {
        break;
    }
                       
    byte[] bytes = getFileManageService().updateDownloadProgress(fileKey);
    if(bytes == null) {
        break;
    }
                       
    if(transferByteCount + bytes.length > fileLength) {
        //如果之前已经传输的数据长度+本批数据长度>文件长度的话,说明这批数据是最后一批数据了;
        //那么本批数据中将会有空字节,需要筛选出来。
        byte[] contents = new byte[fileLength - transferByteCount];
        for(int i=0;i<contents.length;i++) {
            contents[i] = bytes[i];
        }
        transferByteCount = fileLength;
        out.write(contents);
    } else {
        //说明本批数据并非最后一批数据,文件还没有传输完。
        transferByteCount += bytes.length;
        out.write(bytes);
    }
                       
    double dblFinishPercent = (transferByteCount * 1.0 / fileLength) * 100;
    int finishPercent = (int)dblFinishPercent;
    if(finishPercent > 100) {
        finishPercent = 100;
    }
    progressBar.setValue(finishPercent);
}
                   
if(transferByteCount != fileLength) {
    LogInfoUtil.printLog("不能完整地读取文件 " + remoteFilePath);
} else {
    progressBar.setValue(100);
    out.flush();
    if(out != null) {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

实例界面截图如下所示:

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-02/128710.htm

linux
相关资讯       RMI服务 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款