改造 - 将视频上传到XAMPP

时间:2017-11-04 14:17:57

标签: php post video upload retrofit

我是Retrofit的新手,在过去的两天里遇到了很大的问题。我想将设备相机中的视频发送到XAMPP服务器。

应该移动上传视频的php部分:

$returnArray = array();
$videoUploadResult = "";

$target_dir ="/Applications/XAMPP/xamppfiles/htdocs/Projects/Eventtest/videos";
if(!file_exists($target_dir)) {
   mkdir($target_dir, 0777, true);
}

$target_file_name = $target_dir . "/" . basename($_FILES["filename"]["name"]);


if(move_uploaded_file($_FILES["filename"]["tmp_name"], $target_file_name)) {
    $returnArray["video_upload_status"] = "Video uploaded successfully";
} else {
    $returnArray["status"] = 400;
    $returnArray["message"] = "Couldn't upload the video";

    echo json_encode($returnArray);
}
exit;

接口:

public interface ServerInterface {
   @GET("getEvents.php")
   Call<List<JSONData>> getEvent(@Query("result") String tag);

   @POST("createEvent.php")
   //@FormUrlEncoded
   @Multipart
   Call<ResponseBody> uploadVideo(@Part("description") RequestBody description, @Part MultipartBody.Part file); 

代码:

ServerInterface = APIClient.getClient().create(ServerInterface.class);


    RequestBody requestFile =
            RequestBody.create(
                    MediaType.parse("video/mp4"),
                    videoFile
            );
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("filename", videoFile.getName(), requestFile);

    // add another part within the multipart request
    String descriptionString = "hello, this is description speaking";
    RequestBody description =
            RequestBody.create(
                    MultipartBody.FORM, descriptionString);

    // finally, execute the request
    Call<ResponseBody> call = serverInterface.uploadVideo(description, body);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call,
                               Response<ResponseBody> response) {
            Log.v("Upload", "success");
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });

当我使用密钥“filename”通过Postman发送视频文件时,php服务器部分工作,如move_uploaded_file($ _ FILES [“filename”] [“tmp_name”]。

我尝试了不同的例子,这个例子来自https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server 我也尝试发送带字符串和文件的Map,但没有成功。

问题是,日志中没有错误。但我确切地知道,一旦到达move_uploaded_file就会出现问题($ _ FILES [“filename”] [“tmp_name”]

1 个答案:

答案 0 :(得分:0)

好的,我终于找到了问题和解决方案。 首先,我的XAMPP上的目录视频是只读的。我使用Mac并通过获取信息

将文件夹的共享和权限属性更改为读取和写入

其次,我找到了一种将密钥文件对映射到我的php代码的方法,&#34; filename&#34;是 move_uploaded_file($ _ FILES [&#34; filename&#34;] [&#34; tmp_name&#34;] 的关键: 接口:

@POST("createEvent.php")
@Multipart
Call<ResponseBody> uploadVideo(@Part MultipartBody.Part file, @Part("filename") RequestBody name);

代码:

    serverInterface = APIClient.getClient().create(ServerInterface.class);
    RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), videoFile);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("filename", videoFile.getName(), requestBody);
    RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), videoFile.getName());


    Call<ResponseBody> call = serverInterface.uploadVideo(fileToUpload, filename);
call.enqueue(......) // onResponse(), onFailure() goes here
enter code here