Java把图片转成Base64字符串 java 把base64字符串转换成图片
Base64可以将任何二进制文件以可打印字符的方式表示,也就是说,任何二进制文件都可以通过Base64编码技术转成字符串。在Java中,可以使用Base64类实现。1、java把图片(二进制文件)转字符串
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
public class ImageBase64Utils {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = null;
try {
Base64.Encoder encoder = Base64.getEncoder();
inputStream = new FileInputStream("zzzyk.com.jpg");
int available = inputStream.available();
byte[] bytes = new byte[available];
inputStream.read(bytes);
String base64Str = encoder.encodeToString(bytes);
System.out.println(base64Str);
} catch (Exception e) {
e.printStackTrace();
}finally {
inputStream.close();
}
}
}
2、java把字符串转成图片(二进制文件)
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
public class StrBase64Utils {
public static void main(String[] args) throws IOException {
FileOutputStream outputStream = null;
try {
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode("自己复制上面转换的代码");
outputStream = new FileOutputStream("yizuotu.net.jpg");
outputStream.write(bytes);
} catch (Exception e) {
e.printStackTrace();
}finally {
outputStream.close();
}
}
}
补充:软件开发 , C++ ,