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


Trying to upload Base64 Encoded string, server misbehaving giving a 404 eror

我的代码:

 //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)来查看发布的数据

Server response with base64 string post: Error 404: document not found

服务器响应正常,没有base64字符串post。

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

编辑:

test here: http://bentabols.xyz/h2.php

尝试使用/不使用base64字符串上传。没有作品

驾驶我蜡笔…请帮助

编辑:添加参数

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]));
}

你应该得到你的图像的真实路径,你从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。这将为您节省很多时间。