public interface base64Img {
/***
* base64
* @param imgPath
* @return
*/
public String baseCoding(String imgPath);
/***
* base64
* @param imgStr
*
*/
public boolean baseDecoding(String imgStr);
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class base64cdImg implements base64Img {
@Override
public String baseCoding(String imgPath) {
InputStream is=null;
byte [] data=null;
try {
is=new FileInputStream(imgPath);
data=new byte[is.available()];
is.read(data);
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BASE64Encoder encoder=new BASE64Encoder();
return encoder.encode(data);
}
@Override
public boolean baseDecoding(String imgStr) {
if(imgStr==null){
return false;
}
BASE64Decoder decoder=new BASE64Decoder();
try {
byte[] b=decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;i++)
{
if(b[i]<0)
{//
b[i]+=256;
}
}
String imgFilePath="F:\\2.jpg";
OutputStream ot=new FileOutputStream(imgFilePath);
ot.write(b);
ot.flush();
ot.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* @param args
*/
public static void main(String[] args) {
//base64cdImg bi=new base64cdImg();
//
//System.out.print(bi.baseCoding("F:\\2.jpg"));
//
//bi.baseDecoding(bi.baseCoding(F:\\2.jpg));
}
}