问题描述
根据此答案"Android使用HTTP多部分表单数据将视频上传到远程服务器" 我会执行所有步骤. >
但是我不知道如何为服务器端编码!我的意思是一个PHP简单的页面,可以为我的敌人提供上载.
另一个问题是:YOUR_URL(以下代码段的第三行)必须是该PHP页面的地址吗?
private void uploadVideo(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a description of the video");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );
// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
System.out.println( EntityUtils.toString( resEntity ) );
} // end if
if (resEntity != null) {
resEntity.consumeContent( );
} // end if
httpclient.getConnectionManager( ).shutdown( );
}
此代码正常工作,我应该使用的PHP代码非常简单:
<?php
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['videoFile']['name']);
if(move_uploaded_file($_FILES['videoFile']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "upload_fail_php_file";
}
?>
注意,该videoFile必须与之完全匹配
reqEntity.addPart("videoFile", filebodyVideo);
您可能面临的 MOST重要问题是服务器配置中的默认值post_max_size
和upload_max_filesize
!由于默认值太小,并且当您尝试上传大文件时,PHP脚本返回:"upload_fail_php_file" ,没有错误或引发异常. 所以请记住,将这些值设置得足够大...
享受编码.
According this answer "Android upload video to remote server using HTTP multipart form data" I do all steps.
But I don't know how I code for server side! I mean a PHP simple page that serve my reauest foe upload.
And another question is that : YOUR_URL (3rd line of following snippet) must be address of that PHP page?
private void uploadVideo(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a description of the video");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );
// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
System.out.println( EntityUtils.toString( resEntity ) );
} // end if
if (resEntity != null) {
resEntity.consumeContent( );
} // end if
httpclient.getConnectionManager( ).shutdown( );
}
This code worked properly and PHP code I should use is as simple as this:
<?php
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['videoFile']['name']);
if(move_uploaded_file($_FILES['videoFile']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "upload_fail_php_file";
}
?>
NOTE that videoFile must match exactly with
reqEntity.addPart("videoFile", filebodyVideo);
And the MOST Important problem you probably face to, is default value of post_max_size
and upload_max_filesize
in the server config! As default is too small and when you try to upload large files, the PHP script return : "upload_fail_php_file" with no error or exception throwing. So remember to set these values as big as enough...
Enjoy coding.
这篇关于Android:使用MultipartEntity上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!