All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.dennisit.vplus.data.utils.UploadUtils Maven / Gradle / Ivy
/*--------------------------------------------------------------------------
* Copyright (c) 2010-2020, Elon.su All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the elon developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Elon.su, you can also mail [email protected]
*--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.utils;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Created by Elon.su on 17/9/6.
*/
public class UploadUtils {
private static final Logger LOG = LoggerFactory.getLogger(UploadUtils.class);
public static final String BUCKET = "default";
/**
* 文件存储
* @param path 文件路径
* @param uploadFiles 上传文件
* @param accepts 接收的文件格式
* @return 上传文件集合
*/
public static List uploadFile(String path, MultipartFile[] uploadFiles, List accepts) {
return uploadFile(path, BUCKET, uploadFiles, accepts);
}
/**
* 文件存储
* @param path 文件路径
* @param uploadFile 上传文件
* @param accepts 接收的文件格式
* @return 上传文件集
*/
public static FsUploadFile uploadFile(String path, MultipartFile uploadFile, List accepts) {
return uploadFile(path, BUCKET, uploadFile, accepts);
}
/**
* 保存文件到磁盘
* @param path 保存路径
* @param bucket 文件桶
* @param uploadFiles 目标文件 文件类型
* @param accepts 接受的文件后缀 Arrays.asList("pdf", "doc", "docx")
* @return 返回上传文件的包装属性信息
*/
public static List uploadFile(String path, String bucket, MultipartFile[] uploadFiles, List accepts) {
List files = Lists.newArrayList();
Optional.ofNullable(Lists.newArrayList(uploadFiles)).orElse(Lists.newArrayList())
.parallelStream()
.forEach(x -> {
FsUploadFile file = uploadFile(path, bucket, x, accepts);
if(null != file){
files.add(file);
}
});
return files;
}
/**
* 保存文件到磁盘
* @param path 保存路径
* @param bucket 文件桶
* @param uploadFile 目标文件 文件类型
* @param accepts 接受的文件后缀 Arrays.asList("pdf", "doc", "docx")
* @return 返回上传文件的包装属性信息
*/
public static FsUploadFile uploadFile(String path, String bucket, MultipartFile uploadFile, List accepts) {
path = Optional.ofNullable(path).orElse("./");
bucket = Optional.ofNullable(bucket).orElse(BUCKET);
String finalPath = path.concat(File.separator).concat(bucket);
List extensions = Optional.ofNullable(accepts).orElse(Lists.newArrayList()).stream().map(x -> x.toLowerCase()).collect(Collectors.toList());
if (uploadFile.getSize() > 0) {
String originalFilename = uploadFile.getOriginalFilename();
String originalExtension = FilenameUtils.getExtension(originalFilename).toLowerCase();
if (extensions.contains(originalExtension)) {
String fstoreFilename = IdWorker.get32UUID().concat(FilenameUtils.EXTENSION_SEPARATOR_STR).concat(originalExtension);
String md5 = null;
try {
md5 = DigestUtils.md5Hex(uploadFile.getInputStream());
} catch (IOException e) {
LOG.error("get file md5 error! fileName:" + originalFilename, e);
}
File file = new File(finalPath, fstoreFilename);
try {
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
uploadFile.transferTo(file);
} catch (IOException e) {
LOG.error("upload file error! fileName:" + originalFilename, e);
}
FsUploadFile fsUploadFile = new FsUploadFile();
fsUploadFile.setUuid(String.valueOf(IdWorker.getId()));
fsUploadFile.setOriginalName(originalFilename);
fsUploadFile.setStorageName(fstoreFilename);
fsUploadFile.setExtension(originalExtension);
fsUploadFile.setPath(path);
fsUploadFile.setBucket(bucket);
fsUploadFile.setFinalPath(finalPath);
fsUploadFile.setSize(uploadFile.getSize());
fsUploadFile.setMd5(md5);
fsUploadFile.setContentType(uploadFile.getContentType());
fsUploadFile.setCreateTime(new Date());
return fsUploadFile;
}
}
return null;
}
/**
* 获取上传文件的md5
* @param uploadFiles 文件集合
* @return md5集合
*/
public static List getUploadFileMd5(List uploadFiles){
return Optional.ofNullable(uploadFiles).orElse(Lists.newArrayList())
.stream()
.filter(x -> StringUtils.isNotBlank(x.getMd5()))
.distinct()
.map(x -> x.getMd5())
.collect(Collectors.toList());
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class FsUploadFile {
private String bucket;
private String path;
private String finalPath;
private String uuid;
private String originalName;
private String storageName;
private long size;
private String extension;
private String contentType;
private String md5;
private Date createTime;
}
public static void main(String[] args) {
}
}