【问题标题】:Retrofit - upload video to XAMPP改造 - 将视频上传到 XAMPP
【发布时间】:2023-03-13 07:23:01
【问题描述】:

我是 Retrofit 的新手,过去 2 天遇到了一个大问题。我想将视频从我的设备摄像头发送到 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());
        }
    });

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

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

问题是,日志中没有错误。但我确切地知道,问题一到达 move_uploaded_file($_FILES["filename"]["tmp_name"]

【问题讨论】:

    标签: php post video upload retrofit


    【解决方案1】:

    好的,我终于找到了问题和解决方案。 首先,我的 XAMPP 上的 videos 目录是只读的。我使用的是 Mac,并通过 Get Info

    将文件夹的 Sharing and Permissions 属性更改为 Read and Write

    其次,我找到了一种将密钥文件对映射到我的 php 代码的方法,“文件名”是我在 move_uploaded_file($_FILES["filename"]["tmp_name"] 中的密钥: 接口:

    @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
    

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 2016-02-14
      • 2019-10-08
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多