0

嗨,我需要将文件发送到服务器我使用此代码

public void PostFile() {

        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        // httpclient.
        HttpPost httppost = new HttpPost("http://fastcalc.orionsource.ru/test.php");


        File file = cache.getFileFromCache("citys.json");

        FileEntity reqEntity = new FileEntity(file,"text/plain");
        reqEntity.setContentType("text/plain");
        httppost.setEntity(reqEntity);



        System.out.println("executing request " + httppost.getRequestLine());

        HttpResponse response;
        try {
            response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();

            System.out.println(response.getStatusLine());
            if (resEntity != null) {
                System.out.println(EntityUtils.toString(resEntity));
            }
            if (resEntity != null) {
                resEntity.consumeContent();
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpclient.getConnectionManager().shutdown();
    }

在下面的服务器端代码

<?php
print 'Files:';
print_r($_FILES);
print '<br><br>';
print "Response:\n";
print_r($_REQUEST);
print '<br><br>';

?>

在Android上我得到回应

05-29 17:26:29.232: I/System.out(26823): executing request POST http://fastcalc.orionsource.ru/test.php HTTP/1.1
05-29 17:26:29.732: I/System.out(26823): HTTP/1.1 200 OK
05-29 17:26:29.732: I/System.out(26823): Files:Array
05-29 17:26:29.732: I/System.out(26823): (
05-29 17:26:29.732: I/System.out(26823): )
05-29 17:26:29.732: I/System.out(26823): <br><br>Response:
05-29 17:26:29.732: I/System.out(26823): Array
05-29 17:26:29.732: I/System.out(26823): (
05-29 17:26:29.732: I/System.out(26823): )
05-29 17:26:29.732: I/System.out(26823): <br><br>

我不能使用 MultipartEntity,因为它不包含在任何 Android 版本中。它的第三方库。

有人给请链接到仅使用 FileEntity 的教程。

4

1 回答 1

0

嗨,我尝试使用图像文件在服务器上发送。您可以做一件事将文件转换为 base64 字符串并将其发送到服务器,然后从服务器端尝试将 base64 转换为文件

作为参考如何转换为 base64 我的答案:如何将 ImageView 中的图像发送到 php 服务器?

于 2012-05-29T13:41:31.533 回答