关于socket 文件的分块问题!!!!!!

ACsiLence 2016-07-19 04:51:41
菜鸟一枚-------------------------------------------------
包头和包体的形式,多个线程读取分块传输到接收端。
这是每个块的封装
public class FileData {
private String fileName;
private long fileLen;
private byte[] data;

public FileData(String fileName, long fileLen, byte[] data) {
super();
this.fileName = fileName;
this.fileLen = fileLen;
this.data = data;
}

public byte[] getData() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}

}

这是具体的分块方法
public class FileSplit {
private DataInputStream currentDis;
private List<String> filePathList;
private String currentFileName;
private long currentFileLen;

public FileSplit(List<String> filePathList) {
this.filePathList = filePathList;
for (int i = 0; i < filePathList.size(); i++) {
try {
File file = new File(filePathList.get(0));
currentDis = new DataInputStream(new FileInputStream(file));
currentFileName = file.getName();
currentFileLen = file.length();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public FileData get(int len) {
byte[] b = new byte[len];
try {
int l = currentDis.read(b);
//if read finish change dis
if (l == -1) {

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new FileData(currentFileName, currentFileLen, b);

}
}

目前的想法是,我再多封装一个startPos,记录字节读取开始位置,这样接收端解析的时候就知道接收到的块该从哪里开始写入了。现在的疑问是,一个线程执行一次这个方法,我的startPos怎么变化呢,怎么自加呢!
...全文
142 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACsiLence 2016-07-19
  • 打赏
  • 举报
回复
up up up...

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧