duan2428 2017-03-30 05:34
浏览 176
已采纳

尝试上传时,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条回答 默认 最新

  • duandi8544 2017-03-30 05:55
    关注

    For image uploads you can always go for multipart part upload rather than converting it into base64. If you are using retrofit, the implementation will be easier.

    I am adding the php and the android code to do it. I assume you are using httpurlconnection and not retrofit.

    PHP

      <?php
                $target_dir = "uploads/";
                $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
                $uploadOk = 1;
                $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
                // Check if image file is a actual image or fake image
                if(isset($_POST["submit"])) {
                    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
                    if($check !== false) {
                        echo "File is an image - " . $check["mime"] . ".";
                        $uploadOk = 1;
                    } else {
                        echo "File is not an image.";
                        $uploadOk = 0;
                    }
                }
                // Check if file already exists
                if (file_exists($target_file)) {
                    echo "Sorry, file already exists.";
                    $uploadOk = 0;
                }
                // Check file size
                if ($_FILES["fileToUpload"]["size"] > 500000) {
                    echo "Sorry, your file is too large.";
                    $uploadOk = 0;
                }
                // Allow certain file formats
                if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                && $imageFileType != "gif" ) {
                    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                    $uploadOk = 0;
                }
                // Check if $uploadOk is set to 0 by an error
                if ($uploadOk == 0) {
                    echo "Sorry, your file was not uploaded.";
                // if everything is ok, try to upload file
                } else {
                    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                    } else {
                        echo "Sorry, there was an error uploading your file.";
                    }
                }
                ?>
    

    Android

    fileInputStream = new FileInputStream(new File(_filePath));
    
            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();
    
            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
    
            // Enable POST method
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            outputStream = new DataOutputStream(
                    connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream
                    .writeBytes("Content-Disposition: form-data; name=\"fileToUpload\";filename=\""
                            + _filePath + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);
    
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
    
            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            // int contentLength = bytesAvailable+
            // header.getBytes().length+footer.getBytes().length;
    
            while (bytesRead > 0) {
                outputStream.write(buffer, 0, bufferSize);
    
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
    
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                    + lineEnd);
    

    I also suggest you to use retrofit as a lot of network handling are done by the library itself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序