我从安卓客户端接收到了多部分消息。我使用Jersey Web服务来接收这个多部分数据。我能够检索到多部分数据,但是我无法使用@Context HttpServletRequest
请求获取用户ID。我的安卓客户端如下:
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);
获取会话方法是:
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方法我得到了空值。请帮助我。如何在Jersey中获取POST方法的请求。