MultipartEntity图像不被上传 [英] MultipartEntity image is not upload

查看:114
本文介绍了MultipartEntity图像不被上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用MultipartEntity上传图片。我有尝试以下code。

我没有得到任何错误,但图像没有上传。我有足够的权限,上传

PHP

 < PHP    //接收来自Android的数据
    $名称= $ _ POST ['名'];
    $数据= $ _ POST ['数据'];    //接收文件
    $文件= $ _FILES ['形象']    move_uploaded_file($ _ FILES ['形象'] ['tmp_name的值'],User_files /\".$_ FILES ['形象'] ['名']);
    //处理数据    //返回响应于服务器    回声json_en code(
                阵列(
                    '结果'= GT;'成功',
                    '味精'=>报告添加成功。
                    )
                );                ?>公共无效上传()抛出异常{
        //服务器的网址
        字符串URL =URL;
        HttpClient的客户端=新DefaultHttpClient();
        HttpPost后=新HttpPost(URL);
        MultipartEntity mpEntity =新MultipartEntity();
        //文件的路径上传
        字符串的文件路径= Environment.getExternalStorageDirectory()+/ a.png;
        档案文件=新的文件(文件路径);
        ContentBody cbFile =新FileBody(文件,图像/ *);        //将数据添加到多部分实体
        mpEntity.addPart(形象,cbFile);
        mpEntity.addPart(名,新StringBody(测试,Charset.forName(UTF-8)));
        mpEntity.addPart(数据,新StringBody(这是测试报告,Charset.forName(UTF-8)));
        post.setEntity(mpEntity);
        //执行POST请求
        HTT presponse response1 = client.execute(岗位);
        //从服务器的响应
        HttpEntity resEntity = response1.getEntity();
        字符串响应= EntityUtils.toString(resEntity);
        Log.d(响应:响应);
        //生成从响应该阵列
        JSONArray jsonarray =新JSONArray([+响应+]);
        的JSONObject的JSONObject = jsonarray.getJSONObject(0);
        //从响应结果变量
        字符串结果=(jsonobject.getString(结果));
        弦乐味精=(jsonobject.getString(味精));
        //关闭连接
        client.getConnectionManager()shutdown()方法。
    }


解决方案

您需要赋予权限在manifest文件的 WRITE_EXTERNAL_STORAG​​E READ_EXTERNAL_STORAG​​E

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>   <使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAG​​E/>

I want to upload image using MultipartEntity. I have try below code.

I did not get any error but image is not upload. I have enough permission for upload

PHP

<?php

    //Receive the data from android
    $name = $_POST['name'];
    $data = $_POST['data'];

    //Receive the file
    $file = $_FILES['image']

    move_uploaded_file($_FILES['image']['tmp_name'], "User_files/".$_FILES['image']['name']);
    //process the data

    //return response to the server

    echo json_encode(
                array(
                    'result'=>'success',
                    'msg'=>'Report added successfully.'
                    )
                );

                ?>

public void upload() throws Exception {
        //Url of the server
        String url = "URL";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        MultipartEntity mpEntity = new MultipartEntity();
        //Path of the file to be uploaded
        String filepath = Environment.getExternalStorageDirectory()+"/a.png";
        File file = new File(filepath);
        ContentBody cbFile = new FileBody(file, "image/*");         

        //Add the data to the multipart entity
        mpEntity.addPart("image", cbFile);
        mpEntity.addPart("name", new StringBody("Test", Charset.forName("UTF-8")));
        mpEntity.addPart("data", new StringBody("This is test report", Charset.forName("UTF-8")));
        post.setEntity(mpEntity);
        //Execute the post request
        HttpResponse response1 = client.execute(post);
        //Get the response from the server
        HttpEntity resEntity = response1.getEntity();
        String Response=EntityUtils.toString(resEntity);
        Log.d("Response:", Response);
        //Generate the array from the response
        JSONArray jsonarray = new JSONArray("["+Response+"]");
        JSONObject jsonobject = jsonarray.getJSONObject(0);
        //Get the result variables from response 
        String result = (jsonobject.getString("result"));
        String msg = (jsonobject.getString("msg"));
        //Close the connection
        client.getConnectionManager().shutdown();
    }

解决方案

You need to give permission in your manifest file for WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

这篇关于MultipartEntity图像不被上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆