ä¾å说æä¸å
å
å
åå
æµè¯å§ï¼åå
æµè¯ç代ç å¦ä¸ï¼
package test.com.cs;
import com.cs.Base64Convert;
import junit.framework.TestCase;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Logger;
public class TestBase64Convert extends TestCase {
Base64Convert baseCov = null;
public TestBase64Convert(String s) {
super(s);
}
protected void setUp() throws Exception {
baseCov = new Base64Convert();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testIoToBase64() {
try {
String strBase64 = baseCov.ioToBase64(); //å° io 转æ¢ä¸º base64ç¼ç
System.out.println(">>> "+strBase64);
baseCov.base64ToIo(strBase64); //å° base64ç¼ç 转æ¢ä¸º io æ件æµï¼çæä¸å¹
æ°å¾ç
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------
package com.cs;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
public class Base64Convert {
BASE64Decoder decoder = new BASE64Decoder();
public String ioToBase64() throws IOException {
String fileName = "d:/gril.gif"; //
æºæ件 String strBase64 = null;
try {
InputStream in = new FileInputStream(fileName);
// in.available()è¿åæ件çåèé¿åº¦
byte[] bytes = new byte[in.available()];
// å°æ件ä¸çå
容读å
¥å°æ°ç»ä¸
in.read(bytes);
strBase64 = new BASE64Encoder().encode(bytes); //å°åèæµæ°ç»è½¬æ¢ä¸º
å符串 in.close();
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return strBase64;
}
public void base64ToIo(String strBase64) throws IOException {
String string = strBase64;
String fileName = "d:/gril2.gif"; //çæçæ°æ件
try {
// 解ç ï¼ç¶åå°åè转æ¢ä¸ºæ件
byte[] bytes = new BASE64Decoder().decodeBuffer(string); //å°å符串转æ¢ä¸ºbyteæ°ç»
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[1024];
FileOutputStream out = new FileOutputStream(fileName);
int bytesum = 0;
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
out.write(buffer, 0, byteread); //æ件åæä½
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}