目前,我可以使用http://www.codeofaninja.com/2013/04/android-http-client.html中的以下代码片段对文本文件和图像文件进行HTTP Post
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部分:
<?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);
}
?>使用上面的代码片段,可以毫无问题地完成文本文件和图像的HTTP Post。但是,如果我尝试HTTP发布其他文件格式,如加密文件等,它会失败,并且文件不会发布到后端。
你知道问题出在哪里吗?
发布于 2014-03-19 16:08:53
在php.ini文件中,更改以下值:
upload_max_filesize和post_max_size
根据预期的文件大小将其更改为更合适的值。
https://stackoverflow.com/questions/22495954
复制相似问题