简体   繁体   English

具有不同文件格式的Android HTTP Post文件

[英]Android HTTP Post Files with different file formats

Currently, I could HTTP Post text files and Image files using the following code snippets from http://www.codeofaninja.com/2013/04/android-http-client.html 目前,我可以使用来自http://www.codeofaninja.com/2013/04/android-http-client.html的以下代码段通过HTTP发布文本文件和图像文件

Android portion: Android部分:

// the file to be posted
String textFile = Environment.getExternalStorageDirectory() + "/sample.txt";
Log.v(TAG, "textFile: " + textFile);

// the URL where the file will be posted
String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php";
Log.v(TAG, "postURL: " + postReceiverUrl);

// new HttpClient
HttpClient httpClient = new DefaultHttpClient();

// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);

File file = new File(textFile);
FileBody fileBody = new FileBody(file);

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", fileBody);
httpPost.setEntity(reqEntity);

// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

    String responseStr = EntityUtils.toString(resEntity).trim();
    Log.v(TAG, "Response: " +  responseStr);

    // you can add an if statement here and do other actions based on the response
}

PHP portion: PHP部分:

<?php
// if text data was posted
if($_POST){
    print_r($_POST);
}

// if a file was posted
else if($_FILES){
    $file = $_FILES['file'];
    $fileContents = file_get_contents($file["tmp_name"]);
    print_r($fileContents);
}
?>

Using the above code snippets HTTP Post of textfiles and Images could be done without any issue. 使用上面的代码片段,可以完成文本文件和图像的HTTP发布,而不会出现任何问题。 But if I try to HTTP Post other file formats like encrypted file etc. It fails and the file doesn't get posted to the backend. 但是,如果我尝试通过HTTP发布其他文件格式(例如加密文件等),则会失败,并且该文件不会发布到后端。

Any idea what's the issue? 知道是什么问题吗?

Thanks In Advance 提前致谢

Ok managed to solve it. 确定设法解决了。

In your php.ini file change the following values: 在您的php.ini文件中,更改以下值:

upload_max_filesize & post_max_size upload_max_filesize和post_max_size

Change it to a more appropriate values depending on the expected filesize. 根据预期的文件大小将其更改为更合适的值。

And it could work!! 它可以工作!

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

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