@Service
public class FileServerImpl implements FileServer {
public static final int maxWidth = 100;
//拦截的url,虚拟路径
public String pathPattern = "uploads";
//文件磁盘路径
@Value("${file.upload.path}")
private String fileUploadPath;
@Value(value = "${file.upload.suffix:jpg,jpeg,png,bmp,xls,xlsx,pdf}")
private String fileUploadSuffix;
@Value(value = "${file.upload.is-thumb}")
private Boolean isThumb;
@Value(value = "${file.upload.proportion}")
private Integer proportion;
@Autowired
HttpServletRequest request;
@Autowired
FileMapper fileMapper;
//文件上传
@Override
public FileEntity upload(MultipartFile file) {
FileEntity fileRes = new FileEntity();
if (file.isEmpty()) {
// log.error("the file to be uploaded is empty");
return fileRes;
}
List<String> suffixList = Lists.newArrayList(fileUploadSuffix.split(","));
try {
//校验文件后缀
String originalFilename = file.getOriginalFilename();
//获取文件类型
String type = FileUtil.extName(originalFilename);
//文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
if (!suffixList.contains(suffix)) {
//log.error("unsupported file format");
return fileRes;
}
//获取文件md5
String md5 = SecureUtil.md5(file.getInputStream());
// 从数据库查询是否存在相同的记录
FileEntity dbFiles = fileMapper.queryByMd5(md5);
if (dbFiles != null) { // 文件已存在
return dbFiles;
}
String year = new SimpleDateFormat("yyyy").format(new Date());
String month = new SimpleDateFormat("MM").format(new Date());
String day = new SimpleDateFormat("dd").format(new Date());
String fileDir = fileUploadPath;
String filePath = File.separator + year + File.separator + month + File.separator + day + File.separator;
//首次需生成目录
File folder = new File(fileDir + filePath);
if (!folder.exists()) {
folder.mkdirs();
}
AtomicInteger counter = new AtomicInteger(0);
String uniqueString = String.valueOf(Instant.now().toEpochMilli());
String fileName = uniqueString + "." + suffix;
String absolutePath = fileDir + filePath + fileName;
file.transferTo(new File(absolutePath));
//网页路径
String dataFilePath = pathPattern + "/" + year + "/" + "/" + month + "/" + day + "/" + fileName;
fileRes.setFilePath(dataFilePath);
fileRes.setFileName(fileName);
fileRes.setIp(request.getRemoteAddr());
fileRes.setFileSize(file.getSize() / 1024);
fileRes.setFileType(type);
fileRes.setFileExt(suffix);
fileRes.setFileMd5(md5);
//判断是否生成缩率图
if (isThumb) {
createThumb(fileDir, filePath, uniqueString, suffix);
String dataFileThumbPath = pathPattern + "/" + year + "/" + "/" + month + "/" + day + "/" + uniqueString + "_thumb." + suffix;
fileRes.setThumbPath(dataFileThumbPath);
}
fileMapper.insert(fileRes);
} catch (IOException e) {
e.printStackTrace();
throw new ServiceException(e.getMessage());
}
return fileRes;
}
//生成缩率图
@Override
public String createThumb(String fileDir, String filePath, String fileName, String suffix) {
String localPath = fileDir + filePath + fileName + "." + suffix;
String thumbPath = fileDir + filePath + fileName + "_thumb." + suffix;
//判断缩略图是否存在
Path path = Paths.get(thumbPath);
if (Files.exists(path)) {
return filePath + fileName + "_thumb." + suffix;
}
File originalFile = new File(localPath);
try {
BufferedImage originalImage = ImageIO.read(originalFile);
int imageWidth = originalImage.getWidth();
int imageHeight = originalImage.getHeight();
double thumbWidth = 0;
double thumbHeight = 0;
if (imageWidth > maxWidth || imageHeight > maxWidth) {
thumbWidth = (double) imageWidth / (double) proportion;
thumbHeight = (double) imageHeight / (double) proportion;
}
if (thumbHeight > 0) {
// 创建缩略图
BufferedImage thumbnail = new BufferedImage((int) thumbWidth, (int) thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = thumbnail.createGraphics();
graphics.drawImage(originalImage.getScaledInstance((int) thumbWidth, (int) thumbHeight, Image.SCALE_SMOOTH), 0, 0, null);
graphics.dispose();
// 输出到文件
ImageIO.write(thumbnail, suffix, new File(thumbPath));
return filePath + fileName + "_thumb." + suffix;
}
} catch (IOException e) {
e.printStackTrace();
throw new ServiceException(e.getMessage());
}
return "";
}
}
文件上传思路
最新推荐文章于 2024-09-13 21:40:38 发布