doupingtang9627 2015-10-03 19:32
浏览 137

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

My code:

 //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();

Server code is a basic print_r($_POST) to view posted data.

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

Server response normal without base64 string post.

I can view the page (ie. http://myserver.com/script.php) with no error in browser.

edit:

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

try to upload with/without the base64 string.. without works

driving me crayz.. pls help

edit: added params

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条回答 默认 最新

  • dongmangji0950 2015-10-03 20:22
    关注

    You should get the real path of your image that you get it from 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;
              }
        }
    

    and add this method to get the real path of the image

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

    and add this code in your 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();
                }
    

    But I do highly recommend you not to use this way and use network library to make your API calls and I recommend you to use Retrofit. This will save to you alot of time.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?