0

我通过 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 中如何获取文件?

4

1 回答 1

0

I'm not really sure where your file would end up on the server. As far as I can tell you won't be able to access it using the $_FILES array.

I would suggest that you try to upload the file to your server the same way as it would be uploaded using an HTML form. If you do it that way you can handle the file upload using the $_FILES array. Judging from similar questions on SO there's a ton of code you need to write to get this to work though... BUT Android Asynchronous Http Client has good support for file uploads out of the box:

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
}
于 2013-05-22T18:06:36.047 回答