Beispiel #1
0
  @Post
  public Representation update(Representation entity) {
    DomRepresentation r = null;
    try {
      int userid = Integer.parseInt((String) getRequest().getAttributes().get("userid"));
      Users u = userservice.getUsers(userid);
      String fname = u.getLogo();
      // upload picture
      ResourceBundle rb = ResourceBundle.getBundle("config");
      String path = rb.getString("albumpath");
      String fileName = "";
      DiskFileItemFactory factory = new DiskFileItemFactory();
      // Configure the factory here, if desired.
      ServletFileUpload upload = new ServletFileUpload(factory);
      // Configure the uploader here, if desired.
      List fileItems = upload.parseRequest(ServletUtils.getRequest(getRequest()));
      Iterator iter = fileItems.iterator();
      for (; iter.hasNext(); ) {
        FileItem fileItem = (FileItem) iter.next();
        if (fileItem.isFormField()) { // 当前是一个表单项
          System.out.println(
              "form field : " + fileItem.getFieldName() + ", " + fileItem.getString());
        } else {
          // 当前是一个上传的文件
          fileName = fileItem.getName();
          String extension = fileName.substring(fileName.lastIndexOf("."));
          if (fname == null || fname.equals("")) {
            Random random = new Random(10);
            int n = random.nextInt(10000);
            fileName = new Date().getTime() + "-" + n + extension;
          } else fileName = fname;
          fileItem.write(new File(path + fileName));
        }
        // 只处理第一张图片
        break;
      }

      // 生成XML表示
      r = new DomRepresentation(MediaType.TEXT_XML);
      Document doc = r.getDocument();
      Element root = doc.createElement("varkrs");
      root.setAttribute("id", "" + u.getId());
      root.setAttribute("name", u.getUsername());
      root.setAttribute("gender", "" + u.getGender());
      root.setAttribute("grade", "" + u.getGrade());
      root.setAttribute("logo", u.getLogo());
      doc.appendChild(root);
      return r;
    } catch (Exception e) {
      LogDetail.logexception(e);
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      return null;
    }
  }
Beispiel #2
0
  @Get
  public Representation get() {
    try {
      int id = Integer.parseInt(getRequest().getAttributes().get("userid").toString());
      Users u = userservice.getUsers(id);

      // 生成XML表示
      DomRepresentation r = new DomRepresentation(MediaType.TEXT_XML);
      Document doc = r.getDocument();
      Element root = doc.createElement("varkrs");
      root.setAttribute("id", "" + u.getId());
      root.setAttribute("name", u.getUsername());
      root.setAttribute("gender", "" + u.getGender());
      root.setAttribute("grade", "" + u.getGrade());
      root.setAttribute("logo", u.getLogo());
      doc.appendChild(root);
      return r;
    } catch (Exception e) {
      LogDetail.logexception(e);
      getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
      return null;
    }
  }