问题描述
|
我发现有1条关于该问题的帖子,确实部分回答了该问题,但是我恐怕需要一些细节。
我目前正在尝试将BlobStore与我的android应用程序配合使用,除了501错误(HTTP服务器无法处理您的请求)之外,我什么也无法获得。
他是我的密码;
HttpPost httpPostImg = new HttpPost(url);
Header header = new BasicHeader(\"Content-Type\",\"multipart/form-data\");
Header h = new BasicHeader(\"Connection\",\"keep-alive\");
multipartentity entity = new multipartentity(HttpMultipartMode.broWSER_COMPATIBLE);
FormBodyPart form = new FormBodyPart(\"myFile\",new ByteArrayBody(image,\"multipart/form- data\",\"pict.jpeg\"));
entity.addPart(form);
httpPostImg.setEntity(entity);
httpPostImg.setHeader(header);
httpPostImg.setHeader(h);
response = httpClient.execute(httpPostImg);
processResponse(response);
我通过运行良好的GET请求获得了URL。我还尝试了一个包含ByteArrayBody的FormBodyPart,并将ByteArrayBody的mime类型设置为\“ multipart / form-data \”,但没有任何效果。我总是收到501错误(服务器无法处理您的请求)。
谢谢,所有的答案表示赞赏。
解决方法
好吧,经过一番搜索,这是解决方案;
标题似乎没有按我预期的那样工作,因此我只删除了它们。
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart(\"data\",new ByteArrayBody(image,\"image/jpeg\",\"avatar.jpg\"));
httppost.setEntity(entity);
response = httpClient.execute(httppost);
processResponse(response);
, 我使用以下内容,并且有效。
如果您以gzip内容发布,这是一个示例:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(baos);
gzos.write(xml.getBytes());
} finally {
if (gzos != null)
try {
gzos.close();
} catch (IOException ex) {
}
}
byte[] fooGzippedBytes = baos.toByteArray();
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart(\"name\",new ByteArrayBody(fooGzippedBytes,\"application/x-gzip\",\"filename\"));
httpPost.setEntity(entity);