• Android 上传图片并添加参数 PHP接收


    php端接收代码:

        public function get_file(){
            $local_path  = "./Public/daixu_picture/figure/";//服务器文件的存放路径
            $img_name = basename( $_FILES['uploadedfile']['name']);//服务器中的图片名(uploadedfile是键值名,可自行设定)
            $target_path = $local_path.$img_name;
            $result = move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
            if($result) {
                echo "上传成功";
            } else{
                echo "上传失败";
            }
        }

    android端代码:

    /*
        * 作用:上传图片,并携带参数
        * 传入参数:http_url(服务器目标地址),filepath(本机图片的地址)
        */
        public void uploadImage(final String http_url, final String filepath){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        File file = new File(filepath);
    
                        if (!file.exists()) {
                            Log.i("错误", "文件不存在");
                        }
    
                        HttpClient client = new DefaultHttpClient();
    
                        HttpPost post = new HttpPost(http_url);
    
                        FileBody fileBody = new FileBody(file, "image/jpeg");
                        MultipartEntity entity = new MultipartEntity();
    
                        entity.addPart("uploadedfile", fileBody);//uploadedfile是图片上传的键值名
                        entity.addPart("key_app", new StringBody("1",Charset.forName("UTF-8")));//设置要传入的参数,key_app是键值名,此外传参时候需要指定编码格式
    
                        post.setEntity(entity);
    
                        HttpResponse response = client.execute(post);
    
                        if (response.getStatusLine().getStatusCode() == 200) {
    
                            HttpEntity httpEntity = response.getEntity();
    
                            String result = EntityUtils.toString(httpEntity, "utf-8");
    
                            Log.e("返回的结果",result);
                        }
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
  • 相关阅读:
    玩游戏怎么能没有皮肤,Python一键采集王某耀游戏所有皮肤,这波就很舒服
    最近办公室每天都会少点东西,我用Python直接控制摄像头拍照发到邮箱,最后发现...
    23个适合Python初学者练手的小脚本,学会技术嘎嘎增长!
    π的值该怎么求?
    ItextSharp.text.pdf
    改画册相关注意事项
    装饰者模式
    策略模式
    简单工厂模式
    代理模式
  • 原文地址:https://www.cnblogs.com/red-code/p/5862388.html
Copyright © 2020-2023  润新知