图片转文字:
File file=new File(s);
FileInputStream fis=null;
String IO="";
StringBuffer sb=new StringBuffer();
byte[] data = null;
try {
fis = new FileInputStream(file);
data = new byte[fis.available()];
fis.read(data);
fis.close();
BASE64Encoder encoder = new BASE64Encoder();
IO=encoder.encodeBuffer(data);
// IO=encoder.encode(data);
//转码后第一次走到这行正常,但是第二次走就会跳到异常里去
sb.append(IO);
System.out.println(999999);
sb.append("\n=====end=====");
System.out.println(sb.toString());
IO=sb.toString();
System.out.println("向服务器傳輸中");
resp.setBody(IO);
System.out.println("傳輸完畢");
2、文字转图片
String path = ""; String filename = ""; File folder = new File(path); if (!folder.exists()) { folder.mkdirs(); } File dest = new File(folder, filename); BASE64Decoder decoder = new BASE64Decoder(); try { FileOutputStream write = new FileOutputStream(dest); // Base64解码 byte[] bytes = decoder.decodeBuffer(base64Str); write.write(bytes); write.close(); } catch (IOException e) { throw new BusinessException("目标文件上传发生异常fileName:" + dest.getName(), e); }
参考网站:
https://my.oschina.net/KingSirLee/blog/710364
https://www.oschina.net/question/2395099_243234
工具包下载地址:
http://download.csdn.net/detail/u012017115/8439669