duan2428
2017-03-30 05:34
浏览 174

尝试上传时,Android Studio会破坏Base64字符串

So I am testing methods to upload an image to a php server. One of the methods was to convert the image to a Base64 String and upload it and then decode it when needed.

When I uploaded a picture I noticed that there was a difference in the Strings. Some spaces were missing and some next lines were misplaced.

Before I sent the string to the server the string is okay and then it goes through a _POST command in the php file and then it is inserted into the database using INSERT INTO.

I suspect that the header that I use changes the string when I try to upload it. the header is /x-www-form-urlencoded. I tried using /json but if the json object is converted to string it changes the original string. So what header do I need to use so the string won't change? Or is there another method to upload a picture to a MySQL database?

This is the method I use to upload the string to the server:

ImageString = getStringImage(bitmap); 
String ImageUploadString="image="+ImageString; 
final OkHttpClient client = new OkHttpClient(); 
MediaType contentType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"); 

RequestBody body = RequestBody.create(contentType, imageUploadString)); 
Request request = new Request.Builder().url(uploadURL).post(body).build(); 
Response response = null; 
try { 
    response = client.newCall(request).execute(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
try { 
    String json = response.body().string(); 
} catch (IOException e) { 
    e.printStackTrace(); 
}

Edit: this is the php file that I use to insert the picture:

<?php
$response = array();
if (isset($_POST['image']))
{
$image = $_POST['image'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = $db->query("INSERT INTO images(image) VALUES('$image')");
if ($result) {
$response["success"] = 1;
$response["message"] = "Product successfully created.";
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "missing fields";
echo json_encode($response);
}
?>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题