forked from integrated_whb/integrated_whb
149 lines
4.8 KiB
Java
149 lines
4.8 KiB
Java
package com.zcloud.util;
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import org.apache.commons.fileupload.FileItem;
|
|
import org.apache.commons.fileupload.disk.DiskFileItem;
|
|
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
import java.io.*;
|
|
import java.net.URL;
|
|
import java.nio.file.Files;
|
|
import java.util.UUID;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class ImageUtil {
|
|
|
|
/**
|
|
* 图片路径层级分隔符
|
|
*/
|
|
private static final String separator = "/";
|
|
|
|
public static MultipartFile base64ToMultipartFile(String baseImg) {
|
|
|
|
//定义一个正则表达式的筛选规则,为了获取图片的类型
|
|
String rgex = "data:image/(.*?);base64";
|
|
if (StrUtil.isBlank(baseImg)) {
|
|
return null;
|
|
}
|
|
String type = getSubUtilSimple(baseImg, rgex);
|
|
//去除base64图片的前缀
|
|
baseImg = baseImg.replaceFirst("data:(.+?);base64,", "");
|
|
byte[] imageByte;
|
|
String fileName = "";
|
|
//把图片转换成二进制
|
|
imageByte = Base64.decode(baseImg.replaceAll(" ", "+"));
|
|
|
|
//随机生成图片的名字,同时根据类型结尾
|
|
fileName = UUID.randomUUID().toString() + "." + type;
|
|
|
|
InputStream inputStream = new ByteArrayInputStream(imageByte);
|
|
MockMultipartFile mockMultipartFile = null;
|
|
try {
|
|
mockMultipartFile = new MockMultipartFile(fileName, fileName, "", inputStream);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return mockMultipartFile;
|
|
}
|
|
|
|
|
|
public static String getSubUtilSimple(String soap, String rgex) {
|
|
Pattern pattern = Pattern.compile(rgex);
|
|
Matcher m = pattern.matcher(soap);
|
|
while (m.find()) {
|
|
return m.group(1);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
/**
|
|
* 根据url 拉取文件
|
|
* @param url
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static File getFile(String url) throws Exception {
|
|
//对本地文件命名
|
|
String fileName = url.substring(url.lastIndexOf("."),url.length());
|
|
File file = null;
|
|
|
|
URL urlfile;
|
|
InputStream inStream = null;
|
|
OutputStream os = null;
|
|
try {
|
|
file = File.createTempFile("fwj_url", fileName);
|
|
//下载
|
|
urlfile = new URL(url);
|
|
inStream = urlfile.openStream();
|
|
os = new FileOutputStream(file);
|
|
|
|
int bytesRead = 0;
|
|
byte[] buffer = new byte[8192];
|
|
while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
|
|
os.write(buffer, 0, bytesRead);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
try {
|
|
if (null != os) {
|
|
os.close();
|
|
}
|
|
if (null != inStream) {
|
|
inStream.close();
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
/**
|
|
* 根据url获取图片并转换为multipartFile类型
|
|
* @param url
|
|
* @return
|
|
*/
|
|
public static MultipartFile getMultipartFile(String url) {
|
|
FileInputStream inputStream = null;
|
|
OutputStream outputStream = null;
|
|
try {
|
|
File file = getFile(url);
|
|
System.out.println(file.toPath());
|
|
FileItem fileItem = new DiskFileItem("formFieldName",//form表单文件控件的名字随便起
|
|
Files.probeContentType(file.toPath()),//文件类型
|
|
false, //是否是表单字段
|
|
file.getName(),//原始文件名
|
|
(int) file.length(),//Interger的最大值可以存储两部1G的电影
|
|
file.getParentFile());//文件会在哪个目录创建
|
|
//为DiskFileItem的OutputStream赋值
|
|
inputStream = new FileInputStream(file);
|
|
outputStream = fileItem.getOutputStream();
|
|
IOUtils.copy(inputStream, outputStream);
|
|
return new CommonsMultipartFile(fileItem);
|
|
} catch (Exception e) {
|
|
System.out.println("文件类型转换失败" + e.getMessage());
|
|
return null;
|
|
} finally {
|
|
try {
|
|
if (null != inputStream) {
|
|
inputStream.close();
|
|
}
|
|
|
|
if (null != outputStream) {
|
|
outputStream.close();
|
|
}
|
|
} catch (IOException e) {
|
|
System.out.println(">>文件流关闭失败" + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|