客户端的主要功能是打开本地文件,按批读取文件内容byte[]信息,调用RMI接口方法进行传输,同时进行传输进度的提醒。下面是笔者本人采用swing开发的测试代码,采用JProgressBar进行进度的实时提醒。
progressBar.setMinimum(0);
progressBar.setMaximum(100);
InputStream is = null;
try {
File srcFile = new File(localFilePath);
int fileSize = (int)srcFile.length();
String fileKey = getFileManageService().startUploadFile(localFilePath, fileSize, remoteFilePath);
byte[] buffer = new byte[1024 * 1024 * 2];
int offset = 0;
int numRead = 0;
is = new FileInputStream(srcFile);
while(-1 != (numRead=is.read(buffer))) {
offset += numRead;
getFileManageService().updateUploadProgress(fileKey, buffer);
double finishPercent = (offset * 1.0 / fileSize) * 100;
progressBar.setValue((int)finishPercent);
}
if(offset != fileSize) {
throw new IOException("不能完整地读取文件 " + localFilePath);
} else {
progressBar.setValue(100);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if(is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
实例界面截图如下所示: