android httpurlconnection和jersey服务器formdata玩得不好

时间:2016-11-22 09:37:36

标签: android jersey httpurlconnection multipartform-data jersey-2.0

我在使用HttpUrlConnection到jersey服务器的android中进行文件上传时出现问题。

将Android代码更改为jersey http客户端不是选项

我得到了400 Bad Request - The request sent by the client was syntactically incorrect.

Android代码(无论如何):

    final String BOUNDARY = "===" + System.currentTimeMillis() + "===";
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY + "; charset=" + ENCODING);
OutputStream os = conn.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, ENCODING), true);
for (String key : request.getPOSTParams().keySet())
{
    writer.append("--").append(BOUNDARY).append(LINE_FEED);
    writer.append("Content-Disposition: form-data; name=\"").append(key).append("\"").append(LINE_FEED);
    writer.append("Content-Type: text/plain; charset=" + ENCODING).append(LINE_FEED);
    writer.append(LINE_FEED);
    writer.append(request.getPOSTParams().get(key)).append(LINE_FEED);
    writer.flush();
}

服务器代码:

@POST
@Path("/upload")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response upload(@NotNull @FormDataParam("photo") InputStream fileInputStream,
                       @NotNull @FormDataParam("photo") FormDataContentDisposition fileMetaData,
                       @FormDataParam("tags") String[] tags, @NotNull @FormDataParam("geoLongitude") Double geoLongitude,
                       @NotNull @FormDataParam("geoLatitude") Double geoLatitude,
                       @DefaultValue("false") @FormDataParam("isProfilePhoto") boolean isProfilePhoto,
                       @FormDataParam("ref_photo_id") Long refPhotoId)
{
    //other code here
}

0 个答案:

没有答案