繁体   English   中英

在参数为file的android中使用翻新功能上传视频

[英]uploading a video using retrofit in android where parameter is file

我一直在遵循各种教程,并尝试使用android中的改造将视频上传到我的服务器。 我唯一需要的参数如下图所示 在此处输入图片说明

即使增加了超时时间,我仍然会收到超时异常。 这是我的上传视频代码。

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .build();
    Log.v("test_get", "get the file");
    //MultipartBody.Part vFile = MultipartBody.Part.createFormData("video", videoFile.getName(), videoBody);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://xxxx:xxx/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();

    SmileVideoAPI service = retrofit.create(SmileVideoAPI.class);
    MediaType MEDIA_TYPE = MediaType.parse("video/mp4");
    File videoFile = new File(pathToVideoFile);
    RequestBody videoBody = RequestBody.create(MEDIA_TYPE, videoFile);
    Log.v("test_get", "before uploading");
    Call<ResponseBody> call = service.uploadVideo("desc", videoBody);
    Log.v("test_get", "after uploading");
    call.enqueue(new Callback<ResponseBody>(){
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful())
            {
                Log.i("mok","S");
                ResponseBody rb = response.body();
                Log.i("mok",rb.toString());
            }
            else {
                Log.i("mok", "F");
                ResponseBody rb = response.errorBody();
            }
        }
        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.printStackTrace();
            Log.i("mok",t.getCause()+"");
            Log.i("mok","T");
            finish();
        } 
    });
    return msg;

    I have been trying taking reference from this post : 

使用翻新2上传视频

如果您想上传文件(任何类型),这里是一种适用于Android的PHP方法,效果很好。

您需要服务器上的脚本来接收文件:

uploadfile.php

<?php
$fname = $_POST['filename'];
$target_path = "/yourserverpath/".$fname;
$upload_path = $_FILES['uploadedfile']['tmp_name'];
If (move_uploaded_file($upload_path, $target_path)) {
    echo "Moved";
} else {
    echo "Not Moved";
}
?>

您可以通过浏览器在服务器上使用以下HTML测试器文件在服务器上测试您的uploadfile.php脚本:

Uploadfile.html:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: 
<input name="uploadedfile" type="file" /><br />
Filename on server:
<input name="filename" type="text" /><br />
<br />
<input type="submit" value="Upload File" />
</form>

一旦服务器端开始从浏览器上传文件,那么Android部分就非常简单了:

Uploader处理文件的上传。 此类在服务器上调用uploadfile.php脚本。

上载文件可能很棘手,但是我们拥有一个秘密武器,可以使上传变得更加容易。 我们将使用DefaultHttpClient,它使我们能够访问MultipartEntity方法。 使用HttpUrlConnection时,此方法不可用。 fileUpload类位于AsyncTask中。 在我的示例中,selectedPicName只是您的文件名。 postURL是前面提到的脚本的完整URL。

private class fileUpload extends AsyncTask<Void, String, Void> {
    protected Void doInBackground(Void... unused) {
        // upload new picture to the server        
        String postURL = uploadFilesScript;
        File file = new File(path2, selectedPicName);
        // upload the new picture
        Uploader(postURL, file, selectedPicName);
    } 
}

Uploader类如下:(需要注意的是,“ uploadedfile”名称必须与接收上载的PHP脚本中的变量匹配。)

public static void Uploader(String postURL, File file, String fname) {
    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, 
                                            HttpVersion.HTTP_1_1);
        HttpPost httppost = new HttpPost(postURL);
        // the boundary key below is arbitrary,
        // it just needs to match the MPE so it can decode multipart correctly
        httppost.setHeader("Content-Type", "multipart/form-data; boundary=--32530126183148");
        MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
                                                       "--32530126183148", 
                                                       Charset.forName("UTF-8"));
            mpEntity.addPart("uploadedfile", new FileBody((file), "application/txt"));
            mpEntity.addPart("MAX_FILE_SIZE", new StringBody("100000"));
            mpEntity.addPart("filename", new StringBody(fname));
        }
        httppost.setEntity(mpEntity);
        HttpResponse response;
        response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
          if (resEntity != null) {
                resEntity.consumeContent();
                Log.v("UPLOAD", "resEntity=" + resEntity.toString());
            }
        httpclient.getConnectionManager().shutdown();
    } catch (Throwable e) {
    }
}

MultipartEntity允许我们使用addPart方法简单地指定文件名和文件内容。 在下面的示例中,变量名与服务器上接收PHP脚本所期望的变量名匹配。

如果必须使用HttpUrlConnection类完成此上载功能,则将很复杂,因为我们必须创建自己的文件处理包装器。 MultipartEntity还允许我们指定字符集和最大文件大小。 需要提供一个唯一但任意的边界字符串,该字符串必须与服务器脚本匹配。

我在生产应用程序中一直使用这种方法,并且效果很好。

全面披露-我在第5章的服务器微调器应用程序中写了一本书,名为《 Android软件开发:实用项目和上载的集合》 ,但这只是上传视频所需的全部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM