多文件并发传输-------资源文件的接收与发送

mfct 文件的接收与发送

文件的发送方:
主要方法:

private void sendSections() {
          String appName=rbiFromServer.getAppName();
          ResourceBaseInfo1   rbi=ResourcePool1.getResourceBaseInfo(appName);
//        从池子里面去取  ,避免版本号已经不是最新
          List<ResourceStructInfo1>  RsiList=rbi.getRsiList();
//        存着 资源文件的路径、资源文件的标识、资源文件的大小
          List<SectionInfo> siList=rbi.getSiList();
//        文件片段信息   文件的偏移量、文件的的大小、文件的标识
//        因为要发送真正的文件、所以要进行文件片段的发送
//        由 文件片段的文件标识,从文件框架信息列表中找到文件的路径
//        从而得到 raf  进行发送
          rafBuffer=new HashMap<>();
          for(SectionInfo section: siList) {
               int  FileHandel=section.getFileHandle();
               String  filePath=rbi.getAbsoluteRoot()+getRelativePath(FileHandel,RsiList);
               RandomAccessFile  raf=getRaf(filePath);
//             getRaf()方法
//             由于 很多个片段可能由一个文件 而分解的
//             所以  需要一个池子  如果文件 在之前产生过则只需要  根据文件名称来找到即可
               int size=section.getSize();
               long offset=section.getOffset();
               
               
               try {
                    byte[]  bytes=new byte[size];
                    raf.read(bytes, (int)offset, size);
                    
                    FileSection fileSection=new  FileSection();
                    fileSection.setSectionInfo(section);
                    fileSection.setValues(bytes);
                    fileSection.sendSection(dos);
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
               closeFile();
          }
     }
          

文件的接收端

接收服务器的
成员:
资源 以及节点的 相关信息
这里看到由 ufMap 这个类是处理 接收片段时对片段进行组织和统计的
由于接收端 要收到若干 的发送端发来的文件 所以我们想到利用线程池进行优化

     ResourceBaseInfo1  resourceBaseInfo1;
     ServerSocket  server;
     Socket   sender;
//   DataInputStream  dis;
     int rmiPort;
     ThreadPoolExecutor  threadPool;
     int senderCount;
     HashMap<Integer, UnreceivedFileSection1>   ufMap;

主要方法;

@Override
     public void run() {
//   由 文件句柄来找到文件的 (从StructInfo之中)路径
//   然后就可以生成  raf
          String  rootPath=resourceBaseInfo1.getAbsoluteRoot();
          try {
               dis=new  DataInputStream(sender.getInputStream());
               FileSection fileSection=new FileSection();
               fileSection.getFileSection(dis);
//             *******************这时候 fileSection里面储存着 接受好的片段
//             要做的就是依据 路径生成这个文件并且把相应的值set进去********
               SectionInfo   sectionInfo=fileSection.getSectionInfo();
               int FileHandel=sectionInfo.getFileHandle();
               ResourceStructInfo1   resourceStructInfo1=rsiMap.get(FileHandel);
               String  relativeRoot=resourceStructInfo1.getFilePath();
               
               String path=rootPath+relativeRoot;
               RandomAccessFile   randomAccessFile=randomFilePool.getRaf(FileHandel, path);
               int size=sectionInfo.getSize();
               long offset=sectionInfo.getOffset();
               
               randomAccessFile.seek(offset);
               byte[]  bytes=new byte[size];
               randomAccessFile.write(bytes);
               
               UnreceivedFileSection1  unreceivedFileSection1=ufMap.get(FileHandel);
               unreceivedFileSection1.afterReceiveSection(sectionInfo);
               
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          
     }
     
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值