@context对于jersey中post方法的httpServletRequest请求为null

时间:2015-01-08 10:40:05

标签: java android jersey-2.0 jersey-1.0

我从android客户端获取多部分消息。我正在使用jersey webservice来接收多部分数据。我能够检索多部分数据。但我无法使用@Context HttpServletRequest请求获取用户ID。我的android客户端是,

HttpClient httpClient = new DefaultHttpClient();

Log.e("Picture Upload URL is:", QueryConfig.PROTOCOL+ StaticHelper.HOST + StaticHelper.port+QueryConfig.projectService+QueryConfig.sendProfilePicture);

HttpPost postRequest = new HttpPost(QueryConfig.PROTOCOL+ StaticHelper.HOST + StaticHelper.port+QueryConfig.projectService+QueryConfig.sendProfilePicture);

ByteArrayBody bab = new ByteArrayBody(data,StaticHelper.UserID+".jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bab);
reqEntity.addPart("fileFilename",new StringBody(StaticHelper.UserID+".jpg"));
HttpResponse response = httpClient.execute(postRequest);

我的球衣服务是

@Path("/mobileUserPictureInsert")
@POST
@Consumes("multipart/*")
@Produces(MediaType.APPLICATION_JSON)
public String save(@Context HttpServletRequest request, MultiPart multiPart)
        throws ParseException {
    BodyPartEntity bpeTokenId = (BodyPartEntity) multiPart.getBodyParts()
            .get(2).getEntity();
    try {
        tokenId = getString(bpeTokenId.getInputStream());
        String userId = "";
        userId = getSession(tokenId, request);

get session方法是

protected String getSession(String token, HttpServletRequest req)
        throws ServletException, IOException {

    String value = (String) context.getAttribute(token);

    LOG.info("Retrive Token Value-->" + value);

    return value;
}

这里我传递了生成的请求和令牌来检索用户ID。它适用于get方法。但对于post方法我得到空值。我帮帮我。如何在球衣上获得后场方法的请求。

1 个答案:

答案 0 :(得分:0)

您现在正在做的是从ServletContext本身获取context.getAttribute()而不是从HttpServletRequest获取。所以它应该是这样的:String value = (String) req.getAttribute(token)。另请注意。如果不存在给定名称的属性,.getAttribute()方法将返回null。