Android上传文件用php接收

废话不多说直接上代码

导包

    implementation 'com.loopj.android:android-async-http:1.4.11'

http声明

 

<application
        android:usesCleartextTraffic="true"

 

函数

复制代码
public void uploadFile() throws FileNotFoundException {

        AsyncHttpClient client=new AsyncHttpClient();
        RequestParams params=new RequestParams();
        String filePath="/data/data/com.example.android_uploadfile/cache/cxk.jpg";
        params.put("data",new File(filePath));
        client.post(this, "http://XXX.com/XXX.php", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
                System.out.println(new String(responseBody));
            }
            @Override
            public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
                System.out.println(new String(responseBody));
            }
        });
    }
复制代码

修改上面代码中加粗的部分

php代码

<?php
header("Content-Type: text/html;charset=utf-8");
$fileInfo=$_FILES['data'];//获取提交过来的文件
$filename=$fileInfo['name'];//获取文件名
$filePath=$fileInfo['tmp_name'];//获取文件临时目录
$name = iconv('utf-8', 'gb2312', $filename);//把文件名重新编码,避免吗中文乱码
move_uploaded_file($filePath,"./".$name);
echo($filename);

Github地址 下载前给star

 

posted @   Z_Chan  阅读(101)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示