我在使用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
}