php - 安卓 : Upload large files using MultipartEntity

标签 php android upload multipartform-data multipart

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

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

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

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( );
}

最佳答案

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

<?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",没有错误或异常抛出。 所以记住将这些值设置得足够大...

享受编码。

关于php - 安卓 : Upload large files using MultipartEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23504191/

相关文章:

java - 从网络摄像头代码将图片上传到 Twitter 每次都上传相同的照片

javascript - 使用 javascript 阻止当前用户 IP

java - 网络请求 C#、Java

android - 如何根据数据库值在 30 分钟前设置多个本地通知?

android - 如果我们必须在 android 中使用 viewpager 创建多个页面,那么我们是否必须创建多个 fragment ?还有其他选择吗?

java - 使用 Java Spring RestTemplate 上传大型二进制文件

javascript - 为什么 FormData 是空的?

php - Laravel UUID 及其独特性?

php - 从表中删除,其中 UID = x AND CAT = y

php - mod_fcgid : HTTP request length 138520 (so far) exceeds MaxRequestLen (131072)