|
|
@@ -0,0 +1,82 @@
|
|
|
+package com.fs.wxcid;
|
|
|
+
|
|
|
+import javax.imageio.stream.FileImageInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 网络图片转Base64工具类
|
|
|
+ */
|
|
|
+public class FileToBase64Util {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 网络文件 URL 转 byte[]
|
|
|
+ *
|
|
|
+ * @param fileUrl 文件的网络URL
|
|
|
+ * @return 文件字节数组
|
|
|
+ */
|
|
|
+ public static byte[] downloadToBytes(String fileUrl) throws Exception {
|
|
|
+ if (fileUrl == null || fileUrl.trim().isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("文件URL不能为空");
|
|
|
+ }
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ InputStream inputStream = null;
|
|
|
+ ByteArrayOutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ URL url = new URL(fileUrl);
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setConnectTimeout(5000);
|
|
|
+ connection.setReadTimeout(10000);
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+ if (responseCode != HttpURLConnection.HTTP_OK) {
|
|
|
+ throw new Exception("文件URL访问失败,响应码:" + responseCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ inputStream = connection.getInputStream();
|
|
|
+ outputStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ return outputStream.toByteArray();
|
|
|
+ } finally {
|
|
|
+ if (outputStream != null) outputStream.close();
|
|
|
+ if (inputStream != null) inputStream.close();
|
|
|
+ if (connection != null) connection.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 网络文件 URL 转 Base64 编码字符串
|
|
|
+ *
|
|
|
+ * @param imageUrl 文件的网络URL
|
|
|
+ * @return Base64编码字符串(不带data:image/xxx;base64,前缀)
|
|
|
+ */
|
|
|
+ public static String convertImageUrlToBase64(String imageUrl) throws Exception {
|
|
|
+ byte[] bytes = downloadToBytes(imageUrl);
|
|
|
+ return Base64.getEncoder().encodeToString(bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ FileInputStream inputStream = new FileInputStream("F:\\google下载\\a71990bf-4d22-4e9e-9d58-05bf99b6784b.mp3");
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ byte[] bytes = outputStream.toByteArray();
|
|
|
+ System.out.printf(Base64.getEncoder().encodeToString(bytes));
|
|
|
+// String s = convertImageUrlToBase64("https://cdn.his.cdwjyyh.com/fs/20250627/ec72b8ea378340b2b804f38983f1e5e8.wav");
|
|
|
+// System.out.println(s);
|
|
|
+ }
|
|
|
+}
|