使用POST上传图像byte []

时间:2011-10-31 14:56:31

标签: java android

如何使用帖子请求发送带有帖子请求的图片或视频,其中包含其他参数和帖子请求?我有byte []格式的图像和视频,我正在尝试

  HttpClient httpClient = new DefaultHttpClient();
            http.setURI(URI.create(url));
            MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
                  ByteArrayBody bab = new ByteArrayBody(a.getData(),
                      a.getQuestionId() + ".jpg");
                  reqEntity.addPart("type", new StringBody("photo"));            
                  reqEntity.addPart("data", bab);
 http.setEntity(reqEntity);
              HttpResponse response = httpClient.execute(http);

它通过代码,但它不上传任何东西(bab!= null)。有没有人知道出了什么问题,我无法提供更多信息,其他团队在ws?

1 个答案:

答案 0 :(得分:1)

您的代码应该看起来更像这样:

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayBody bab = new ByteArrayBody(a.getData(), a.getQuestionId() + ".jpg");
reqEntity.addPart("type", new StringBody("photo"));            
reqEntity.addPart("data", bab);

HttpPost postMethod = new HttpPost(url);
postMethod.setEntity(reqEntity);

HttpClient httpClient = new DefaultHttpClient():
HttpResponse response = httpClient.execute(postMethod);