java读取图片成base64
时间: 2023-11-10 11:03:42 浏览: 65
您可以使用Java中的Base64类来将图片转换为Base64编码字符串,示例如下:
```
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
public class ImageToBase64 {
public static void main(String[] args) {
String base64String = null;
File file = new File("image.jpg");
try (FileInputStream imageInFile = new FileInputStream(file)) {
// Reading a Image file from file system
byte[] imageData = new byte[(int) file.length()];
imageInFile.read(imageData);
base64String = Base64.getEncoder().encodeToString(imageData);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Base64String:\n" + base64String);
}
}
```
阅读全文