Android改造2,选择视频并上传到服务器

时间:2018-08-28 22:52:50

标签: android retrofit2 slim-3

我正在尝试学习android,现在还可以,现在还不错。试图扩展我的技能。

因此,我正在尝试将视频进行翻新后上传到服务器。我的改造工作可以上传EditText等。我在这里使用的IP地址与此处相同。
在代码中,摄像头可以很好地记录视频并重命名视频,我相信选择视频可以正常工作(对此不完全确定,但是画廊打开了,我选择了一个视频,并且它没有崩溃,所以...,PHP允许我使用邮递员发布视频,但是当我按下上传按钮时崩溃。我不确定问题出在获取真实路径,翻新代码中还是我的文件选择不起作用?我需要更好的编码器来帮助我。我已经在堆栈上寻找了好几天,尝试了多个示例,只是没有使其正常工作。 我无法弄清楚错误。我试图在上面扔些吐司,看我是否出错,但我从没吐司。只是崩溃。 这是我的代码。我至少正在尝试,给我扔骨头。

Slim / PHP索引

sed -E 's|^Link[\s]*([a-zA-Z\/\-]*)[\s]*([a-zA-Z\/\-]*)$|"\2" : "\1"|' ./backward

uploads.php

  $app->post('/saveFile', function(Request $request, Response $response){

 $response = array();

if (isset($_POST['desc']) && ($_POST['ID']) && strlen($_POST['desc']) > 0 && 
$_FILES['image']['error'] === UPLOAD_ERR_OK) {

$upload = new uploads();

            $file = $_FILES['image']['tmp_name'];

            $desc = $_POST['desc'];
            $ID = $_POST['ID'];  

            if ($upload->saveFile($ID, $file, 
 getFileExtension($_FILES['image']['name']), $desc)) {
                $response['error'] = false;
                $response['message'] = 'File Uploaded Successfullly';
            }

         else {
            $response['error'] = true;
            $response['message'] = 'Required parameters are not available';
        }


        echo json_encode($response);

 } 
});

 function getFileExtension($file)
{
$path_parts = pathinfo($file);
return $path_parts['extension'];
}

API接口

 class uploads
{

 private $con;

 public function __construct()
 {
    require_once dirname(__FILE__) . '/DbConnect.php';

    $db = new DbConnect();
    $this->con = $db->connect();
}


public function saveFile($ID, $file, $extension, $desc)
{
    $name = round(microtime(true) * 1000) . '.' . $extension;
    $filedest = dirname(__FILE__) . UPLOAD_PATH . $name;
    move_uploaded_file($file, $filedest);

    $url = $server_ip = gethostbyname(gethostname());

    $stmt = $this->con->prepare("INSERT INTO images (ID, description, url) 
 VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $ID, $desc, $name);
    if ($stmt->execute())
        return true;
else

    return false;
}


}

我的活动课

@Multipart
@POST("saveFile")
Call<MyResponse> uploadImage(@Part MultipartBody.Part file, @Part("desc") 
RequestBody desc, @Field("ID") String ID);

1 个答案:

答案 0 :(得分:0)

您的控制器方法完全错误,您必须使用Request / Response对象,然后返回修改后的Response;我的版本更接近于预期的用途,但这并不是完美的方法,主要是因为我没有android工具。

我希望它能对您有所帮助,更多信息在这里: Request UploadedFiles HttpStatusCodes

    $app->post('/saveFile', function(\Slim\Http\Request $request, \Slim\Http\Response $response){

    $responseData = [];
    $responseCode = 400;

    /** @var \Psr\Http\Message\UploadedFileInterface[] $files */
    $files    = $request->getUploadedFiles();
    /** @var string[] $postData */
    $postData = $request->getParsedBody();

    if ( !empty($postData['desc']) && ($postData['ID']) && !empty($files)) {
        $upload = new uploads();

        // use this if you are only upload one file
        // otherwise loop though them all
        $file = reset($files);

        $desc = $postData['desc'];
        $ID   = $postData['ID'];

        // this means the file was uploaded ok
        if ( $file->getError() === UPLOAD_ERR_OK ) {
            if ( $upload->saveFile($ID, $file, $file->getClientMediaType(), $desc) ) {
                $responseData['error']   = false;
                $responseData['message'] = 'File Uploaded Successfully';

                $responseCode = 200;
            }

            else {
                $responseData['error']   = true;
                $responseData['message'] = 'Required parameters are not available';

                $responseCode = 406;
            }
        }
        else {
            $responseData['error']   = true;
            $responseData['message'] = 'File upload error: '. $file->getError();

            $responseCode = 409;
        }
    }

    return $response->withJson($responseData, $responseCode);
});

上传修改

class uploads
{

    private $con;

    public function __construct()
    {
        // you shouldn't need this if you  are using Slim
        // and you are doing require_once '/vendor/autoload.php' at the beginning of your 
        // index.php
        require_once dirname(__FILE__) . '/DbConnect.php';

        $db = new DbConnect();
        $this->con = $db->connect();
    }


    public function saveFile($ID, \Psr\Http\Message\UploadedFileInterface $file, $extension, $desc)
    {
        $name     = round(microtime(true) * 1000) . '.' . $extension;
        $filedest = dirname(__FILE__) . UPLOAD_PATH . $name;

        $file->moveTo($filedest);

        $url = $server_ip = gethostbyname(gethostname());

        $stmt = $this->con->prepare("INSERT INTO images (ID, description, url) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $ID, $desc, $name);

        return $stmt->execute();
    }
}