Initial commit

This commit is contained in:
yan.y
2024-07-08 14:58:50 +08:00
commit c347facccd
934 changed files with 68903 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.linln.component</groupId>
<artifactId>fileUpload</artifactId>
<packaging>jar</packaging>
<name>组件:文件上传</name>
<parent>
<groupId>com.linln</groupId>
<artifactId>component</artifactId>
<version>2.0.3</version>
</parent>
<dependencies>
<dependency>
<groupId>com.linln</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.linln.modules</groupId>
<artifactId>system</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,155 @@
package com.linln.component.fileUpload;
import com.linln.common.exception.ResultException;
import com.linln.common.utils.SpringContextUtil;
import com.linln.common.utils.ToolUtil;
import com.linln.component.fileUpload.config.properties.UploadProjectProperties;
import com.linln.component.fileUpload.enums.UploadResultEnum;
import com.linln.modules.system.domain.Upload;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* 文件上传处理工具
* @author gion
* @date 2018/11/4
*/
public class FileUpload {
/**
* 创建一个Upload实体对象
* @param multipartFile MultipartFile对象
* @param modulePath 文件模块路径
*/
public static Upload getFile(MultipartFile multipartFile, String modulePath){
if (multipartFile.getSize() == 0){
throw new ResultException(UploadResultEnum.NO_FILE_NULL);
}
Upload upload = new Upload();
upload.setMime(multipartFile.getContentType());
upload.setSize(multipartFile.getSize());
upload.setName(FileUpload.genFileName(multipartFile.getOriginalFilename()));
upload.setPath(getPathPattern() + modulePath + FileUpload.genDateMkdir("yyyyMMdd") + upload.getName());
return upload;
}
/**
* 判断文件是否为支持的格式
* @param multipartFile MultipartFile对象
* @param types 支持的文件类型数组
*/
public static boolean isContentType(MultipartFile multipartFile, String[] types){
List<String> typeList = Arrays.asList(types);
return typeList.contains(multipartFile.getContentType());
}
/**
* 获取文件上传保存路径
*/
public static String getUploadPath(){
UploadProjectProperties properties = SpringContextUtil.getBean(UploadProjectProperties.class);
return properties.getFilePath();
}
/**
* 获取文件上传目录的静态资源路径
*/
public static String getPathPattern(){
UploadProjectProperties properties = SpringContextUtil.getBean(UploadProjectProperties.class);
return properties.getStaticPath().replace("/**", "");
}
/**
* 生成随机且唯一的文件名
*/
public static String genFileName(String originalFilename){
String fileSuffix = ToolUtil.getFileSuffix(originalFilename);
return UUID.randomUUID().toString().replace("-", "") + fileSuffix;
}
/**
* 生成指定格式的目录名称(日期格式)
*/
public static String genDateMkdir(String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
return "/" + sdf.format(new Date()) + "/";
}
/**
* 获取目标文件对象
* @param upload 上传实体类
*/
public static File getDestFile(Upload upload) throws IOException {
// 创建保存文件对象
String path = upload.getPath().replace(getPathPattern(), "");
String filePath = getUploadPath() + path;
File dest = new File(filePath.replace("//", "/"));
if(!dest.exists()){
dest.getParentFile().mkdirs();
dest.createNewFile();
}
return dest;
}
/**
* 保存文件及获取文件MD5值和SHA1值
* @param multipartFile MultipartFile对象
* @param upload Upload
*/
public static void transferTo(MultipartFile multipartFile, Upload upload) throws IOException, NoSuchAlgorithmException {
byte[] buffer = new byte[4096];
MessageDigest md5 = MessageDigest.getInstance("MD5");
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
try (OutputStream fos = Files.newOutputStream(getDestFile(upload).toPath()); InputStream fis = multipartFile.getInputStream()) {
int len = 0;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
md5.update(buffer, 0, len);
sha1.update(buffer, 0, len);
}
fos.flush();
}
BigInteger md5Bi = new BigInteger(1, md5.digest());
BigInteger sha1Bi = new BigInteger(1, sha1.digest());
upload.setMd5(md5Bi.toString(16));
upload.setSha1(sha1Bi.toString(16));
}
/**
* 获取文件的SHA1值
*/
public static String getFileSha1(MultipartFile multipartFile) {
if (multipartFile.getSize() == 0){
throw new ResultException(UploadResultEnum.NO_FILE_NULL);
}
byte[] buffer = new byte[4096];
try (InputStream fis = multipartFile.getInputStream()) {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
int len = 0;
while ((len = fis.read(buffer)) != -1) {
sha1.update(buffer, 0, len);
}
BigInteger sha1Bi = new BigInteger(1, sha1.digest());
return sha1Bi.toString(16);
} catch (IOException | NoSuchAlgorithmException e) {
return null;
}
}
}
@@ -0,0 +1,11 @@
package com.linln.component.fileUpload.config;
import org.springframework.context.annotation.ComponentScan;
/**
* @author gion
* @date 2021/3/19
*/
@ComponentScan(basePackages = "com.linln.component.fileUpload")
public class FileUploadAutoConfig {
}
@@ -0,0 +1,22 @@
package com.linln.component.fileUpload.config;
import com.linln.common.utils.SpringContextUtil;
import com.linln.component.fileUpload.FileUpload;
import com.linln.component.fileUpload.config.properties.UploadProjectProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author gion
* @date 2018/11/3
*/
@Configuration
public class UploadFileConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
UploadProjectProperties properties = SpringContextUtil.getBean(UploadProjectProperties.class);
registry.addResourceHandler(properties.getStaticPath()).addResourceLocations("file:" + FileUpload.getUploadPath());
}
}
@@ -0,0 +1,34 @@
package com.linln.component.fileUpload.config.properties;
import com.linln.common.utils.ToolUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 项目-文件上传配置项
* @author gion
* @date 2018/11/6
*/
@Data
@Component
@ConfigurationProperties(prefix = "project.upload")
public class UploadProjectProperties {
/** 上传文件路径 */
private String filePath;
/** 上传文件静态访问路径 */
private String staticPath = "/upload/**";
/** 获取文件路径 */
public String getFilePath() {
if (filePath == null) {
return ToolUtil.getProjectPath() + "/upload/";
}
if (!filePath.endsWith("/")) {
return filePath + "/";
}
return filePath;
}
}
@@ -0,0 +1,30 @@
package com.linln.component.fileUpload.enums;
import com.linln.common.exception.interfaces.ResultInterface;
import lombok.Getter;
/**
* 后台返回结果集枚举
* @author gion
* @date 2018/8/14
*/
@Getter
public enum UploadResultEnum implements ResultInterface {
/**
* 文件操作
*/
NO_FILE_NULL(401, "文件不能为空"),
NO_FILE_TYPE(402, "不支持该文件类型"),
;
private Integer code;
private String message;
UploadResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.linln.component.fileUpload.config.FileUploadAutoConfig