dongzhunqiu4841 2015-10-03 00:38
浏览 82
已采纳

将图像从android上传到php服务器无法正常工作

I´m trying to upload an image using openurlconnection but I´m facing some problems. this is my java class:

public class Upload extends AppCompatActivity
{
    InputStream inputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_upload);

      StrictMode.enableDefaults();

      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sss);
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
      byte [] byte_arr = stream.toByteArray();
      String encodedImage = Base64.encodeToString(byte_arr, Base64.DEFAULT);
      String msj = downloadImage(encodedImage);
      Toast.makeText(getBaseContext(), "mensaje "+msj, Toast.LENGTH_SHORT).show();
    }


    public String downloadImage(String tabla)
    {
      String urlG = "http://192.168.43.98/jober/";

      try {
          URL url = new URL(urlG+"upload.php");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("POST");

          // para activar el metodo post
          conn.setDoOutput(true);
          conn.setDoInput(true);
          DataOutputStream wr = new DataOutputStream(
                  conn.getOutputStream());
          wr.writeBytes("image="+tabla);
          wr.flush();
          wr.close();

          InputStream is = conn.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line;
          StringBuffer response = new StringBuffer();
          while((line = rd.readLine()) != null) {
              response.append(line);
              response.append('');
          }
          rd.close();
          return response.toString();
       }
        catch(Exception e){ return "error";}
    }
}

and php code is:

<?php   
    $base=$_REQUEST['image'];
    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');
    $file = fopen('uploaded_image.png', 'wb');
    fwrite($file, $binary);
    fclose($file);
    echo 'Image upload complete!!, Please check your php file directory……';
?>

the only problem is when I check the file and looks like:

enter image description here

I was looking for the error but I´m not able to.

  • 写回答

1条回答 默认 最新

  • douzhang3822 2015-10-03 05:35
    关注

    You have to URLEncode the String before you send it to PHP, since the default base64 decode also includes the symbols + and =. Change

    String encodedImage = Base64.encodeToString(byte_arr, Base64.DEFAULT);
    

    to

    String encodedImage = URLEncoder.encode(Base64.encodeToString(byte_arr, Base64.DEFAULT), "UTF-8");
    

    and you are good to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置