使用PHP WebService将图像插入MySQL数据库

时间:2022-09-25 16:39:14

I searched a lot about this question but I didn't find any direct answer.

我搜索了很多关于这个问题,但我没有找到任何直接答案。

I am programming an Android application that uses SQL to store data. I'm passing SQL query trought a simple PHP Web Service.

我正在编写一个使用SQL来存储数据的Android应用程序。我正在通过一个简单的PHP Web服务传递SQL查询。

The Web service code is

Web服务代码是

<?php

if(isset($_GET['user'])) { $user = $_GET['user']; }
if(isset($_GET['pass'])) { $pass = $_GET['pass']; }
if(isset($_GET['query'])) { $query = $_GET['query']; }

if(isset($query)) {
    $link = mysql_connect('localhost',$user,$pass) or die('Connexion impossible au serveur SQL');
    mysql_select_db('dbi',$link) or die('Impossible de sélectionner la base de donnée');

    $result = mysql_query($query,$link) or die('Erreur dans la requete : '.$query);
    $count=0;
    while($row = mysql_fetch_row($result)){
    if ($count>0) echo ";";
        for($a=0;$a<count($row);$a++){
            echo $row[$a];
            if ($a<(count($row)-1)) echo ',';
        }
    $count++;
    }

    @mysql_close($link);
}
?>

Everything is working (I in fact only use the webservice to Insert data in the database (I dump the database into a sqlite database to work with it localy)) except when I try to insert a Blob. I get illegal characters, so I try to encode the query but the blob is corrupt.

一切正常(我实际上只使用webservice在数据库中插入数据(我将数据库转储到sqlite数据库以便与它一起工作))除非我尝试插入Blob。我得到非法字符,所以我尝试编码查询但blob已损坏。

private String executeQuery(String query){
        HttpClient client= new DefaultHttpClient();
        String encodedQuery=null;
        try {
            encodedQuery = URLEncoder.encode(query,"UTF-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
        HttpGet httpget = new HttpGet(serverURL+"?user="+username+"&pass="+password+"&query="+encodedQuery);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String result = client.execute(httpget, responseHandler);
        client.getConnectionManager().shutdown();
        return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
        }
    }

I execute the query like that

我执行这样的查询

executeQuery("INSERT INTO " + DBIDatabase.TABLE_signatures + " (name, job, signature) VALUES ('" + pm.signature.name + "','" + job_id + "','" + new String(pm.signature.sign.imageByteArray) + "')")

Original BLOB (As printed by a System.out.println in eclipse):

原始BLOB(由eclipse中的System.out.println打印):

�PNG


������
IHDR�����������d��������fw������sBIT|d�����RIDATx���=��D���oJ~�����DBB���OH��6B�QDP%�]��n�"����ڞ��3��G��};���������������������������������x��8�&������#9lě����۸ݪ8No���N}��؉t9u%�|��ح�jU\��㠽��f��T"��rp,������7_�
�G��]] L�*��������lK?L+P��+��;��x�I�z�r�+ؐ%���q3��ˆS��$��P�V���*���Ѷ�Dҗ$�ڟZ�b�k��'�!".#�����]So�d��-�k1���X��[�a�2�Y/#�;�~�Кx�J2�ڶ�PwݴОf,�ـ�su{��t��O�5��%��:�t�E�W�/2�?$AԸݠj[<��zEy��+�a�Wй��V̚�i��>]Pk��u���,QQM�2�
ɖ<�5�x�
d��R���wJ�����1��C����e�'K�ف�X�{��N�%'����V�ɐ����-���#V����iD|�1��xK��͋�%l�]�g�zXBܩָNP�v�l8�0נ��1�o��4��9��j�Jj*b��q�'U�????،{�n`b5�x*�Ӧle�mZ��r%�GS�7G�}�Z�y�e)cyc����3����z�X���Z�����.��

��8s,��s���]~��d�v�GD|����0T�3��X�䴋��:�>�����("~�a�;�k���_s�(u:�ݒ��1�o�~x0��N�.�26Qy s��R��IC��TkaN���->��Ա�dPL�١OJ2��ؗ�N��g,s���..�ߩ�쓒Aq?�_�7��ʲv�c|��ѢHm�,e{S�q�X�l8���j3�ߕ�i�0I�%�*ۜ�s�0�2����<���!�"��u=x���rVK� �����}UE�N��F�E�YD|�#��y��G��/>G�<D����1�#�߈�+�z�m�{�>//@�����P�D��о8Z�1 ��1�z�Ÿ����!����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������P�����'3������������IEND�B`�')

Blob in the database:

Blob在数据库中:

�PNG


IHDR�d��fwsBIT|d��IDATx���=n�@��Q��s�4p��5�� N@�@�p (4�i�DB "��P�b�Zo�����y��VZ�^���xF��ĥ�`�I���Or��cLއ$G�?��~5���q�����k�W��M;`4��?uw*��.տ�낹V���T"�|O�o�`.���J��n��jI��=�V��
�N�`��>�%��R�N��I.�¤a��,H��47�=:m{�
e+������a�`.(��A�$ɳ���I�{����ł�X�1���)B���G����
�^�����t������0�S����T_�`P��� �,��}�S@�~f|O�uvv)�u?��}�`.�!�U[#�zN��,�?tf(7�f���R���T�s�-.��ÕY˼Iu_�`� ��w�dX�~�Fy�)�g�.u
�Y��Ƭ`.���WAM��Z��u�m6xL�k;�}�q������������$_rv��N�V(#L�E���j��e���k��X,��������K�>}�P>轺�fL��Ի��
���!v悡�%y�z�0v�p.OSos�T@��N����0L�S��(���ZK�u�ϱ�9U��@{����*J�YIEND�B`�

If I load back the picture, it's an empty picture, sadly. Is there something I missed? Is there any other way to pass the Blob to the PHP Web Service?

如果我加载回来的照片,可悲的是,这是一张空白照片。我错过了什么吗?有没有其他方法可以将Blob传递给PHP Web服务?

Thx

1 个解决方案

#1


0  

You need to Base64-encode your image, then store as String. This ensures that you only use valid characters on your php, database, and wherever.

您需要对图像进行Base64编码,然后存储为String。这可确保您只在php,数据库和任何地方使用有效字符。

#1


0  

You need to Base64-encode your image, then store as String. This ensures that you only use valid characters on your php, database, and wherever.

您需要对图像进行Base64编码,然后存储为String。这可确保您只在php,数据库和任何地方使用有效字符。