EN code和德code的base64错误时抛出存储到SQL Server 2008 R2 [英] encode and decode base64 excpetion storing to sql server 2008 r2

查看:232
本文介绍了EN code和德code的base64错误时抛出存储到SQL Server 2008 R2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android客户端,我想从我的Andr​​oid发送图像到我的服务器,我将图像转换为字节数组,然后他们字节数组为Base64,我以base64字符串发送到服务器,我去code它在服务器然后存储在我的SQL Server 2008 R2。

我的问题

我无法将字符串存储到我的数据库,然后正确地检索它,我没有得到任何异常,我得到的结果,但现在看来,这是错误的结果。

我的结论是通过执行此操作。

1 - 我送的base64从机器人到服务器并保存在一个静态变量中检索到的字符串。
2 - 我要求服务器以检索静态的字符串,我得到在android背面的图像。
3,但是,当我问服务器检索图像,我认为我在我得到了错误的图像数据库保存它,actullly我得到的结果,但这个结果不能再次去codeD。

我会告诉你我的code为插入和retrive数据库中的图像,pleaes告诉我什么我做错了,

存储到数据库

 无效uploadImage(字符串图像){
连接CON = Database.getConnection();
        CallableStatement的CallableStatement的= NULL;
        尝试{
            CallableStatement的= CON
                    。prepareCall({调用insertRestaurantFoodImage(,)?});
            ByteArrayInputStream进行B =新ByteArrayInputStream的(
                    stringImage.getBytes());
            callableStatement.setInt(1,ID);
            ;
            callableStatement.setBinaryStream(2,B,
                    。stringImage.getBytes()长);
            callableStatement.executeUpdate();
        }赶上(的SQLException E){
            e.printStackTrace();
        }
}

存储过程中插入图像

  ALTER PROCEDURE dbo.insertRestaurantFoodImage
    (
    @restaurantFoodID INT,
    @image VARBINARY(MAX)
    )

开始
    SET NOCOUNT ON
    UPDATE Food_Restaurant
    SET [图片] = @image
    WHERE ID = @restaurantFoodID
结束

从数据库中检索

 连接CON = Database.getConnection();
            CallableStatement的CallableStatement的= NULL;
            尝试{
                CallableStatement的= CON
                        。prepareCall({调用getRestaurantFoodImage(,)?});
                callableStatement.setInt(1,的getID());
                callableStatement.registerOutParameter(2,
                        java.sql.Types.VARBINARY);
                callableStatement.execute();
                字节[]字节= callableStatement.getBytes(2);
                图像=新的String(字节);
            }赶上(的SQLException E){
                e.printStackTrace();
            }

存储过程从数据库以检索为:

  ALTER PROCEDURE dbo.getRestaurantFoodImage

    @foodRestaurantID INT,
    @image VARBINARY(MAX)输出
    )

开始
    SET NOCOUNT ON
    SELECT @image = [图]
    从Food_Restaurant
    WHERE ID = @foodRestaurantID
结束

也许我讲太多,但在previus问题,用户告诉我,我已经不只是把code,但我已经把我的问题,我在做什么。

对于那些谁用途问我如何连接code和android中去这里code是它:

连接code

 专用字节[] getBytesFromBitmap(位图位图){
        ByteArrayOutputStream流=新ByteArrayOutputStream();
        bitmap.com preSS(比较pressFormat.PNG,70,流);
        返回stream.toByteArray();
    }    私人字符串getBase64(位图位图){
        字符串imgString = Base64.en codeToString(getBytesFromBitmap(位图)
                Base64.NO_WRAP);
        返回imgString;
    }

德code

 字符串retrievedBase63 =客户端2
                .getBaseURI(restaurantFoods / OneFood /+ 5 +/的getImage);
        //Log.d(\"image,图像);
        字节[]德codedString = Base64.de code(retrievedBase63,Base64.DEFAULT);
        位图德codedByte = BitmapFactory.de codeByteArray的(德codedString,0,
                德codedString.length);


解决方案
在<α

当我还评论(并回答) href=\"http://stackoverflow.com/questions/16981631/jdbc-stored-png-image-at-sql-server-2008-r2-gives-not-complete-data\">jdbc在SQL Server 2008 R2的存储PNG图像提供了不完整的内容中的SQL Server的旧版本有8KB的限制 VARBINARY VARCHAR 。无论是司机根本不为的getBytes 支持更长或的getString ,或者你可能会使用旧的JDBC驱动程序。另一种可能性是,你需要注册OUT参数为 java.sql.types.LONGVARBINARY (或 LONGVARCHAR )。

在任何情况下(也有其他的问题),我想知道你为什么用base64编码的第一存储数据之前。只是原始字节保存在 VARBINARY

I have an android client, I want to send image from my android to my server, i convert the image to byte array, then they byte array to base64, i send the base64 string to server, i decode it in the server then stored it in my sql server 2008 r2.

my problem

I can't store the string to my database then retrieve it correctly, I didn't get any exception and I get result but it seems it is the wrong result.

i conclude that by doing this.

1- i send the base64 from android to server and saved the string retrieved in a static variable. 2- i asked the server to retrive the static string, i get the image in the android back. 3- but when i asked the server to retrieve the image that i supposed i saved it in the database i got wrong image ,actullly i get result but this result can't be decoded again.

I will tell you my code for insert and retrive the image in database, pleaes tell me what am i doing wrong,

Stored to database

void uploadImage(String image) {
Connection con = Database.getConnection();
        CallableStatement callableStatement = null;
        try {
            callableStatement = con
                    .prepareCall("{call insertRestaurantFoodImage(?,?)}");
            ByteArrayInputStream b = new ByteArrayInputStream(
                    stringImage.getBytes());
            callableStatement.setInt(1, ID);
            ;
            callableStatement.setBinaryStream(2, b,
                    stringImage.getBytes().length);
            callableStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
}

the stored procedure to insert the image is

ALTER PROCEDURE dbo.insertRestaurantFoodImage
    (
    @restaurantFoodID INT,
    @image VARBINARY(MAX)
    )
AS
BEGIN
    SET NOCOUNT ON
    UPDATE Food_Restaurant
    SET [image] = @image
    WHERE ID = @restaurantFoodID
END

Retrieve from database

Connection con = Database.getConnection();
            CallableStatement callableStatement = null;
            try {
                callableStatement = con
                        .prepareCall("{call getRestaurantFoodImage(?,?)}");
                callableStatement.setInt(1, getID());
                callableStatement.registerOutParameter(2,
                        java.sql.Types.VARBINARY);
                callableStatement.execute();
                byte[] bytes = callableStatement.getBytes(2);
                image = new String(bytes);
            } catch (SQLException e) {
                e.printStackTrace();
            }

the stored procedure to retrive from database is :

ALTER PROCEDURE dbo.getRestaurantFoodImage
(
    @foodRestaurantID INT,
    @image VARBINARY(MAX) OUTPUT
    )
AS
BEGIN
    SET NOCOUNT ON
    SELECT @image = [image]
    FROM Food_Restaurant
    WHERE ID = @foodRestaurantID
END

maybe i talked too much, but in the previus question, a user told me that i have to not just put the code but i have to put my problem and what i am doing.

for those uses who asked me about how to encode and decode in android here is it:

encode

private byte[] getBytesFromBitmap(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 70, stream);
        return stream.toByteArray();
    }

    private String getBase64(Bitmap bitmap) {
        String imgString = Base64.encodeToString(getBytesFromBitmap(bitmap),
                Base64.NO_WRAP);
        return imgString;
    }

decode

String retrievedBase63 = client2
                .getBaseURI("restaurantFoods/OneFood/" + 5 + "/getImage");
        //Log.d("image", image);
        byte[] decodedString = Base64.decode(retrievedBase63, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
                decodedString.length);

解决方案

As I also commented (and responded) on jdbc stored png image at sql server 2008 r2 gives not complete data older version of SQL Server had a limit of 8kb for varbinary and varchar. Either the driver simply doesn't support longer for getBytes or getString, or you might be using an older JDBC driver. Another possibility is that you need to register the OUT parameter as java.sql.types.LONGVARBINARY (or LONGVARCHAR).

In any case (also in the other question), I am wondering why you are first encoding in base64 before storing your data. Just store the raw bytes in a VARBINARY.

这篇关于EN code和德code的base64错误时抛出存储到SQL Server 2008 R2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆