首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不同文件格式的Android HTTP Post文件

不同文件格式的Android HTTP Post文件
EN

Stack Overflow用户
提问于 2014-03-19 12:02:11
回答 1查看 96关注 0票数 0

目前,我可以使用http://www.codeofaninja.com/2013/04/android-http-client.html中的以下代码片段对文本文件和图像文件进行HTTP Post

Android部分:

代码语言:javascript
运行
复制
// 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部分:

代码语言:javascript
运行
复制
<?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发布其他文件格式,如加密文件等,它会失败,并且文件不会发布到后端。

你知道问题出在哪里吗?

EN

回答 1

Stack Overflow用户

发布于 2014-03-19 16:08:53

在php.ini文件中,更改以下值:

upload_max_filesize和post_max_size

根据预期的文件大小将其更改为更合适的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22495954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档