|
|
|
@ -24,12 +24,18 @@ public class FileUtils {
|
|
|
|
|
* @param url 文件读取路径
|
|
|
|
|
* @param suffix 文件后缀名
|
|
|
|
|
*/
|
|
|
|
|
public static File createFileByUrl(String url, String suffix) {
|
|
|
|
|
// 根据URL创建文件
|
|
|
|
|
public static File createFileByUrl(String url, String suffix) {
|
|
|
|
|
// 从URL获取图片字节数组
|
|
|
|
|
byte[] byteFile = getImageFromNetByUrl(url);
|
|
|
|
|
// 如果字节数组不为空
|
|
|
|
|
if (byteFile != null) {
|
|
|
|
|
// 从字节数组获取文件
|
|
|
|
|
File file = getFileFromBytes(byteFile, suffix);
|
|
|
|
|
// 返回文件
|
|
|
|
|
return file;
|
|
|
|
|
} else {
|
|
|
|
|
// 否则返回null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -42,9 +48,13 @@ public class FileUtils {
|
|
|
|
|
*/
|
|
|
|
|
private static byte[] getImageFromNetByUrl(String strUrl) {
|
|
|
|
|
try {
|
|
|
|
|
// 创建URL对象
|
|
|
|
|
URL url = new URL(strUrl);
|
|
|
|
|
// 打开连接
|
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
// 设置请求方法
|
|
|
|
|
conn.setRequestMethod("GET");
|
|
|
|
|
// 设置连接超时时间
|
|
|
|
|
conn.setConnectTimeout(5 * 1000);
|
|
|
|
|
// 通过输入流获取图片数据
|
|
|
|
|
InputStream inStream = conn.getInputStream();
|
|
|
|
@ -65,41 +75,61 @@ public class FileUtils {
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private static byte[] readInputStream(InputStream inStream) throws Exception {
|
|
|
|
|
// 创建一个ByteArrayOutputStream对象,用于存储读取的字节数据
|
|
|
|
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
|
|
// 创建一个字节数组,用于存储每次读取的字节数据
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
// 定义一个变量,用于存储每次读取的字节数
|
|
|
|
|
int len = 0;
|
|
|
|
|
// 循环读取输入流中的数据,直到读取完毕
|
|
|
|
|
while ((len = inStream.read(buffer)) != -1) {
|
|
|
|
|
// 将读取的字节数据写入ByteArrayOutputStream对象中
|
|
|
|
|
outStream.write(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
// 关闭输入流
|
|
|
|
|
inStream.close();
|
|
|
|
|
// 将ByteArrayOutputStream对象中的字节数据转换为字节数组并返回
|
|
|
|
|
return outStream.toByteArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建临时文件
|
|
|
|
|
private static File getFileFromBytes(byte[] b, String suffix) {
|
|
|
|
|
// 根据字节数组和后缀名创建临时文件
|
|
|
|
|
private static File getFileFromBytes(byte[] b, String suffix) {
|
|
|
|
|
// 定义输出流
|
|
|
|
|
BufferedOutputStream stream = null;
|
|
|
|
|
// 定义文件
|
|
|
|
|
File file = null;
|
|
|
|
|
try {
|
|
|
|
|
// 创建临时文件
|
|
|
|
|
file = File.createTempFile("pattern", "." + suffix);
|
|
|
|
|
// 输出临时文件位置
|
|
|
|
|
System.out.println("临时文件位置:" + file.getCanonicalPath());
|
|
|
|
|
// 创建文件输出流
|
|
|
|
|
FileOutputStream fstream = new FileOutputStream(file);
|
|
|
|
|
// 创建缓冲输出流
|
|
|
|
|
stream = new BufferedOutputStream(fstream);
|
|
|
|
|
// 将字节数组写入文件
|
|
|
|
|
stream.write(b);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 打印异常信息
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
// 关闭输出流
|
|
|
|
|
if (stream != null) {
|
|
|
|
|
try {
|
|
|
|
|
stream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 打印异常信息
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 返回文件
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MultipartFile createImg(String url) {
|
|
|
|
|
// 创建一个MultipartFile对象,根据给定的url
|
|
|
|
|
public static MultipartFile createImg(String url) {
|
|
|
|
|
try {
|
|
|
|
|
// File转换成MutipartFile
|
|
|
|
|
File file = FileUtils.createFileByUrl(url, "jpg");
|
|
|
|
@ -131,12 +161,15 @@ public class FileUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean base64ToFile(String filePath, String base64Data) throws Exception {
|
|
|
|
|
// 定义变量dataPrix和data,用于存储base64Data中的数据
|
|
|
|
|
String dataPrix = "";
|
|
|
|
|
String data = "";
|
|
|
|
|
|
|
|
|
|
// 判断base64Data是否为空
|
|
|
|
|
if (base64Data == null || "".equals(base64Data)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
// 将base64Data按照"base64,"进行分割,获取dataPrix和data
|
|
|
|
|
String[] d = base64Data.split("base64,");
|
|
|
|
|
if (d != null && d.length == 2) {
|
|
|
|
|
dataPrix = d[0];
|
|
|
|
|