示例#1
0
  /**
   * 下载附件
   *
   * @author 何道军
   * @date 2011-1-7
   * @return String
   */
  public String downloadAttach() {
    try {
      if (attachId == 0) {
        result = "不存在的附件ID,下载失败,请稍后再试!";
        return "success";
      }
      Attach attach = attachService.getAttach(attachId);
      if (attach == null) {
        write(
            "<html><head><title>出错了</title></head><body>"
                + "<div style=\"text-align: center;margin-top: 100px;\">您下载的附件不存在,请检查数据完整性!</div>"
                + "</body></html>");
        return null;
      }
      fileStream = new FileInputStream(attach.getAttachUrl());
      uploadAttachFileName = attach.getAttachName();

      attach.setDownloadTime(attach.getDownloadTime() + 1);
      attachService.updateAttach(attach);

    } catch (FileNotFoundException e) {
      write(
          "<html><head><title>出错了</title></head><body>"
              + "<div style=\"text-align: center;margin-top: 100px;\">您下载的文件不存在,可能已经从服务器上删除!</div>"
              + "</body></html>");
      return null;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "success";
  }
示例#2
0
  /**
   * 添加附件
   * {"attachType":null,"attachName":"intel_ICS_Dx32.exe","result":"Y","attachGroupId":281,"attachId":345,"attachSize":"7.42
   * MB"}
   *
   * @author 何道军
   * @date 2011-1-7
   * @return String
   */
  public String addAttach() {
    Map<String, Object> attachMap = new HashMap<String, Object>();
    if (uploadAttach == null) {
      attachMap.put("success", false);
      attachMap.put("result", "添加失败。");
      JsonUtil.outJson(attachMap);
      return null;
    }

    if (uploadAttach.length() == 0) {
      attachMap.put("success", false);
      attachMap.put("result", "添加失败:不能上传大小为0 KB的文件。");
      JsonUtil.outJson(attachMap);
      return null;
    }

    if (maximumSize != null && maximumSize > 0 && maximumSize < uploadAttach.length()) {
      attachMap.put("success", false);
      attachMap.put(
          "result",
          "添加失败:文件大小不能超过 "
              + FormatUnitsUtil.formatBytes(maximumSize)
              + "。当前文件大小为:"
              + FormatUnitsUtil.formatBytes(uploadAttach.length()));
      JsonUtil.outJson(attachMap);
      return null;
    }
    if (StringUtils.isNotBlank(allowedExtensions)
        && !allowedExtensions.contains(FileUtil.getFileExp(uploadAttachFileName))) {
      attachMap.put("success", false);
      attachMap.put("result", "添加失败:只能上传后缀名为:" + allowedExtensions + "的文件。");
      JsonUtil.outJson(attachMap);
      return null;
    }
    try {
      if (attachGroupId != 0) {
        attachGroup = attachGroupService.getAttachGroup(attachGroupId);
        if (attachGroup == null) {
          attachMap.put("success", false);
          attachMap.put(
              "result", "系统错误:更新的附件组ID:" + attachGroupId + "不存在,如果要重新生成附件组ID,请设置附件组ID为空或0!");
          JsonUtil.outJson(attachMap);
          return null;
        }
      } else {
        attachGroup = new AttachGroup();
        attachGroup.setStatus(STATUS_NOT_DELETE);
        attachGroup.setSubmitDate(new Date());
        attachGroupService.saveAttachGroup(attachGroup);
      }

      dealSavePath();

      attach = new Attach();
      attach.setStatus(STATUS_NOT_DELETE);
      attach.setAttachName(uploadAttachFileName);
      attach.setAttachType(attachType);
      String attachURL = FileUtil.upload(uploadAttach, uploadAttachFileName, savePath);
      attach.setAttachUrl(attachURL);
      attach.setAttachGroup(attachGroup);
      attach.setSubmitDate(new Date());
      attach.setFileSize(FormatUnitsUtil.formatBytes(uploadAttach.length()));
      attach.setDownloadTime(0);
      attachService.saveAttach(attach);

      attachMap.put("attachGroupId", attachGroup.getAttachGroupId());
      attachMap.put("attachName", attach.getAttachName());
      attachMap.put("attachId", attach.getAttachId());
      attachMap.put("attachType", attach.getAttachType());
      attachMap.put("fileSize", attach.getFileSize());
      attachMap.put("success", true);
    } catch (Exception e) {
      attachMap.put("success", false);
      attachMap.put("result", "添加失败:" + e.getMessage());
      e.printStackTrace();
    }
    JsonUtil.outJson(attachMap);
    return null;
  }