【问题标题】:PHP: Get file sent from DefaultHTTPClient AndroidPHP:获取从 DefaultHTTPClient Android 发送的文件
【发布时间】:2013-05-22 17:35:55
【问题描述】:

我通过 android 中的 HTTPPost 发送文件:

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
        reqEntity.setContentType("binary/octet-stream");
        reqEntity.setChunked(true); // Send in multiple parts if needed
        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);

在 PHP 中我如何获取文件?

【问题讨论】:

    标签: php android file-upload http-post


    【解决方案1】:

    我不确定您的文件最终会在服务器上的哪个位置。据我所知,您将无法使用$_FILES array 访问它。

    我建议您尝试将文件上传到您的服务器,就像使用 HTML 表单上传文件一样。如果您这样做,您可以使用$_FILES array 处理文件上传。从similar questions on SO 来看,您需要编写大量代码才能使其正常工作......但Android Asynchronous Http Client 对开箱即用的文件上传有很好的支持:

    AsyncHttpClient client = new AsyncHttpClient();
    File myFile = new File("/path/to/file.png");
    RequestParams params = new RequestParams();
    try {
        params.put("profile_picture", myFile);
        client.post("http://www.yourserver.com/upload.php", params, responseHandler);
    }
    catch(FileNotFoundException e) {
        // handle
    }
    

    【讨论】:

    • AsynchHttpClient 在 android 中不存在。需要安装jar文件吗?
    • 是的,它是一个非常受欢迎的第三方库。我链接的页面上有一个绿色的大下载按钮。下载 jar 文件,将其放在项目的 /libs 文件夹中并确保包含它(我认为在 Eclipse 中简单刷新项目就足够了)
    猜你喜欢
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2020-03-14
    • 2021-05-31
    • 1970-01-01
    相关资源
    最近更新 更多