首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android :使用MultipartEntity上传大型文件

Android :使用MultipartEntity上传大型文件
EN

Stack Overflow用户
提问于 2014-05-06 20:26:40
回答 1查看 3.6K关注 0票数 3

根据这个答案,"Android upload video to remote server using HTTP multipart form data",我做了所有步骤。

但是我不知道我是如何为服务器端编码的!我的意思是一个PHP简单的页面,为我最忠实的敌人上传服务。

另一个问题是: YOUR_URL (以下代码段的第3行)必须是该PHP页面的地址?

代码语言:javascript
复制
private void uploadVideo(String videoPath) throws ParseException, IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(YOUR_URL);

    FileBody filebodyVideo = new FileBody(new File(videoPath));
    StringBody title = new StringBody("Filename: " + videoPath);
    StringBody description = new StringBody("This is a description of the video");

    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("videoFile", filebodyVideo);
    reqEntity.addPart("title", title);
    reqEntity.addPart("description", description);
    httppost.setEntity(reqEntity);

    // DEBUG
    System.out.println( "executing request " + httppost.getRequestLine( ) );
    HttpResponse response = httpclient.execute( httppost );
    HttpEntity resEntity = response.getEntity( );

    // DEBUG
    System.out.println( response.getStatusLine( ) );
    if (resEntity != null) {
      System.out.println( EntityUtils.toString( resEntity ) );
    } // end if

    if (resEntity != null) {
      resEntity.consumeContent( );
    } // end if

    httpclient.getConnectionManager( ).shutdown( );
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-06 21:47:02

这段代码正常工作,我应该使用的PHP代码非常简单:

代码语言:javascript
复制
<?php

    $file_path = "uploads/";

    $file_path = $file_path . basename( $_FILES['videoFile']['name']);
    if(move_uploaded_file($_FILES['videoFile']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "upload_fail_php_file";
    }
 ?>

注意到,videoFile必须与

reqEntity.addPart("videoFile", filebodyVideo);

最重要的问题可能是服务器配置中post_max_sizeupload_max_filesize的默认值!由于缺省值太小,并且当您试图上传大型文件时,PHP脚本将返回:"upload_fail_php_file“,没有错误或异常抛出。所以记住把这些值设置得足够大..。

享受编码吧。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23504191

复制
相关文章

相似问题

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