• base64编码的测试 - [CS\IT]

    2008-07-18

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://kerochan.blogbus.com/logs/24960774.html

    sun.misc里也有base64编码器,但我更倾向于Apache的包,算是好感行吧~

    Apache下的commons项目有很多好用的东西,这次就用到了codec和io包,io那个强大啊,省却很多功夫。

    还有eclipse也挺好用的,记住的快捷键多一点的话就更好用了,难道我的vim时代要过去了?

    恩下面把我测试base64的代码贴出来,main里一个是encode,一个是decode,编码再解码后的文件和原文件是一样的。

    Java代码: Test1.java
    01 import java.io.File;
    02 import java.io.FileInputStream;
    03 import java.io.FileNotFoundException;
    04 import java.io.FileOutputStream;
    05 import java.io.IOException;
    06
    07 import org.apache.commons.codec.binary.Base64;
    08
    09 /* it works :-) */
    10 public class Test1 {
    11
    12     public static byte[] encodeFile(String fileName) {
    13         byte[] bs = null;
    14         try {
    15             bs = org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(
    16                     new File(fileName)));
    17         } catch (FileNotFoundException e) {
    18             // TODO Auto-generated catch block
    19             e.printStackTrace();
    20         } catch (IOException e) {
    21             // TODO Auto-generated catch block
    22             e.printStackTrace();
    23         }
    24         if (bs == null) {
    25             return null;
    26         }
    27         return Base64.encodeBase64(bs);
    28     }
    29
    30     public static byte[] decodeFile(String fileName) {
    31         byte[] bs = null;
    32         try {
    33             bs = org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(
    34                     new File(fileName)));
    35         } catch (FileNotFoundException e) {
    36             // TODO Auto-generated catch block
    37             e.printStackTrace();
    38         } catch (IOException e) {
    39             // TODO Auto-generated catch block
    40             e.printStackTrace();
    41         }
    42         if (bs == null) {
    43             return null;
    44         }
    45         return Base64.decodeBase64(bs);
    46     }
    47
    48     /**
    49      * @param args
    50      */
    51     public static void main(String[] args) {
    52         // encode
    53         String fileName = "/tmp/001.jpg";
    54         byte[] bs = Test1.encodeFile(fileName);
    55
    56         FileOutputStream fo = null;
    57         try {
    58             fo = new FileOutputStream(new File("/tmp/001.jpg.b64"));
    59         } catch (FileNotFoundException e) {
    60             // TODO Auto-generated catch block
    61             e.printStackTrace();
    62         }
    63
    64         if (fo == null) {
    65             System.out.println("error");
    66         }
    67
    68         try {
    69             fo.write(bs);
    70         } catch (IOException e) {
    71             // TODO Auto-generated catch block
    72             e.printStackTrace();
    73         }
    74
    75         // decode
    76         /*
    77          * String fileName = "/tmp/001.jpg.b64"; byte[] bs =
    78          * Test1.decodeFile(fileName);
    79          *
    80          * FileOutputStream fo = null; try { fo = new FileOutputStream(new
    81          * File("/tmp/001.jpg")); } catch (FileNotFoundException e) { // TODO
    82          * Auto-generated catch block e.printStackTrace(); }
    83          *
    84          * if (fo == null) { System.out.println("error"); }
    85          *
    86          * try { fo.write(bs); } catch (IOException e) { // TODO Auto-generated
    87          * catch block e.printStackTrace(); }
    88          */
    89
    90     }
    91
    92 }

     哎这blogbus要是能把代码发芽网的功能整合进来那真是妙极了!


    收藏到:Del.icio.us