1.vue端设置
- 将ueditor复制到public下,如下图
- ueditor.config.js 文件设置内容:
- 在对应的页面引入:
import VueUeditorWrap from "vue-ueditor-wrap";
export default {
components:{
VueUeditorWrap
},
name: "Questionbank",
dicts: ['question_type'],
data() {
return {
myConfig: {
autoHeightEnabled: false,
initialFrameHeight: 400,
initialFrameWidth: "100%",
UEDITOR_HOME_URL: "/UEditor/",
toolbars: [
['fontfamily','fontsize','|','removeformat','formatmatch','spechars','superscript','subscript','|', 'pasteplain', '|','bold','italic','underline','horizontal'],
['indent','lineheight', 'rowspacingtop','rowspacingbottom','|','insertorderedlist','insertunorderedlist','autotypeset','|','justifyleft','justifyright',
'justifycenter', 'justifyjustify','|','insertimage','searchreplace']
]
}
}
}
- 页面中需要富文本的填写:
<vue-ueditor-wrap v-model="form.answer" :config="myConfig"></vue-ueditor-wrap>
2. 后端代码
package com.ruoyi.ueditor;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.BadRequestException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.ue.FileProperties;
import com.ruoyi.common.utils.ue.FileUtil;
import com.ruoyi.common.utils.ue.UeFileManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
@RestController
public class UEditorController {
@Autowired
private FileProperties fileProperties;
@GetMapping(value = "/ue/config")
public void ueConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
if ("listimage".equalsIgnoreCase(request.getParameter("action"))) {
int start = Integer.parseInt(request.getParameter("start"));
String urlPrefix = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/";
File file = new File(urlPrefix);
UeFileManager fileManager = new UeFileManager(file.getParent() + File.separator, file.getName(), new String[]{"png", "jpg", "jpeg", "gif", "bmp"}, 20);
response.getWriter().write(fileManager.listFile(start).toJSONString());
return;
}
String urlPrefix = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/";
String exec = "{\n" +
" /* 上传图片配置项 */\n" +
" \"imageActionName\": \"uploadimage\", /* 执行上传图片的action名称 */\n" +
" \"imageFieldName\": \"upfile\", /* 提交的图片表单名称 */\n" +
" \"imageMaxSize\": 2048000, /* 上传大小限制,单位B */\n" +
" \"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"], /* 上传图片格式显示 */\n" +
" \"imageCompressEnable\": true, /* 是否压缩图片,默认是true */\n" +
" \"imageCompressBorder\": 1600, /* 图片压缩最长边限制 */\n" +
" \"imageInsertAlign\": \"none\", /* 插入的图片浮动方式 */\n" +
" \"imageUrlPrefix\": \"" + urlPrefix + "\", /* 图片访问路径前缀 */\n" +
" \"imagePathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\", /* 上传保存路径,可以自定义保存路径和文件名格式 */\n" +
" /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */\n" +
" /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */\n" +
" /* {time} 会替换成时间戳 */\n" +
" /* {yyyy} 会替换成四位年份 */\n" +
" /* {yy} 会替换成两位年份 */\n" +
" /* {mm} 会替换成两位月份 */\n" +
" /* {dd} 会替换成两位日期 */\n" +
" /* {hh} 会替换成两位小时 */\n" +
" /* {ii} 会替换成两位分钟 */\n" +
" /* {ss} 会替换成两位秒 */\n" +
" /* 非法字符 \\ : * ? \" < > | */\n" +
" /* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */\n" +
"\n" +
" /* 涂鸦图片上传配置项 */\n" +
" \"scrawlActionName\": \"uploadscrawl\", /* 执行上传涂鸦的action名称 */\n" +
" \"scrawlFieldName\": \"upfile\", /* 提交的图片表单名称 */\n" +
" \"scrawlPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\", /* 上传保存路径,可以自定义保存路径和文件名格式 */\n" +
" \"scrawlMaxSize\": 2048000, /* 上传大小限制,单位B */\n" +
" \"scrawlUrlPrefix\": \"\", /* 图片访问路径前缀 */\n" +
" \"scrawlInsertAlign\": \"none\",\n" +
"\n" +
" /* 截图工具上传 */\n" +
" \"snapscreenActionName\": \"uploadimage\", /* 执行上传截图的action名称 */\n" +
" \"snapscreenPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\", /* 上传保存路径,可以自定义保存路径和文件名格式 */\n" +
" \"snapscreenUrlPrefix\": \"\", /* 图片访问路径前缀 */\n" +
" \"snapscreenInsertAlign\": \"none\", /* 插入的图片浮动方式 */\n" +
"\n" +
" /* 抓取远程图片配置 */\n" +
" \"catcherLocalDomain\": [\"127.0.0.1\", \"localhost\", \"img.baidu.com\"],\n" +
" \"catcherActionName\": \"catchimage\", /* 执行抓取远程图片的action名称 */\n" +
" \"catcherFieldName\": \"source\", /* 提交的图片列表表单名称 */\n" +
" \"catcherPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\", /* 上传保存路径,可以自定义保存路径和文件名格式 */\n" +
" \"catcherUrlPrefix\": \"" + urlPrefix + "\", /* 图片访问路径前缀 */\n" +
" \"catcherMaxSize\": 2048000, /* 上传大小限制,单位B */\n" +
" \"catcherAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"], /* 抓取图片格式显示 */\n" +
"\n" +
" /* 上传视频配置 */\n" +
" \"videoActionName\": \"uploadvideo\", /* 执行上传视频的action名称 */\n" +
" \"videoFieldName\": \"upfile\", /* 提交的视频表单名称 */\n" +
" \"videoPathFormat\": \"/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}\", /* 上传保存路径,可以自定义保存路径和文件名格式 */\n" +
" \"videoUrlPrefix\": \"\", /* 视频访问路径前缀 */\n" +
" \"videoMaxSize\": 102400000, /* 上传大小限制,单位B,默认100MB */\n" +
" \"videoAllowFiles\": [\n" +
" \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
" \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\"], /* 上传视频格式显示 */\n" +
" /* 上传文件配置 */\n" +
" \"fileActionName\": \"uploadfile\", /* controller里,执行上传视频的action名称 */\n" +
" \"fileFieldName\": \"upfile\", /* 提交的文件表单名称 */\n" +
" \"filePathFormat\": \"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\", /* 上传保存路径,可以自定义保存路径和文件名格式 */\n" +
" \"fileUrlPrefix\": \"\", /* 文件访问路径前缀 */\n" +
" \"fileMaxSize\": 51200000, /* 上传大小限制,单位B,默认50MB */\n" +
" \"fileAllowFiles\": [\n" +
" \".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\",\n" +
" \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
" \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\",\n" +
" \".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\",\n" +
" \".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"\n" +
" ], /* 上传文件格式显示 */\n" +
" /* 列出指定目录下的图片 */\n" +
" \"imageManagerActionName\": \"listimage\", /* 执行图片管理的action名称 */\n" +
" \"imageManagerListPath\": \"/ueditor/jsp/upload/image/\", /* 指定要列出图片的目录 */\n" +
" \"imageManagerListSize\": 20, /* 每次列出文件数量 */\n" +
" \"imageManagerUrlPrefix\": \"" + urlPrefix + "\", /* 图片访问路径前缀 */\n" +
" \"imageManagerInsertAlign\": \"none\", /* 插入的图片浮动方式 */\n" +
" \"imageManagerAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"], /* 列出的文件类型 */\n" +
" /* 列出指定目录下的文件 */\n" +
" \"fileManagerActionName\": \"listfile\", /* 执行文件管理的action名称 */\n" +
" \"fileManagerListPath\": \"/ueditor/jsp/upload/file/\", /* 指定要列出文件的目录 */\n" +
" \"fileManagerUrlPrefix\": \"\", /* 文件访问路径前缀 */\n" +
" \"fileManagerListSize\": 20, /* 每次列出文件数量 */\n" +
" \"fileManagerAllowFiles\": [\n" +
" \".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\",\n" +
" \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
" \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\",\n" +
" \".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\",\n" +
" \".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"\n" +
" ] /* 列出的文件类型 */\n" +
"}";
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
}
@PostMapping(value = "/ue/config", params = {"action=catchimage"})
public Object catcherImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
JSONObject result = new JSONObject();
result.put("state", "SUCCESS");
String[] sources = request.getParameterValues("source[]");
if (sources == null) {
return result;
}
JSONArray list = new JSONArray();
for (String source : sources) {
JSONObject object = new JSONObject();
object.put("state", "SUCCESS");
object.put("source", source);
object.put("url", FileUtil.catchImage(source));
list.add(object);
}
result.put("list", list);
return result;
}
@PostMapping("/ue/config")
public Object updateFile(MultipartFile upfile) throws Exception{
String suffix = FileUtil.getExtensionName(upfile.getOriginalFilename());
String type = FileUtil.getFileType(suffix);
String path = FileUploadUtils.upload(RuoYiConfig.getProfile() + "/answerBank", upfile, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
if (path == null) {
throw new BadRequestException("上传失败");
}
String name = FileUtil.getFileNameNoEx(upfile.getOriginalFilename());
JSONObject result = new JSONObject();
result.put("state", "SUCCESS");
result.put("title", name);
result.put("size", FileUtil.getSize(upfile.getSize()));
result.put("url", path.replaceFirst("/", ""));
result.put("type", type);
result.put("original", path.substring(path.lastIndexOf("/") + 1));
return result;
}
}
package com.ruoyi.common.utils.file;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Objects;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.Seq;
public class FileUploadUtils
{
public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
public static final int DEFAULT_FILE_NAME_LENGTH = 100;
private static String defaultBaseDir = RuoYiConfig.getProfile();
public static void setDefaultBaseDir(String defaultBaseDir)
{
FileUploadUtils.defaultBaseDir = defaultBaseDir;
}
public static String getDefaultBaseDir()
{
return defaultBaseDir;
}
public static final String upload(MultipartFile file) throws IOException
{
try
{
return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e.getMessage(), e);
}
}
public static final String upload(String baseDir, MultipartFile file) throws IOException
{
try
{
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e.getMessage(), e);
}
}
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
InvalidExtensionException
{
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
{
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file, allowedExtension);
String fileName = extractFilename(file);
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
file.transferTo(Paths.get(absPath));
return getPathFileName(baseDir, fileName);
}
public static final String extractFilename(MultipartFile file)
{
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
}
public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
{
File desc = new File(uploadDir + File.separator + fileName);
if (!desc.exists())
{
if (!desc.getParentFile().exists())
{
desc.getParentFile().mkdirs();
}
}
return desc;
}
public static final String getPathFileName(String uploadDir, String fileName) throws IOException
{
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
}
public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, InvalidExtensionException
{
long size = file.getSize();
if (size > DEFAULT_MAX_SIZE)
{
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
}
String fileName = file.getOriginalFilename();
String extension = getExtension(file);
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
{
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
{
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
fileName);
}
else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
{
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
fileName);
}
else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
{
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
fileName);
}
else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
{
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
fileName);
}
else
{
throw new InvalidExtensionException(allowedExtension, extension, fileName);
}
}
}
public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
{
for (String str : allowedExtension)
{
if (str.equalsIgnoreCase(extension))
{
return true;
}
}
return false;
}
public static final String getExtension(MultipartFile file)
{
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (StringUtils.isEmpty(extension))
{
extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
}
return extension;
}
}
package com.ruoyi.common.utils.ue;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
public class UeFileManager {
private String dir = null;
private String rootPath = null;
private String[] allowFiles = null;
private int count = 0;
public UeFileManager(String rootPath, String dir, String[] allowFiles, Integer count) {
this.rootPath = rootPath;
this.dir = this.rootPath + dir;
this.allowFiles = allowFiles;
this.count = count;
}
public JSONObject listFile(int index) {
File dir = new File(this.dir);
JSONObject state = new JSONObject();
Collection<File> list = FileUtils.listFiles(dir, this.allowFiles, true);
if (index >= 0 && index <= list.size()) {
Object[] fileList = Arrays.copyOfRange(list.toArray(), index, index + this.count);
state.put("list", this.getState(fileList));
}
state.put("state", "SUCCESS");
state.put("start", index);
state.put("total", list.size());
return state;
}
private JSONArray getState(Object[] files) {
JSONArray state = new JSONArray();
File file = null;
JSONObject fileState = new JSONObject(true);
for (Object obj : files) {
if (obj == null) {
break;
}
file = (File) obj;
fileState = new JSONObject(true);
fileState.put("url", this.getPath(file));
fileState.put("state", "SUCCESS");
state.add(fileState);
}
System.out.println(state);
return state;
}
private String getPath(File file) {
String path = file.getAbsolutePath();
return path.replace(this.rootPath, "").replaceAll(File.separator + File.separator, "/");
}
}
package com.ruoyi.common.utils.ue;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.ruoyi.common.exception.BadRequestException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class FileUtil extends cn.hutool.core.io.FileUtil {
private static final int GB = 1024 * 1024 * 1024;
private static final int MB = 1024 * 1024;
private static final int KB = 1024;
private static final DecimalFormat DF = new DecimalFormat("0.00");
private static final FileProperties properties = SpringUtils.getBean(FileProperties.class);
public static File toFile(MultipartFile multipartFile) {
String fileName = multipartFile.getOriginalFilename();
String prefix = "." + getExtensionName(fileName);
File file = null;
try {
file = File.createTempFile(IdUtil.simpleUUID(), prefix);
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
public static File toFile(MultipartFile file, String path) throws IOException {
File dest = new File(path).getCanonicalFile();
if (!dest.getParentFile().exists()) {
if (!dest.getParentFile().mkdirs()) {
System.out.println("was not successful.");
}
}
file.transferTo(dest);
return dest;
}
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
public static String getSize(long size) {
String resultSize;
if (size / GB >= 1) {
resultSize = DF.format(size / (float) GB) + "GB ";
} else if (size / MB >= 1) {
resultSize = DF.format(size / (float) MB) + "MB ";
} else if (size / KB >= 1) {
resultSize = DF.format(size / (float) KB) + "KB ";
} else {
resultSize = size + "B ";
}
return resultSize;
}
public static File uploadAllpic(MultipartFile file, String filePath) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");
String suffix = getExtensionName(file.getOriginalFilename());
String nowStr = format.format(date);
try {
String fileName = nowStr + "." + suffix;
String path = filePath + fileName;
return toFile(file, path);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static File upload(MultipartFile file, String filePath) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");
String name = getFileNameNoEx(file.getOriginalFilename());
String suffix = getExtensionName(file.getOriginalFilename());
String nowStr = "-" + format.format(date);
try {
String fileName = name + nowStr + "." + suffix;
String path = filePath + fileName;
return toFile(file, path);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String uploadFile(MultipartFile file) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
String suffix = getExtensionName(file.getOriginalFilename());
String folderName = new SimpleDateFormat("yyyy-MM-dd").format(date);
try {
String fileName = format.format(date) + "." + suffix;
String path = properties.getPath().getPath() + folderName + File.separator + fileName;
toFile(file, path);
return "/file/" + folderName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void delete(String path) {
if (StringUtils.isBlank(path)) {
return;
}
if (path.startsWith("/file")) {
String p = properties.getPath().getPath() + path.replaceAll("/file", "").replaceAll("/", File.separator);
del(p);
return;
}
if (path.startsWith("/avatar")) {
String p = properties.getPath().getAvatar() + path.replaceAll("/avatar", "").replaceAll("/", File.separator);
del(p);
return;
}
}
public static String uploadPic(MultipartFile multipartFile, String filePath) {
File file = uploadAllpic(multipartFile, properties.getPath().getPicurl() + filePath + File.separator);
if (file == null) {
return null;
}
String url = File.separator + "picurl" + File.separator + filePath + File.separator + file.getName();
return url.replaceAll("\\\\", "/");
}
public static String uploadAvatar(MultipartFile multipartFile, String filePath) {
File file = upload(multipartFile, properties.getPath().getAvatar() + filePath + File.separator);
if (file == null) {
return null;
}
String url = File.separator + "avatar" + File.separator + filePath + File.separator + file.getName();
return url.replaceAll("\\\\", "/");
}
public static String uploadActivity(MultipartFile file, String folderName) {
if (file == null) {
return null;
}
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
String suffix = getExtensionName(file.getOriginalFilename());
try {
String fileName = format.format(date) + "." + suffix;
String path = properties.getPath().getActivity() + folderName + File.separator + fileName;
toFile(file, path);
return "/activity/" + folderName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static List<String> uploadFeedback(List<MultipartFile> files) {
List<String> urls = new ArrayList<>(5);
if (files == null || files.isEmpty()) {
return urls;
}
Date date = new Date();
String dateStr = new SimpleDateFormat("yyyyMMdd").format(date);
String dateStr2 = new SimpleDateFormat("yyyyMMddHHmmssS").format(date);
Random random = new Random();
for (MultipartFile file : files) {
String suffix = getExtensionName(file.getOriginalFilename());
try {
String fileName = dateStr2 + random.nextInt(100) + "." + suffix;
String path = properties.getPath().getFeedback() + dateStr + File.separator + fileName;
toFile(file, path);
urls.add("/feedback/" + dateStr + "/" + fileName); ;
} catch (Exception e) {
e.printStackTrace();
}
}
return urls;
}
public static String uploadProductPhoto(MultipartFile file) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
String suffix = getExtensionName(file.getOriginalFilename());
String folderName = new SimpleDateFormat("yyyy-MM-dd").format(date);
try {
String fileName = format.format(date) + "." + suffix;
String path = properties.getPath().getProduct() + folderName + File.separator + fileName;
toFile(file, path);
return "/product/" + folderName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
writer.write(list, true);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
ServletOutputStream out = response.getOutputStream();
file.deleteOnExit();
writer.flush(out, true);
IoUtil.close(out);
}
public static String getFileType(String type) {
String documents = "txt doc pdf ppt pps xlsx xls docx";
String music = "mp3 wav wma mpa ram ra aac aif m4a";
String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
if (image.contains(type)) {
return "图片";
} else if (documents.contains(type)) {
return "文档";
} else if (music.contains(type)) {
return "音乐";
} else if (video.contains(type)) {
return "视频";
} else {
return "其他";
}
}
public static void checkSize(long maxSize, long size) {
int len = 1024 * 1024;
if (size > (maxSize * len)) {
throw new BadRequestException("文件超出规定大小");
}
}
public static boolean check(File file1, File file2) {
String img1Md5 = getMd5(file1);
String img2Md5 = getMd5(file2);
return img1Md5.equals(img2Md5);
}
public static boolean check(String file1Md5, String file2Md5) {
return file1Md5.equals(file2Md5);
}
private static byte[] getByte(File file) {
byte[] b = new byte[(int) file.length()];
try {
InputStream in = new FileInputStream(file);
try {
System.out.println(in.read(b));
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return b;
}
private static String getMd5(byte[] bytes) {
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(bytes);
byte[] md = mdTemp.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file, boolean deleteOnExit) {
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
IOUtils.copy(fis, response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
if (deleteOnExit) {
file.deleteOnExit();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String getMd5(File file) {
return getMd5(getByte(file));
}
public static String catchImage(String imageSource) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
String suffix = getExtensionName(imageSource);
String folderName = new SimpleDateFormat("yyyy-MM-dd").format(date);
String fileName = format.format(date) + "." + suffix;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f = new File(properties.getPath().getPath() + File.separator + folderName);
f.mkdirs();
try {
URL urlfile = new URL(imageSource);
HttpURLConnection httpUrl = (HttpURLConnection) urlfile.openConnection();
httpUrl.connect();
File saveFile = new File(f, fileName);
bis = new BufferedInputStream(httpUrl.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
int len = 2048;
byte[] b = new byte[len];
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
httpUrl.disconnect();
return "file/" + folderName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
package com.ruoyi.common.utils.ue;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "file")
public class FileProperties {
private Long maxSize;
private Long avatarMaxSize;
private ElPath mac;
private ElPath linux;
private ElPath windows;
public ElPath getPath() {
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
return windows;
} else if (os.toLowerCase().startsWith("mac")) {
return mac;
}
return linux;
}
@Data
public static class ElPath {
private String path;
private String avatar;
private String picurl;
private String activity;
private String feedback;
private String product;
}
}