2017-11-29 5 views
0
ByteArrayOutputStream bytearray = new ByteArrayOutputStream(); 
mBitmapProfile.compress(Bitmap.CompressFormat.JPEG, 100, bytearray); 
String base64 = Base64.encodeToString(bytearray.toByteArray(), Base64.DEFAULT); 
String data = URLEncoder.encode("SOURCE", "UTF-8") + "=" base64; 

String result = getHttpData("http://example/p.php", data); 


private String getHttpData(String httpUrl, String param) { 
     String urlString = httpUrl; 
     String data = param; 

     try { 
      URL url = new URL(urlString); 

      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      // urlConnection.setConnectTimeout(CONNECTION_TIMEOUT); 
      // urlConnection.setReadTimeout(DATARETRIEVAL_TIMEOUT); 

      urlConnection.setRequestMethod("POST"); 
      urlConnection.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream()); 

      wr.write(data); 
      wr.flush(); 

      int responseCode = urlConnection.getResponseCode(); 

      if (responseCode == HttpsURLConnection.HTTP_OK) { 

       BufferedReader br=new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
       String response = ""; 
       String line = ""; 
       while ((line = br.readLine()) != null) { 
        response+=line; 
       } 
       return response; 
      } 
     } catch(MalformedURLException e){ 
      e.printStackTrace(); 
      return null; 
     } catch(IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
     return ""; 
    } 

をPHPにアンドロイドからの投稿を送信します。は、base64で画像を圧縮して、base64では、AndroidからPHPへの要求は、問題が発生します作り、POSTを使用して画像</p> <p>をコードした後

EX)

アンドロイド送信データ:/ 9J/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBA + BAQEBAQE ==

PHPがデータを受信:/ 9J/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBABAQEBAQE

一部の特殊文字があります失われた

なぜそうですか?

答えて

0

問題は、base64でエンコードされたデータに '+'文字を含めることができます。 x-www-form-urlencodedデータでは、受信者は '+'がスペース文字のエンコーディングであることを知っています。したがって、base64の値をURLエンコードしていないため、 '+'のインスタンスは受信時にデータが破損する原因となります。

関連する問題