@Override
  public void execute() {
    // 包格式:序列化的原语|源标签

    if (this.packet.getSubsegmentCount() < 2) {
      Logger.e(ServerDialogueCommand.class, "Dialogue packet format error");
      return;
    }

    byte[] priData = this.packet.getSubsegment(0);
    ByteArrayInputStream stream = new ByteArrayInputStream(priData);

    byte[] tagData = this.packet.getSubsegment(1);
    String speakerTag = Utils.bytes2String(tagData);

    byte[] identifierData = this.packet.getSubsegment(2);

    // 反序列化原语
    Primitive primitive = new Primitive(speakerTag);
    primitive.read(stream);

    this.service.processDialogue(
        this.session, speakerTag, Utils.bytes2String(identifierData), primitive);
  }
Ejemplo n.º 2
0
  /**
   * 接收文件数据。
   *
   * @param name
   * @param request
   */
  public void receive(String name, HttpServletRequest request) {
    ServletFileUpload upload = new ServletFileUpload(this.factory);
    upload.setSizeMax(this.fileSizeMax);
    upload.setHeaderEncoding("UTF-8");

    // 设置文件上传进度监听器
    FileProgressListener pl = new FileProgressListener(name, request.getSession());
    upload.setProgressListener(pl);

    try {
      List<FileItem> items = upload.parseRequest(request);

      for (FileItem item : items) {
        if (!item.isFormField()) {
          // 原始文件名
          String originFileName = item.getName();

          // 文件后缀名
          String extension = FileUtils.extractFileExtension(originFileName);

          // 文件别名
          String aliasFileName = Utils.randomString(32) + "." + extension;

          FileType fileType = FileType.parseType(extension);

          if (FileType.UNKNOWN != fileType) {
            // 存储路径
            String strFilePath = this.storePath + name + "/";

            // 创建工作目录
            File filePath = new File(this.workPath + name + "/");
            if (!filePath.exists()) {
              filePath.mkdirs();
            }
            // 创建存储目录
            filePath = new File(strFilePath);
            if (!filePath.exists()) {
              filePath.mkdirs();
            }
            filePath = null;

            // 删除相同文件
            SharedFile sf = this.existSharedFile(originFileName);
            if (null != sf) {
              sf.delete();
              this.removeSharedFile(sf);
            }

            // 原文件
            File originFile = new File(strFilePath, originFileName);
            // 别名文件
            File aliasFile = new File(this.workPath + name + "/", aliasFileName);

            sf = new SharedFile(name, originFile, aliasFile, fileType, request.getSession());

            // 添加分享文件
            this.addSharedFile(sf);

            synchronized (pl.files) {
              pl.files.add(sf);
            }

            // 写文件
            item.write(originFile);

            request.getSession().setAttribute("name", name);
            request.getSession().setAttribute("filename", originFileName);
          } else {
            // 不支持的类型
            Logger.w(this.getClass(), "不支持的文件类型: " + originFileName);

            request.getSession().setAttribute("name", name);
            request.getSession().setAttribute("filename", originFileName);
            request.getSession().setAttribute("state", 300);
          }
        }
      }

      // 结束
      pl.finish();
    } catch (FileUploadException e) {
      Logger.log(this.getClass(), e, LogLevel.ERROR);
    } catch (Exception e) {
      Logger.log(this.getClass(), e, LogLevel.ERROR);
    }
  }