图片上传UploadUtils
Posted lgz0921
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片上传UploadUtils相关的知识,希望对你有一定的参考价值。
import org.apache.tomcat.util.codec.binary.Base64;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class UploadUtils {
//返回的是图片名
public static String convertStringtoImage(String encodedImageStr, String userName) {
String imageName = null;
try {
// Base64解码图片
byte[] imageByteArray = Base64.decodeBase64(encodedImageStr);
String dirPath = "D:/uploadsSpringdd/";
File dateDir = new File(dirPath);
if (!dateDir.exists()) {
dateDir.mkdirs();
}
imageName = userName + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + UUID.randomUUID().toString().replace("-", "") + ".jpg";
String imagePath = dirPath + imageName;
FileOutputStream imageOutFile = new FileOutputStream(imagePath);
imageOutFile.write(imageByteArray);
imageOutFile.close();
System.out.println("Article Successfully Stored");
} catch (FileNotFoundException fnfe) {
System.out.println("Image Path not found" + fnfe);
} catch (IOException ioe) {
System.out.println("Exception while converting the Image " + ioe);
}
return imageName;
}
}
以上是关于图片上传UploadUtils的主要内容,如果未能解决你的问题,请参考以下文章