MultipartEntity文件无法通过设备上传

问题描述 投票:0回答:1

可能这是一个重复的问题,但我已经尝试了这个网站的所有答案,但没有一个工作!

我正在使用MultipartEntity进行图片上传。它在模拟器中工作得很好但是当我检查设备时它无法正常工作。

在我的代码下面

 HttpParams params = new BasicHttpParams();
 params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP);    

 HttpClient httpClient = new DefaultHttpClient(params);
 HttpContext localContext = new BasicHttpContext();
 HttpPost httpPost = new HttpPost("http://url.com/webservice_uploadvideo.php");

 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);

 FileBody filebodyVideopre = new FileBody(new File(videoUploadpathPre)); //pre

 entity.addPart("fin_detail", filebodyVideopre);

 httpPost.setEntity(entity);

 HttpResponse response = httpClient.execute(httpPost, localContext);
java android httpclient
1个回答
1
投票

这是我的多部分图像上传的工作代码。是的,它适用于真实设备。

private int uploadImage(String selectedImagePath) {

  try {
    HttpClient client = new DefaultHttpClient();
    File file = new File(selectedImagePath);
    HttpPost post = new HttpPost(Constants.URL);

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
                    Constants.BOUNDARY, Charset.defaultCharset());

    entity.addPart(Constants.MULTIPART_FORM_DATA_NAME, new FileBody(file));
    post.setHeader("Accept", "application/json");
    post.setHeader("Content-Type", "multipart/form-data; boundary=" + Constants.BOUNDARY);

    post.setEntity(entity);

    HttpResponse response = client.execute(post);
    HttpEntity httpEntity = response.getEntity();

    int status = response.getStatusLine().getStatusCode();

    return status;

  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.