public class Base64ToMultipartFile implements MultipartFile {
private static Logger logger = LoggerFactory.getLogger(Base64ToMultipartFile.class);
private final byte[] imgContent;
private final String header;
public Base64ToMultipartFile(byte[] imgContent, String header) {
this.imgContent = imgContent;
this.header = header.split(";")[0];
}
@Override
public String getName() {
return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
}
@Override
public String getOriginalFilename() {
return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
}
@Override
public String getContentType() {
return header.split(":")[1];
}
@Override
public boolean isEmpty() {
return imgContent == null || imgContent.length == 0;
}
@Override
public long getSize() {
return imgContent.length;
}
@Override
public byte[] getBytes() throws IOException {
return imgContent;
}
@Override
public InputStream getInputStream() throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imgContent);
// 关闭流,不然文件会一直占用资源,无法对其操作
byteArrayInputStream.close();
return byteArrayInputStream;
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
FileOutputStream fileOutputStream = new FileOutputStream(dest);
fileOutputStream.write(imgContent);
// 关闭流,不然文件会一直占用资源,无法对其操作
fileOutputStream.close();
}
/**
* base64转multipartFile
*
* @param
* @return
*/
public static MultipartFile base64Convert(String base64) {
String[] baseStrs = base64.split(",");
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
try {
b = decoder.decodeBuffer(baseStrs[1]);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
return new Base64ToMultipartFile(b, baseStrs[0]);
}
public static CommonsMultipartFile base64toMultipartFile(String base64filestr){
final File file = base64ToFile(base64filestr);
if(file.exists()){
logger.info("存在");
logger.info("长度"+file.length());
}
DiskFileItem item = (DiskFileItem)new DiskFileItemFactory().createItem(file.getName(), "image/jpeg", false, file.getName());
InputStream input = null;
try {
input = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStream os = null;
try {
os = item.getOutputStream();
int ret = input.read();
while ( ret != -1 )
{
os.write(ret);
ret = input.read();
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
return new CommonsMultipartFile(item);
}
public static File base64ToFile(String base64) {
if(base64==null||"".equals(base64)) {
return null;
}
//getRealImgeStr 方法 截取bese64字符串 逗号后半部分有用的字符串
byte[] buff= Base64.decode(getRealImgeStr(base64));
File file=null;
FileOutputStream fout=null;
try {
file = File.createTempFile("tmp", ".jpg");
fout=new FileOutputStream(file);
fout.write(buff);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fout!=null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
public static String getRealImgeStr(String imgStr){
String[] str = imgStr.split(",");
return str[1];
}
}
Base64转换为CommonsMultipartFile
最新推荐文章于 2024-07-21 02:52:23 发布
该代码实现了一个将Base64编码的图像数据转换为MultipartFile对象的类,支持文件的读取、写入和传输。主要方法包括从Base64字符串解码,创建MultipartFile实例,以及将Base64内容写入临时文件。
摘要由CSDN通过智能技术生成