2013-02-11 87 views
0

我有以下方法:编码从字节数组的问题一个字符串

/** 
    * Encodes the byte array into base64 string 
    * @param imageByteArray - byte array 
    * @return String a {@link java.lang.String} 
    */ 
public static String encodeImage(byte[] imageByteArray) { 
    return Base64.encodeBase64URLSafeString(imageByteArray); 
} 

我输入“org.apache.commons.codec.binary.Base64;”但是,我得到的错误:在该行

多个标记 - 方法encodeBase64URLSafeString(字节[])是未定义的 类型的Base64 - 行断点:的MySqlConnection [线:287] - encodeImage(字节[] )

我已将此代码从“http://www.myjeeva.com/2012/07/how-to-convert-image-to-string-and-string-to-image-in-java/”复制过来。我正在使用Eclipse Juno(完全更新)和GWT。

有没有人知道我在做什么错在这里?

问候,

格林

谢谢马库斯答:我现在已经从您的信息创建此类:

package org.AwardTracker.server; 

public class Base64Decode { 
private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/"; 

public static byte[] decode(String s) { 

    // remove/ignore any characters not in the base64 characters list 
    // or the pad character -- particularly newlines 
    s = s.replaceAll("[^" + base64chars + "=]", ""); 

    // replace any incoming padding with a zero pad (the 'A' character is 
    // zero) 
    String p = (s.charAt(s.length() - 1) == '=' ? (s.charAt(s.length() - 2) == '=' ? "AA" 
      : "A") 
      : ""); 

    s = s.substring(0, s.length() - p.length()) + p; 
    int resLength = (int) Math.ceil(((float) (s.length())/4f) * 3f); 
    byte[] bufIn = new byte[resLength]; 
    int bufIn_i = 0; 

    // increment over the length of this encrypted string, four characters 
    // at a time 
    for (int c = 0; c < s.length(); c += 4) { 

     // each of these four characters represents a 6-bit index in the 
     // base64 characters list which, when concatenated, will give the 
     // 24-bit number for the original 3 characters 
     int n = (base64chars.indexOf(s.charAt(c)) << 18) 
       + (base64chars.indexOf(s.charAt(c + 1)) << 12) 
       + (base64chars.indexOf(s.charAt(c + 2)) << 6) 
       + base64chars.indexOf(s.charAt(c + 3)); 

     // split the 24-bit number into the original three 8-bit (ASCII) 
     // characters 

     char c1 = (char) ((n >>> 16) & 0xFF); 
     char c2 = (char) ((n >>>8) & 0xFF); 
     char c3 = (char) (n & 0xFF); 

     bufIn[bufIn_i++] = (byte) c1; 
     bufIn[bufIn_i++] = (byte) c2; 
     bufIn[bufIn_i++] = (byte) c3; 

    } 

    return bufIn; 
} 


} 

,改变了呼吁: 进口org.AwardTracker.server .Base64Decode;

public static String encodeImage(byte[] imageByteArray) { 
    return Base64Decode(imageByteArray); [Error on this line] 
} 

,我现在得到的错误:

在该行 多个标记 - 方法Base64Decode(字节[])是未定义的类型 的MySqlConnection

帮助greatfuly赞赏。

问候,

格林

第三轮

好了,我有我的加密和解密以防万一走错了路。这现在已经被纠正并放置在正确的库中。但是,我仍然在encodeImage方法中有错误。这是我用来调用encodeImage方法的方法。我怀疑是:

java.sql.Blob imageBlob = result.getBlob(1); 
byte[] imageData = imageBlob.getBytes(1, (int) imageBlob.length()); 

是不正确的。但是,我已经将ImageData定义为byte [],所以我不明白为什么encodeImage认为它是一个String?

public String getImageData(String id){ 
    ResultSet result = null; 
    PreparedStatement ps = null; 
    String imageDataString = null; 
    try { 
     // Read in the image from the database. 
     ps = conn.prepareStatement(
       "SELECT at_cub_details.cd_photograph " + 
         "FROM at_cub_details " + 
         "WHERE at_cub_details.cd_id = \"" + id + "\""); 
     result = ps.executeQuery(); 
     while (result.next()) { 
      java.sql.Blob imageBlob = result.getBlob(1); 
      byte[] imageData = imageBlob.getBytes(1, (int) imageBlob.length()); 

      //Convert Image byte array into Base64 String 
      imageDataString = encodeImage(imageData); 
     } 

    } 

在一个错误而导致:

public static String encodeImage(byte[] imageByteArray) { 
    return Base64Encode.encode(imageByteArray); [Error on this line] 
} 

“在类型Base64Encode的方法编码(字符串)是不适用的参数(字节[])”

感谢所有的帮助。

问候,

格林

附:,我认为这完全值得我写一篇博客,因为我在这方面找不到任何定义的工作,而且我会认为这会经常使用。我的下一个项目。

编码类是:

package org.AwardTracker.server; 

public class Base64Encode { 
private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/"; 

public static String encode(String s) { 

    // the result/encoded string, the padding string, and the pad count 
    String r = "", p = ""; 
    int c = s.length() % 3; 

    // add a right zero pad to make this string a multiple of 3 characters 
    if (c > 0) { 
     for (; c < 3; c++) { 
      p += "="; 
      s += "\0"; 
     } 
    } 

    // increment over the length of the string, three characters at a time 
    for (c = 0; c < s.length(); c += 3) { 

     // we add newlines after every 76 output characters, according to 
     // the MIME specs 
     if (c > 0 && (c/3 * 4) % 76 == 0) 
      r += "\r\n"; 

     // these three 8-bit (ASCII) characters become one 24-bit number 
     int n = (s.charAt(c) << 16) + (s.charAt(c + 1) << 8) 
       + (s.charAt(c + 2)); 

     // this 24-bit number gets separated into four 6-bit numbers 
     int n1 = (n >> 18) & 63, n2 = (n >> 12) & 63, n3 = (n >> 6) & 63, n4 = n & 63; 

     // those four 6-bit numbers are used as indices into the base64 
     // character list 
     r += "" + base64chars.charAt(n1) + base64chars.charAt(n2) 
       + base64chars.charAt(n3) + base64chars.charAt(n4); 
    } 

    return r.substring(0, r.length() - p.length()) + p; 
} 
} 

而解码类:

package org.AwardTracker.server; 

public class Base64Decode { 
private final static String base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/"; 

public static byte[] decode(String s) { 

    // remove/ignore any characters not in the base64 characters list 
    // or the pad character -- particularly newlines 
    s = s.replaceAll("[^" + base64chars + "=]", ""); 

    // replace any incoming padding with a zero pad (the 'A' character is 
    // zero) 
    String p = (s.charAt(s.length() - 1) == '=' ? (s.charAt(s.length() - 2) == '=' ? "AA" 
      : "A") 
      : ""); 

    s = s.substring(0, s.length() - p.length()) + p; 
    int resLength = (int) Math.ceil(((float) (s.length())/4f) * 3f); 
    byte[] bufIn = new byte[resLength]; 
    int bufIn_i = 0; 

    // increment over the length of this encrypted string, four characters 
    // at a time 
    for (int c = 0; c < s.length(); c += 4) { 

     // each of these four characters represents a 6-bit index in the 
     // base64 characters list which, when concatenated, will give the 
     // 24-bit number for the original 3 characters 
     int n = (base64chars.indexOf(s.charAt(c)) << 18) 
       + (base64chars.indexOf(s.charAt(c + 1)) << 12) 
       + (base64chars.indexOf(s.charAt(c + 2)) << 6) 
       + base64chars.indexOf(s.charAt(c + 3)); 

     // split the 24-bit number into the original three 8-bit (ASCII) 
     // characters 

     char c1 = (char) ((n >>> 16) & 0xFF); 
     char c2 = (char) ((n >>>8) & 0xFF); 
     char c3 = (char) (n & 0xFF); 

     bufIn[bufIn_i++] = (byte) c1; 
     bufIn[bufIn_i++] = (byte) c2; 
     bufIn[bufIn_i++] = (byte) c3; 

    } 

    return bufIn; 
} 


} 

我终于找到了一个编码类,它的工作原理:

package org.AwardTracker.server; 

public class Base64Encode2 { 
private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/".toCharArray(); 

    private static int[] toInt = new int[128]; 

    static { 
     for(int i=0; i< ALPHABET.length; i++){ 
      toInt[ALPHABET[i]]= i; 
     } 
    } 

/** 
* Translates the specified byte array into Base64 string. 
* 
* @param buf the byte array (not null) 
* @return the translated Base64 string (not null) 
*/ 
public static String encode(byte[] buf){ 
    int size = buf.length; 
    char[] ar = new char[((size + 2)/3) * 4]; 
    int a = 0; 
    int i=0; 
    while(i < size){ 
     byte b0 = buf[i++]; 
     byte b1 = (i < size) ? buf[i++] : 0; 
     byte b2 = (i < size) ? buf[i++] : 0; 

     int mask = 0x3F; 
     ar[a++] = ALPHABET[(b0 >> 2) & mask]; 
     ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask]; 
     ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask]; 
     ar[a++] = ALPHABET[b2 & mask]; 
    } 
    switch(size % 3){ 
     case 1: ar[--a] = '='; 
     case 2: ar[--a] = '='; 
    } 
    return new String(ar); 
} 

} 

感谢您的帮助Markus A.非常感谢。

问候,

格林

+0

你有没有正确的Base64版本,因为方法已经改变了几次。确保您拥有的版本具有您正在使用的电话。 – 2013-02-11 03:19:38

+0

你使用URL编码器对一个字节数组(这是一个图像?)进行编码? – texasbruce 2013-02-11 03:19:43

回答

1

GWT不提供阿帕奇百科全书包,所以我不认为你可以只使用此功能在你的代码。

以下是你可以在GWT使用的东西:

https://developers.google.com/web-toolkit/doc/latest/RefJreEmulation

如果你想使用它,你需要实际拥有的Base64类的源代码(以及所有依赖的)可用并将其放到GWT编译器可以将它转换为JavaScript的地方。

对于其他的方法可以做到的编码,在这里看到:

How do I encode/decode short strings as Base64 using GWT?

加为添加到原来的职位新问题:

首先,你需要调用一个静态函数类似这样:

return Base64Decode.decode(imageByteArray); 

但是你似乎也有你的类型向后:encodeImage需要一个byte []并返回一个String,而Base64Decode.decode接受一个String并返回一个byte []。您可能需要使用等效的Base64Encode.encode函数,而不是解码函数。

+0

感谢Markus A.我从你的链接中获取了代码。但是我仍然遇到错误。我已经使用新代码更新了发布内容。谢谢格林。 – Glyn 2013-02-11 04:47:47

+0

让我们希望我的更新答案能够解决问题;) – 2013-02-11 05:55:34

+0

您好Markus,您在这里完全符合标记,并且我已经纠正了双方(即交换我的解码和编码)。一个愚蠢的错误导致我对此感到沮丧。所以我的班级现在都是正确的,并且处在正确的位置。不过,我仍然遇到了调用encode类的错误。请参阅上文。谢谢,格林。 – Glyn 2013-02-11 22:25:35