如何将图片转成base64
时间: 2023-10-05 19:06:28 浏览: 39
将图片转换为base64编码,可以使用如下Java代码:
```java
public static String imageToBase64(String imagePath) throws IOException {
File file = new File(imagePath);
String base64 = null;
if (file.exists()) {
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
inputStream.read(bytes);
base64 = Base64.getEncoder().encodeToString(bytes);
inputStream.close();
}
return base64;
}
```
这个方法接收一个图片文件的路径作为参数,然后将该图片读取为字节数组,再将字节数组编码为base64字符串并返回。注意,在使用该方法前,需确保已经导入了java.util.Base64类。
相关问题
java将图片转成base64
Java可以将图片转成base64字符串的方法主要有两种:
1.使用Java自带的Base64类
```
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
public class ImageToBase64 {
public static void main(String[] args) {
String base64Str = null;
InputStream in = null;
try {
in = new FileInputStream("image.jpg");
byte[] bytes = new byte[in.available()];
in.read(bytes);
base64Str = Base64.getEncoder().encodeToString(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(base64Str);
}
}
```
2.使用第三方库Apache Commons Codec
```
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
public class ImageToBase64 {
public static void main(String[] args) {
String base64Str = null;
FileInputStream fileInputStreamReader = null;
try {
File file = new File("image.jpg");
byte[] bytes = new byte[(int) file.length()];
fileInputStreamReader = new FileInputStream(file);
fileInputStreamReader.read(bytes);
base64Str = new String(Base64.encodeBase64(bytes), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(base64Str);
}
}
```
Java将图片转成base64
在Java中,可以使用Base64编码将图片转换为Base64字符串下面是一个示例代码:
```java
import java.io.File;
import jav.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
public class ImageToBase64 {
public static void main(String[] args) {
String imagePath = "path/to/image.jpg"; // 图片文件路径
try {
// 读取图片文件
File file = new File(imagePath);
FileInputStream fis = new FileInputStream(file);
byte[] imageBytes = new byte[(int) file.length()];
fis.read(imageBytes);
fis.close();
// 将图片字节数组转换为Base64字符串
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
System.out.println("Base64字符串: " + base64Image);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
上述代码中,首先通过`FileInputStream`读取图片文件的字节数组,然后使用`Base64.getEncoder().encodeToString()`方法将字节数组转换为Base64字符串。最后打印输出Base64字符串。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)