php - 将视频从 Android 上传到 PHP 的代码

标签 php android image-uploading

我正在尝试将视频从 Android 代码上传到我的 PHP 网络服务器代码,但无法成功。我指的是以下 link执行上传任务,但我在我的 Android 代码中收到以下响应,但在 PHP 服务器上找不到任何文件。

安卓响应

DEBUG/ServerCode(29484): 200
DEBUG/serverResponseMessage(29484): OK

我检查了很多东西,比如在 php.ini 中设置值文件。 尽管我可以从 Android 代码将图像上传到服务器,因为我发送的是 64 位编码的 64 位编码 ByteArray并且在 php 中有一个现成的函数,它可以从编码的 ByteArray 创建图像

但是如果任何其他文件的类型不是图像,该代码也不起作用。

如果你们以前做过类似的事情,请指导我。

我正在使用的 PHP 代码:

<?php

    $target_path  = "./upload/";

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
    {
        echo "The file ".basename( $_FILES['uploadedfile']['name'])." has been uploaded";
    } 
    else
    {
        echo "There was an error uploading the file, please try again!";
    }
?>

我正在使用的安卓代码:

public void videoUpload()
{
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;


    String pathToOurFile = "/sdcard/video-2010-03-07-15-40-57.3gp";
    String urlServer = "http://10.0.0.15/sampleWeb/handle_upload.php";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary");

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile );
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    int serverResponseCode = connection.getResponseCode();
     String serverResponseMessage = connection.getResponseMessage();
     Log.d("ServerCode",""+serverResponseCode);
     Log.d("serverResponseMessage",""+serverResponseMessage);
    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

最佳答案

我的 java 有点笨拙但是......

看起来 connection.getResponseCode 仅返回 HTTP 状态代码,而 connection.getResponseMessage 仅返回 HTTP 状态消息 - 但您的 PHP 不执行任何操作这些值的操作。您可以尝试:

$target_path  = "./upload/";
$src = $_FILES['uploadedfile']['name'];

$target_path .= basename($src);

if(file_exists($src) 
        && 
        && move_uploaded_file($src, $target_path)
   ) {
    echo "The file ".basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else {
    header("Server Error", true, 503);
    echo "There was an error uploading the file, please try again!";
    $msg = "src size ? " 
       . filesize($src) . "\n dest dir writable ?"
       . is_writeable(dirname($target_path)) ? "Y\n" : "N\n"
       . "FILES contains :\n";
       . var_export($_FILES,true);
    // now write $msg somwhere you can read it
}

这应该有助于缩小错误范围

关于php - 将视频从 Android 上传到 PHP 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4944650/

相关文章:

Angularjs多图像上传与裁剪

PHP:我怎么知道函数的调用者?

php - 使用 PHP 拆分 mySQL 查询结果

php - Photoshop 中是否有类似 Blob 修复工具的 JQuery 插件?

android - 将 Canvas 转换为 Drawable

javascript - React JS 图像 uploader |更改背景图片

android - Retrofit2 多部分上传图像和其他数据

php - 如何安装供所有用户全局使用的 Python 包(包括 www-data)

android - 如何解决框架布局中的内存泄漏问题?

android - 为什么我收到后端内部错误 : "Exception during IR lowering error" when using Jetpack Compose clickable-Modifier?