首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MultipartEntity文件不通过设备上传

MultipartEntity文件不通过设备上传
EN

Stack Overflow用户
提问于 2016-04-21 10:28:15
回答 1查看 694关注 0票数 0

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

我正在使用MultipartEntity进行图像上传。它在仿真器中完全正常工作,但是当我签入设备时,它就不能工作了。

在我的代码下面

代码语言:javascript
复制
 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-21 10:35:20

这是我的多部分图像上传的工作代码。是的,它在真正的设备中工作。

代码语言:javascript
复制
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;
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36766819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档