试图上传Base64编码的字符串,服务器行为不端给出404错误

时间:2015-10-03 19:32:50

标签: php android server base64

我的代码:

 //bitmap to base64
 bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
 byte[] byte_arr = stream.toByteArray();
 imageEncoded[y] = Base64.encodeToString(byte_arr,     Base64.DEFAULT);

 //httppost
 HttpClient client = new DefaultHttpClient();
 HttpPost httppost = new HttpPost(url);

 httppost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
 HttpResponse r =  client.execute(httppost);
 HttpEntity httpEntity = r.getEntity();

服务器代码是查看发布数据的基本print_r($ _ POST)。

使用base64字符串发布的服务器响应:错误404:找不到文档

没有base64字符串帖子的服务器响应正常。

我可以在浏览器中查看页面(即http://myserver.com/script.php)并且没有错误。

编辑:

在此测试:http://bentabols.xyz/h2.php

尝试使用/不使用base64字符串进行上传..无需工作

驾驶我crayz ..请帮忙

编辑:添加参数

params.add(new BasicNameValuePair("postitem", "1"));
params.add(new BasicNameValuePair("name", "Item Name"));
params.add(new BasicNameValuePair("price", "232"));
params.add(new BasicNameValuePair("section", "forsale"));
params.add(new BasicNameValuePair("warranty", "nowarranty"));
params.add(new BasicNameValuePair("condition", "used"));
params.add(new BasicNameValuePair("province", "albay"));
params.add(new BasicNameValuePair("duration", "5"));
params.add(new BasicNameValuePair("category", "computers"));
params.add(new BasicNameValuePair("description", "desc"));

for (int p = 0; p < imagepaths.length; p++) {
     params.add(new BasicNameValuePair("image_"+p, imageEncoded[p]));
     params.add(new BasicNameValuePair("imgnames_"+p, imagenames[p]));
}

1 个答案:

答案 0 :(得分:0)

你应该从onActivityResult

获得你的图像的真实路径
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            If (resultCode == RESULT_OK) {
                if (requestCode == 1) {
                    Bitmap bm = (Bitmap) data.getExtras().get("data");
                    MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null);
                    Uri tempUri = getImageUri(getApplicationContext(), bm); 
                    stro = getRealPathFromURI(tempUri);

                    } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage, filePath,
                        null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);

                String picturePath = c.getString(columnIndex);
                c.close();
                // uploadFile(picturePath);
                stro=picturePath;
          }
    }

并添加此方法以获取图像的真实路径

public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        return cursor.getString(idx); 
    }

并在doInBackground中添加此代码

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("image", stro));
try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                HttpPost httpPost = new HttpPost(DOMAINURL1);
                MultipartEntity entity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);

                for (int index = 0; index < pairs.size(); index++) {
                    if (pairs.get(index).getName().equalsIgnoreCase("image")) {
                        // If the key equals to "image", we use FileBody to
                        // transfer the data
                        try {
                            // File f = new File(pairs.get(index).getValue());
                            File f = new File(stro);
                            entity.addPart(pairs.get(index).getName(),new FileBody(f));

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } else {
                        // Normal string data
                        entity.addPart(pairs.get(index).getName(),new StringBody(pairs.get(index).getValue()));
                    }
                }

                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost,localContext);
                if (response != null) {
                    InputStream in = response.getEntity().getContent(); // Get
                                                                        // the
                                                                        // data
                                                                        // in
                                                                        // the
                                                                        // entity
                    InputStreamReader reader = new InputStreamReader(in);
                    BufferedReader bf1 = new BufferedReader(reader);
                    validuser = bf1.readLine();



                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

但我强烈建议您不要使用这种方式并使用网络库进行API调用,我建议您使用Retrofit。这将节省很多时间。