use of com.moon.admin.domain.FileInfo in project moon by gentoo111.
the class FileServiceImpl method save.
@Override
public FileInfo save(MultipartFile file) throws IOException {
String fileOrigName = file.getOriginalFilename();
if (!fileOrigName.contains(".")) {
throw new IllegalArgumentException("缺少后缀名");
}
String md5 = FileUtils.fileMd5(file.getInputStream());
FileInfo fileInfo = fileInfoDao.getById(md5);
if (fileInfo != null) {
fileInfoDao.update(fileInfo);
return fileInfo;
}
fileOrigName = fileOrigName.substring(fileOrigName.lastIndexOf("."));
// String path2 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
// 获取根目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
// if (!path.exists()) {
// path = new File("");
// }
// 如果上传目录为/static/images/upload/,则可以如下获取:
File upload = new File(path.getAbsolutePath(), "static/images/upload/");
if (!upload.exists()) {
upload.mkdirs();
}
// 在开发测试模式时,得到的地址为:{项目根目录}/target/static/images/upload/
// 在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/
String absolutePath = upload.getAbsolutePath();
String pathname = FileUtils.getPath() + md5 + fileOrigName;
String fullPath = absolutePath + pathname;
FileUtils.saveFile(file, fullPath);
long size = file.getSize();
String contentType = file.getContentType();
fileInfo = new FileInfo();
fileInfo.setId(md5);
fileInfo.setContentType(contentType);
fileInfo.setSize(size);
fileInfo.setPath(fullPath);
fileInfo.setUrl(pathname);
fileInfo.setType(contentType.startsWith("image/") ? 1 : 0);
fileInfoDao.save(fileInfo);
LOGGER.debug("上传文件{}", fullPath);
return fileInfo;
}
use of com.moon.admin.domain.FileInfo in project moon by gentoo111.
the class FileController method uploadLayuiFile.
/**
* layui富文本文件自定义上传
*
* @param file
* @param domain
* @return
* @throws IOException
*/
@LogAnnotation
@PostMapping("/layui")
@ApiOperation(value = "layui富文本文件自定义上传")
public LayuiFile uploadLayuiFile(MultipartFile file, String domain) throws IOException {
FileInfo fileInfo = fileService.save(file);
LayuiFile layuiFile = new LayuiFile();
layuiFile.setCode(0);
LayuiFile.LayuiFileData data = new LayuiFile.LayuiFileData();
layuiFile.setData(data);
data.setSrc(domain + "/files" + fileInfo.getUrl());
data.setTitle(file.getOriginalFilename());
return layuiFile;
}
Aggregations