This commit is contained in:
czhqwer
2025-03-09 00:30:04 +08:00
commit 46bdc08cd5
61 changed files with 5647 additions and 0 deletions
+83
View File
@@ -0,0 +1,83 @@
# Java / Spring Boot 项目忽略项
# 构建产物
*.class
*.jar
*.war
*.ear
target/
build/
# Maven 相关
*.iml
*.ipr
*.iws
.idea/
mvnw
mvnw.cmd
.mvn/
dependency-reduced-pom.xml
# 日志文件
*.log
logs/
*.log.*
# 临时文件
*.tmp
*.bak
*.swp
*~
# 本地配置
*.local
application-local.yml
application-dev.yml
# 数据库相关
*.sql
# Vue 2 项目忽略项
# 构建产物
dist/
dist-ssr/
# 依赖目录
node_modules/
# 日志和临时文件
npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.log
# 本地开发工具
.vscode/
*.sublime-*
*.code-workspace
# 测试相关
coverage/
*.lcov
# 环境变量文件
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# 通用忽略项
# IDE 和编辑器
.DS_Store
Thumbs.db
*.swp
# 本地缓存
.cache/
.temp/
# 其他
upload-file-backend/data/
*.md5
*.sha1
*.sha256
+79
View File
@@ -0,0 +1,79 @@
<?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>cn.czh</groupId>
<artifactId>upload-file-backend</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.9</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.12.404</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,31 @@
package cn.czh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
/**
* 解决前后端分离跨域问题
*/
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
@@ -0,0 +1,30 @@
package cn.czh.base;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Data
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String code; //异常状态码
private String msg; //异常信息
public BusinessException(String msg) {
super(msg);
}
public BusinessException(Throwable cause) {
super(cause);
}
public BusinessException(String msg, Throwable cause) {
super(msg, cause);
}
}
@@ -0,0 +1,57 @@
package cn.czh.base;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
private Integer code;
private String msg;
private T data;
/**
* 操作失败,无返回值
*/
public static <T> Result<T> error(String msg) {
Result<T> responseWrapper = new Result<>();
responseWrapper.setMsg(msg);
responseWrapper.setCode(500);
return responseWrapper;
}
/**
* 操作成功,无返回值
*/
public static Result<?> success() {
Result<?> responseWrapper = new Result<>();
responseWrapper.setCode(200);
responseWrapper.setMsg("操作成功");
return responseWrapper;
}
/**
* 操作成功,有返回值
*/
public static <T> Result<T> success(T data) {
Result<T> responseWrapper = new Result<T>();
responseWrapper.setCode(200);
responseWrapper.setMsg("操作成功");
responseWrapper.setData(data);
return responseWrapper;
}
}
@@ -0,0 +1,15 @@
package cn.czh.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "storage.minio") // 注意前缀
public class MinioProperties {
private String endpoint;
private String accessKey;
private String secretKey;
private String bucket;
}
@@ -0,0 +1,22 @@
package cn.czh.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
@@ -0,0 +1,38 @@
package cn.czh.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
/**
* spring工具类用来获取一些实体类
*/
@Service
public class SpringBeanUtil implements ApplicationContextAware {
private static ApplicationContext appContext;
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
appContext = context;
}
public static ApplicationContext getApplicationContext(){
return appContext;
}
public static <T> T getBean(Class<T> cls) {
return appContext.getBean(cls);
}
public static <T> T getBean(String className) {
return (T) appContext.getBean(className);
}
public static <T> T getBean(String className, Class<T> cls) {
return appContext.getBean(className, cls);
}
}
@@ -0,0 +1,65 @@
package cn.czh.controller;
import cn.czh.base.Result;
import cn.czh.entity.StorageConfig;
import cn.czh.service.IFileService;
import cn.czh.service.IStorageConfigService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@RestController
@RequestMapping("/file")
public class FileController {
@Resource
private IStorageConfigService storageConfigService;
@Resource
private IFileService fileService;
@GetMapping("/preview/{storageType}/**")
public ResponseEntity<byte[]> previewFile(
@PathVariable String storageType,
HttpServletRequest request) throws IOException {
// 从请求中提取剩余路径作为 objectKey
String objectKey = request.getRequestURI()
.substring(request.getContextPath().length())
.replaceFirst("/file/preview/" + storageType + "/", "");
if (StorageConfig.LOCAL.equals(storageType)) {
StorageConfig storageConfig = storageConfigService.getStorageConfigByType(storageType);
String filePath = storageConfig.getBucket() + "\\" + objectKey;
File file = new File(filePath);
if (!file.exists()) {
return ResponseEntity.notFound().build();
}
byte[] fileBytes = Files.readAllBytes(file.toPath());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<>(fileBytes, headers, HttpStatus.OK);
}
return ResponseEntity.badRequest().build();
}
@GetMapping("/page")
public Result<?> pageFiles(Integer page, Integer pageSize, String storageType, String fileName) {
if (page == null) {
page = 1;
}
if (pageSize == null) {
pageSize = 10;
}
return Result.success(fileService.pageFiles(page, pageSize, storageType, fileName));
}
}
@@ -0,0 +1,115 @@
package cn.czh.controller;
import cn.czh.base.Result;
import cn.czh.dto.req.CreateMultipartUpload;
import cn.czh.entity.StorageConfig;
import cn.czh.entity.UploadFile;
import cn.czh.service.IStorageService;
import cn.czh.service.StorageServiceFactory;
import cn.czh.utils.FileTypeUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/upload")
public class UploadController {
@Resource
private StorageServiceFactory storageServiceFactory;
/**
* 从请求中获取存储类型(示例:从请求头获取)
* @param request HTTP 请求对象
* @return 存储类型("MINIO" 或 "LOCAL"
*/
private String getStorageTypeFromRequest(HttpServletRequest request) {
String storageType = request.getHeader("Storage-Type");
if (storageType == null || storageType.trim().isEmpty()) {
storageType = StorageConfig.LOCAL; // 默认值
}
return storageType;
}
@GetMapping("/getUploadProgress")
public Result<?> getUploadProgress(HttpServletRequest request, String identifier) {
String storageType = getStorageTypeFromRequest(request);
IStorageService storageService = storageServiceFactory.getStorageService(storageType);
return Result.success(storageService.getUploadProgress(identifier));
}
@PostMapping("/createMultipartUpload")
public Result<?> createMultipartUpload(HttpServletRequest request, @Valid @RequestBody CreateMultipartUpload req) {
String storageType = getStorageTypeFromRequest(request);
IStorageService storageService = storageServiceFactory.getStorageService(storageType);
return Result.success(storageService.createMultipartUpload(req));
}
@GetMapping("/getPreSignUploadUrl")
public Result<?> getPreSignUploadUrl(HttpServletRequest request, String identifier, Integer partNumber) {
String storageType = getStorageTypeFromRequest(request);
IStorageService storageService = storageServiceFactory.getStorageService(storageType);
UploadFile uploadFile = storageService.getByIdentifier(identifier);
if (uploadFile == null) {
return Result.success("分片任务不存在");
}
Map<String, String> params = new HashMap<>();
params.put("partNumber", partNumber.toString());
params.put("uploadId", uploadFile.getUploadId());
return Result.success(storageService.genPreSignUploadUrl(uploadFile.getObjectKey(), params));
}
@PostMapping("/part")
public ResponseEntity<String> uploadPart(
HttpServletRequest request,
@RequestParam("uploadId") String uploadId,
@RequestParam("partNumber") Integer partNumber,
@RequestParam("file") MultipartFile partFile) {
String storageType = getStorageTypeFromRequest(request);
IStorageService storageService = storageServiceFactory.getStorageService(storageType);
storageService.uploadPart(uploadId, partNumber, partFile);
return ResponseEntity.ok("分片上传成功");
}
@PostMapping("/merge")
public Result<?> merge(HttpServletRequest request, String identifier) {
String storageType = getStorageTypeFromRequest(request);
IStorageService storageService = storageServiceFactory.getStorageService(storageType);
return Result.success(storageService.merge(identifier));
}
@PostMapping
public Result<?> uploadFile(HttpServletRequest request, MultipartFile file, String folder, Integer fileType) throws IOException {
if (file == null || file.getBytes().length == 0) {
return Result.error("上传文件不能为空");
}
String filename = file.getOriginalFilename();
if (fileType != null) {
String fileTypeExists = FileTypeUtil.isFileTypeExists(fileType, filename);
if (fileTypeExists != null) {
return Result.success("数据格式不正确");
}
}
String md5 = SecureUtil.md5(file.getInputStream());
synchronized (md5.intern()) {
String dateFormat = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
assert filename != null;
String objectName = (folder == null ? "default" : folder) + "/" +
dateFormat + "/" + md5 + "." + FileTypeUtil.getFileSuffix(filename);
String storageType = getStorageTypeFromRequest(request);
IStorageService storageService = storageServiceFactory.getStorageService(storageType);
return Result.success(storageService.uploadFile(file, md5, objectName));
}
}
}
@@ -0,0 +1,38 @@
package cn.czh.dto;
import lombok.Data;
@Data
public class FileRecordDTO {
/**
* 文件ID
*/
private Long fileId;
/**
* 分片上传的uploadId
*/
private String uploadId;
/**
* 文件MD5值
*/
private String fileIdentifier;
/**
* 原始文件名称
*/
private String originalName;
/**
* 访问URL
*/
private String accessUrl;
/**
* 下载URL
*/
private String downloadUrl;
}
@@ -0,0 +1,15 @@
package cn.czh.dto;
import java.util.Date;
public interface MyPartSummary {
int getPartNumber();
Date getLastModified();
String getETag();
long getSize();
}
@@ -0,0 +1,31 @@
package cn.czh.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class TaskInfoDTO {
/**
* 是否完成上传(是否已经合并分片)
*/
private boolean finished;
/**
* 文件地址
*/
private String path;
/**
* 上传记录
*/
private TaskRecordDTO taskRecord;
}
@@ -0,0 +1,28 @@
package cn.czh.dto;
import cn.czh.entity.UploadFile;
import cn.hutool.core.bean.BeanUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Data
@ToString
@Accessors(chain = true)
public class TaskRecordDTO extends UploadFile {
/**
* 已上传完的分片
*/
private List<MyPartSummary> exitPartList;
public static TaskRecordDTO convertFromEntity (UploadFile task) {
TaskRecordDTO dto = new TaskRecordDTO();
BeanUtil.copyProperties(task, dto);
return dto;
}
}
@@ -0,0 +1,36 @@
package cn.czh.dto.aws;
import cn.czh.dto.MyPartSummary;
import java.util.Date;
/**
* AWS S3 - PartSummary
*/
public class AwsPartSummaryWrapper implements MyPartSummary {
private final com.amazonaws.services.s3.model.PartSummary awsPartSummary;
public AwsPartSummaryWrapper(com.amazonaws.services.s3.model.PartSummary awsPartSummary) {
this.awsPartSummary = awsPartSummary;
}
@Override
public int getPartNumber() {
return awsPartSummary.getPartNumber();
}
@Override
public Date getLastModified() {
return awsPartSummary.getLastModified();
}
@Override
public String getETag() {
return awsPartSummary.getETag();
}
@Override
public long getSize() {
return awsPartSummary.getSize();
}
}
@@ -0,0 +1,39 @@
package cn.czh.dto.aws;
import cn.czh.dto.MyPartSummary;
import java.util.Date;
public class LocalPartSummary implements MyPartSummary {
private final int partNumber;
private final String eTag;
private final long size;
private final Date lastModified;
public LocalPartSummary(int partNumber, String eTag, long size, Date lastModified) {
this.partNumber = partNumber;
this.eTag = eTag;
this.size = size;
this.lastModified = lastModified;
}
@Override
public int getPartNumber() {
return partNumber;
}
@Override
public Date getLastModified() {
return lastModified;
}
@Override
public String getETag() {
return eTag;
}
@Override
public long getSize() {
return size;
}
}
@@ -0,0 +1,33 @@
package cn.czh.dto.aws;
import cn.czh.dto.MyPartSummary;
import java.util.Date;
public class OssPartSummaryWrapper implements MyPartSummary {
private final com.aliyun.oss.model.PartSummary ossPartSummary;
public OssPartSummaryWrapper(com.aliyun.oss.model.PartSummary ossPartSummary) {
this.ossPartSummary = ossPartSummary;
}
@Override
public int getPartNumber() {
return ossPartSummary.getPartNumber();
}
@Override
public Date getLastModified() {
return ossPartSummary.getLastModified();
}
@Override
public String getETag() {
return ossPartSummary.getETag();
}
@Override
public long getSize() {
return ossPartSummary.getSize();
}
}
@@ -0,0 +1,28 @@
package cn.czh.dto.req;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Data
public class CreateMultipartUpload {
@NotBlank(message = "文件标识不能为空")
private String identifier;
@NotBlank(message = "文件名称不能为空")
private String fileName;
@NotNull(message = "文件大小不能为空")
private Long totalSize;
@NotNull(message = "分片大小不能为空")
private Long chunkSize;
@NotBlank(message = "文件类型不能为空")
private String contentType;
private String folder;
}
@@ -0,0 +1,57 @@
package cn.czh.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("storage_config")
public class StorageConfig implements Serializable {
public static final String LOCAL = "local";
public static final String MINIO = "minio";
public static final String OSS = "oss";
public static final String OBS = "obs";
private static final long serialVersionUID=1L;
/**
* 配置id
*/
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 存储类型:'local','minio','oss','obs'
*/
private String type;
/**
* 访问地址
*/
private String endpoint;
/**
* 用户名
*/
private String accessKey;
/**
* 密码
*/
private String secretKey;
/**
* 桶
*/
private String bucket;
}
@@ -0,0 +1,77 @@
package cn.czh.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
@NoArgsConstructor
@Data
@Accessors(chain = true)
@TableName("upload_file")
public class UploadFile implements Serializable {
/**
* 主键
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 分片上传的uploadId
*/
private String uploadId;
/**
* 文件唯一标识(md5
*/
private String fileIdentifier;
/**
* 文件名
*/
private String fileName;
/**
* 文件的key
*/
private String objectKey;
/**
* 文件大小(byte
*/
private Long totalSize;
/**
* 每个分片大小(byte)
*/
private Long chunkSize;
/**
* 分片数量
*/
private Integer chunkNum;
/**
* 是否已完成上传(完成合并),1是0否
*/
private Integer isFinish;
/**
* 文件类型
*/
private String contentType;
/**
* 访问地址
*/
private String accessUrl;
/**
* 下载地址
*/
private String downloadUrl;
/**
* 存储类型
*/
private String storageType;
/**
* 创建时间
*/
private Date createTime;
}
@@ -0,0 +1,9 @@
package cn.czh.mapper;
import cn.czh.entity.StorageConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StorageConfigMapper extends BaseMapper<StorageConfig> {
}
@@ -0,0 +1,18 @@
package cn.czh.mapper;
import cn.czh.entity.UploadFile;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UploadFileMapper extends BaseMapper<UploadFile> {
IPage<UploadFile> pageFiles(@Param("page") Page<UploadFile> page,
@Param("storageType") String storageType,
@Param("fileName") String fileName);
}
@@ -0,0 +1,10 @@
package cn.czh.service;
import cn.czh.entity.UploadFile;
import com.baomidou.mybatisplus.core.metadata.IPage;
public interface IFileService {
IPage<UploadFile> pageFiles(Integer page, Integer pageSize, String storageType, String fileName);
}
@@ -0,0 +1,12 @@
package cn.czh.service;
import cn.czh.entity.StorageConfig;
public interface IStorageConfigService {
/**
* 根据type获取存储配置
*/
StorageConfig getStorageConfigByType(String type);
}
@@ -0,0 +1,49 @@
package cn.czh.service;
import cn.czh.dto.FileRecordDTO;
import cn.czh.dto.TaskInfoDTO;
import cn.czh.dto.req.CreateMultipartUpload;
import cn.czh.entity.UploadFile;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
public interface IStorageService {
/**
* 普通文件上传
*/
FileRecordDTO uploadFile(MultipartFile file, String md5, String objectName);
/**
* 获取文件上传进度
*/
TaskInfoDTO getUploadProgress(String identifier);
/**
* 发起分片上传
*/
TaskInfoDTO createMultipartUpload(CreateMultipartUpload req);
/**
* 合并分片文件
*/
UploadFile merge(String identifier);
/**
* 根据md5标识获取分片上传任务
*/
UploadFile getByIdentifier(String identifier);
/**
* 生成预签名上传url
*/
String genPreSignUploadUrl (String objectKey, Map<String, String> params);
/**
* 上传分片文件
*/
void uploadPart(String uploadId, Integer partNumber, MultipartFile partFile);
}
@@ -0,0 +1,37 @@
package cn.czh.service;
import cn.czh.base.BusinessException;
import cn.czh.entity.StorageConfig;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class StorageServiceFactory {
@Resource(name = "minioStorageService")
private IStorageService minioStorageService;
@Resource(name = "localStorageService")
private IStorageService localStorageService;
@Resource(name = "ossStorageService")
private IStorageService ossStorageService;
/**
* 根据存储类型获取对应的存储服务
* @param storageType 存储类型
* @return 对应的存储服务实例
*/
public IStorageService getStorageService(String storageType) {
switch (storageType) {
case StorageConfig.MINIO:
return minioStorageService;
case StorageConfig.OSS:
return ossStorageService;
case StorageConfig.LOCAL:
return localStorageService;
default:
throw new BusinessException("不支持的存储类型");
}
}
}
@@ -0,0 +1,22 @@
package cn.czh.service.impl;
import cn.czh.entity.UploadFile;
import cn.czh.mapper.UploadFileMapper;
import cn.czh.service.IFileService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class FileServiceImpl implements IFileService {
@Resource
private UploadFileMapper uploadFileMapper;
@Override
public IPage<UploadFile> pageFiles(Integer page, Integer pageSize, String storageType, String fileName) {
return uploadFileMapper.pageFiles(new Page<>(page, pageSize), storageType, fileName);
}
}
@@ -0,0 +1,278 @@
package cn.czh.service.impl;
import cn.czh.base.BusinessException;
import cn.czh.dto.FileRecordDTO;
import cn.czh.dto.MyPartSummary;
import cn.czh.dto.TaskInfoDTO;
import cn.czh.dto.TaskRecordDTO;
import cn.czh.dto.aws.LocalPartSummary;
import cn.czh.dto.req.CreateMultipartUpload;
import cn.czh.entity.StorageConfig;
import cn.czh.entity.UploadFile;
import cn.czh.mapper.UploadFileMapper;
import cn.czh.service.IStorageConfigService;
import cn.czh.service.IStorageService;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service("localStorageService")
@Slf4j
public class LocalStorageService implements IStorageService {
@Resource
private UploadFileMapper uploadFileMapper;
@Resource
private IStorageConfigService storageConfigService;
private StorageConfig storageConfig;
@PostConstruct
public void init() {
this.storageConfig = storageConfigService.getStorageConfigByType(StorageConfig.LOCAL);
// Ensure the root directory exists
File rootDir = new File(storageConfig.getBucket());
if (!rootDir.exists()) {
boolean mkdirs = rootDir.mkdirs();
if (!mkdirs) {
throw new BusinessException("无法创建根目录");
}
}
}
@Override
public FileRecordDTO uploadFile(MultipartFile file, String md5, String objectName) {
UploadFile uploadFile = getByIdentifier(md5);
if (uploadFile != null) {
FileRecordDTO fileRecord = new FileRecordDTO();
BeanUtils.copyProperties(uploadFile, fileRecord);
fileRecord.setFileId(uploadFile.getId());
fileRecord.setOriginalName(uploadFile.getFileName());
return fileRecord;
}
String filePath = generateFilePath(objectName);
File dest = new File(filePath);
if (!dest.getParentFile().exists()) {
boolean mkdirs = dest.getParentFile().mkdirs();
if (!mkdirs) {
throw new BusinessException("无法创建文件目录");
}
}
try {
file.transferTo(dest);
uploadFile = new UploadFile();
uploadFile.setUploadId(md5);
uploadFile.setFileName(file.getOriginalFilename());
uploadFile.setObjectKey(objectName);
uploadFile.setContentType(file.getContentType());
uploadFile.setAccessUrl(generateWebUrl(objectName));
uploadFile.setDownloadUrl(generateWebUrl(objectName));
uploadFile.setIsFinish(1);
uploadFile.setTotalSize(file.getSize());
uploadFile.setChunkNum(1);
uploadFile.setChunkSize(file.getSize());
uploadFile.setFileIdentifier(md5);
uploadFile.setStorageType(StorageConfig.LOCAL);
uploadFile.setCreateTime(new Date());
uploadFileMapper.insert(uploadFile);
FileRecordDTO fileRecord = new FileRecordDTO();
BeanUtils.copyProperties(uploadFile, fileRecord);
fileRecord.setFileId(uploadFile.getId());
fileRecord.setOriginalName(file.getOriginalFilename());
return fileRecord;
} catch (IOException e) {
throw new BusinessException("文件上传失败", e);
}
}
@Override
public TaskInfoDTO getUploadProgress(String identifier) {
UploadFile task = getByIdentifier(identifier);
if (task == null) {
return null;
}
TaskInfoDTO result = new TaskInfoDTO()
.setFinished(task.getIsFinish() == 1)
.setTaskRecord(TaskRecordDTO.convertFromEntity(task))
.setPath(generateWebUrl(task.getObjectKey()));
if (task.getIsFinish() == 0) {
List<MyPartSummary> uploadedParts = getUploadedParts(task.getObjectKey());
result.setFinished(false).getTaskRecord().setExitPartList(uploadedParts);
}
return result;
}
@Override
public TaskInfoDTO createMultipartUpload(CreateMultipartUpload param) {
String identifier = param.getIdentifier();
String fileName = param.getFileName();
String objectKey = generateObjectKey(param.getFolder(), fileName);
UploadFile uploadFile = new UploadFile();
int chunkNum = (int) Math.ceil(param.getTotalSize() * 1.0 / param.getChunkSize());
uploadFile.setChunkNum(chunkNum)
.setUploadId(identifier)
.setChunkSize(param.getChunkSize())
.setTotalSize(param.getTotalSize())
.setFileIdentifier(identifier)
.setFileName(fileName)
.setObjectKey(objectKey)
.setIsFinish(0)
.setStorageType(StorageConfig.LOCAL)
.setCreateTime(new Date())
.setContentType(param.getContentType());
uploadFileMapper.insert(uploadFile);
String tempDir = getTempDir(objectKey);
boolean mkdirs = new File(tempDir).mkdirs();
if (!mkdirs) {
throw new BusinessException("无法创建临时目录");
}
return new TaskInfoDTO()
.setFinished(false)
.setTaskRecord(TaskRecordDTO.convertFromEntity(uploadFile))
.setPath(generateWebUrl(objectKey));
}
@Override
public UploadFile merge(String identifier) {
UploadFile uploadFile = getByIdentifier(identifier);
if (uploadFile == null) {
throw new BusinessException("上传任务不存在");
}
String tempDir = getTempDir(uploadFile.getObjectKey());
File[] parts = new File(tempDir).listFiles();
if (parts == null || parts.length != uploadFile.getChunkNum()) {
throw new BusinessException("缺少分块");
}
String filePath = generateFilePath(uploadFile.getObjectKey());
File dest = new File(filePath);
if (!dest.getParentFile().exists()) {
boolean mkdirs = dest.getParentFile().mkdirs();
if (!mkdirs) {
throw new BusinessException("无法创建根目录");
}
}
try (FileOutputStream fos = new FileOutputStream(dest)) {
for (int i = 1; i <= uploadFile.getChunkNum(); i++) {
File partFile = new File(tempDir, String.valueOf(i));
try (FileInputStream fis = new FileInputStream(partFile)) {
IOUtils.copy(fis, fos);
}
boolean delete = partFile.delete();
if (!delete) {
log.warn("无法删除块文件: {}", partFile.getAbsolutePath());
}
}
} catch (IOException e) {
throw new BusinessException("文件合并失败", e);
}
boolean delete = new File(tempDir).delete();
if (!delete) {
log.warn("无法删除临时目录: {}", tempDir);
}
uploadFile.setAccessUrl(generateWebUrl(uploadFile.getObjectKey()));
uploadFile.setDownloadUrl(generateWebUrl(uploadFile.getObjectKey()));
uploadFile.setIsFinish(1);
uploadFileMapper.updateById(uploadFile);
return uploadFile;
}
@Override
public UploadFile getByIdentifier(String identifier) {
return uploadFileMapper.selectOne(
new LambdaQueryWrapper<UploadFile>()
.eq(UploadFile::getFileIdentifier, identifier)
);
}
@Override
public String genPreSignUploadUrl(String objectKey, Map<String, String> params) {
throw new UnsupportedOperationException("本地存储不支持预签名的URL");
}
public void uploadPart(String uploadId, Integer partNumber, MultipartFile partFile) {
UploadFile uploadFile = getByIdentifier(uploadId);
if (uploadFile == null) {
throw new BusinessException("上传任务不存在");
}
String tempDir = getTempDir(uploadFile.getObjectKey());
File partDest = new File(tempDir, partNumber.toString());
if (!partDest.getParentFile().exists()) {
boolean mkdirs = partDest.getParentFile().mkdirs();
if (!mkdirs) {
throw new BusinessException("无法创建临时目录");
}
}
try {
partFile.transferTo(partDest);
} catch (IOException e) {
throw new BusinessException("分片上传失败", e);
}
}
private String generateFilePath(String objectKey) {
return storageConfig.getBucket() + File.separator + objectKey.replace("/", File.separator);
}
private String getTempDir(String objectKey) {
return storageConfig.getBucket() + File.separator + "temp" + File.separator + objectKey.replace("/", File.separator);
}
private String generateObjectKey(String folder, String fileName) {
String suffix = fileName.substring(fileName.lastIndexOf("."));
return StrUtil.format("{}/{}/{}", folder, DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN), IdUtil.randomUUID() + suffix);
}
private List<MyPartSummary> getUploadedParts(String objectKey) {
String tempDir = getTempDir(objectKey);
File[] files = new File(tempDir).listFiles();
if (files == null) {
return Collections.emptyList();
}
return Arrays.stream(files)
.map(file -> (MyPartSummary) new LocalPartSummary(
Integer.parseInt(file.getName()), file.getName(), file.length(), new Date()
))
.collect(Collectors.toList());
}
private String generateWebUrl(String objectKey) {
return "http://localhost:10086/file/preview/local/" + objectKey;
}
}
@@ -0,0 +1,325 @@
package cn.czh.service.impl;
import cn.czh.base.BusinessException;
import cn.czh.dto.FileRecordDTO;
import cn.czh.dto.MyPartSummary;
import cn.czh.dto.TaskInfoDTO;
import cn.czh.dto.TaskRecordDTO;
import cn.czh.dto.aws.AwsPartSummaryWrapper;
import cn.czh.dto.req.CreateMultipartUpload;
import cn.czh.entity.StorageConfig;
import cn.czh.entity.UploadFile;
import cn.czh.mapper.UploadFileMapper;
import cn.czh.service.IStorageConfigService;
import cn.czh.service.IStorageService;
import cn.czh.utils.ImgUtils;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.HttpMethod;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.minio.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Service("minioStorageService")
@Slf4j
public class MinioStorageService implements IStorageService {
@Resource
private UploadFileMapper uploadFileMapper;
@Resource
private IStorageConfigService storageConfigService;
/**
* 预签名url过期时间: 10分钟
*/
private static final Integer PRE_SIGN_URL_EXPIRE = 10 * 60 * 1000;
/**
* 下载URL拼接前缀 TODO 文件下载功能
*/
private static final String DOWNLOAD_URL = "/downloadFile?objectName=%s";
/**
* 后端分片上传到Minio服务器分片大小
*/
public static final long PART_MAX_SIZE = 5 * 1024 * 1024;
private StorageConfig storageConfig;
private MinioClient minioClient;
private AmazonS3 amazonS3;
@PostConstruct
public void init() {
this.storageConfig = storageConfigService.getStorageConfigByType(StorageConfig.MINIO);
this.minioClient = MinioClient.builder()
.endpoint(storageConfig.getEndpoint())
.credentials(storageConfig.getAccessKey(), storageConfig.getSecretKey())
.build();
this.amazonS3 = minioAmazonS3Client(storageConfig);
}
private AmazonS3 minioAmazonS3Client(StorageConfig storageConfig) {
// 设置连接时的参数
ClientConfiguration config = new ClientConfiguration();
// 设置连接方式为HTTP,可选参数为HTTP和HTTPS
config.setProtocol(Protocol.HTTP);
// 设置网络访问超时时间
config.setConnectionTimeout(5000);
config.setUseExpectContinue(true);
AWSCredentials credentials = new BasicAWSCredentials(storageConfig.getAccessKey(), storageConfig.getSecretKey());
// 设置Endpoint
AwsClientBuilder.EndpointConfiguration endPoint = new AwsClientBuilder.EndpointConfiguration(storageConfig.getEndpoint(), Regions.US_EAST_1.name());
return AmazonS3ClientBuilder.standard()
.withClientConfiguration(config)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withEndpointConfiguration(endPoint)
.withPathStyleAccessEnabled(true)
.build();
}
@Override
public FileRecordDTO uploadFile(MultipartFile file, String md5, String objectName) {
UploadFile uploadFile = getByIdentifier(md5);
if (ObjectUtil.isNotNull(uploadFile)) {
FileRecordDTO fileRecord = new FileRecordDTO();
BeanUtils.copyProperties(uploadFile, fileRecord);
fileRecord.setFileId(uploadFile.getId());
fileRecord.setOriginalName(uploadFile.getFileName());
return fileRecord;
}
try {
// 检查桶是否存在,如果不存在则创建
boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(storageConfig.getBucket()).build());
if (!bucketExists) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(storageConfig.getBucket()).build());
}
String filename = file.getOriginalFilename();
PutObjectArgs putObjectArgs = getPutObjectArgs(file, objectName);
minioClient.putObject(putObjectArgs);
uploadFile = new UploadFile();
uploadFile.setUploadId(getUploadId(objectName));
uploadFile.setFileName(filename);
uploadFile.setObjectKey(objectName);
uploadFile.setContentType(file.getContentType());
uploadFile.setAccessUrl(getFileAccessUrl(objectName));
uploadFile.setDownloadUrl(getFileDownloadUrl(objectName));
uploadFile.setCreateTime(new Date());
uploadFile.setIsFinish(1);
uploadFile.setTotalSize(file.getSize());
uploadFile.setChunkNum(1);
uploadFile.setChunkSize(file.getSize());
uploadFile.setFileIdentifier(md5);
uploadFile.setStorageType(StorageConfig.MINIO);
uploadFileMapper.insert(uploadFile);
FileRecordDTO fileRecord = new FileRecordDTO();
BeanUtils.copyProperties(uploadFile, fileRecord);
fileRecord.setFileId(uploadFile.getId());
fileRecord.setOriginalName(filename);
return fileRecord;
} catch (Exception e) {
throw new BusinessException("文件上上传失败", e);
}
}
@Override
public TaskInfoDTO getUploadProgress(String identifier) {
UploadFile task = getByIdentifier(identifier);
if (task == null) {
return null;
}
TaskInfoDTO result = new TaskInfoDTO()
.setFinished(true)
.setTaskRecord(TaskRecordDTO.convertFromEntity(task))
.setPath(getPath(task.getObjectKey()));
boolean doesObjectExist = amazonS3.doesObjectExist(storageConfig.getBucket(), task.getObjectKey());
if (!doesObjectExist) {
// 未上传完,返回已上传的分片
ListPartsRequest listPartsRequest = new ListPartsRequest(storageConfig.getBucket(), task.getObjectKey(), task.getUploadId());
PartListing partListing = amazonS3.listParts(listPartsRequest);
List<MyPartSummary> awsPartSummary = partListing.getParts().stream().map(AwsPartSummaryWrapper::new).collect(Collectors.toList());
result.setFinished(false).getTaskRecord().setExitPartList(awsPartSummary);
}
return result;
}
@Override
public TaskInfoDTO createMultipartUpload(CreateMultipartUpload param) {
Date currentDate = new Date();
String fileName = param.getFileName();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
String key = StrUtil.format("{}/{}/{}.{}", param.getFolder(), DateUtil.format(currentDate, DatePattern.PURE_DATE_PATTERN), IdUtil.randomUUID(), suffix);
UploadFile task = new UploadFile();
int chunkNum = (int) Math.ceil(param.getTotalSize() * 1.0 / param.getChunkSize());
task.setChunkNum(chunkNum)
.setChunkSize(param.getChunkSize())
.setTotalSize(param.getTotalSize())
.setFileIdentifier(param.getIdentifier())
.setFileName(fileName)
.setObjectKey(key)
.setIsFinish(0)
.setCreateTime(currentDate)
.setContentType(param.getContentType())
.setStorageType(StorageConfig.MINIO)
.setUploadId(getUploadId(key));
uploadFileMapper.insert(task);
return new TaskInfoDTO().setFinished(false).setTaskRecord(TaskRecordDTO.convertFromEntity(task)).setPath(getPath(key));
}
@Override
public UploadFile merge(String identifier) {
UploadFile uploadFile = getByIdentifier(identifier);
if (uploadFile == null) {
throw new BusinessException("上传任务不存在");
}
ListPartsRequest listPartsRequest = new ListPartsRequest(storageConfig.getBucket(), uploadFile.getObjectKey(), uploadFile.getUploadId());
PartListing partListing = amazonS3.listParts(listPartsRequest);
List<PartSummary> parts = partListing.getParts();
if (!uploadFile.getChunkNum().equals(parts.size())) {
// 已上传分块数量与记录中的数量不对应,不能合并分块
throw new BusinessException("分片缺失");
}
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest()
.withUploadId(uploadFile.getUploadId())
.withKey(uploadFile.getObjectKey())
.withBucketName(storageConfig.getBucket())
.withPartETags(parts.stream().map(partSummary -> new PartETag(partSummary.getPartNumber(), partSummary.getETag())).collect(Collectors.toList()));
amazonS3.completeMultipartUpload(completeMultipartUploadRequest);
// 更新数据状态
uploadFile.setAccessUrl(getFileAccessUrl(uploadFile.getObjectKey()));
uploadFile.setDownloadUrl(getFileDownloadUrl(uploadFile.getObjectKey()));
uploadFile.setIsFinish(1);
uploadFileMapper.updateById(uploadFile);
return uploadFile;
}
@Override
public UploadFile getByIdentifier(String identifier) {
return uploadFileMapper.selectOne(new LambdaQueryWrapper<UploadFile>().eq(UploadFile::getFileIdentifier, identifier));
}
@Override
public String genPreSignUploadUrl(String objectKey, Map<String, String> params) {
Date currentDate = new Date();
Date expireDate = DateUtil.offsetMillisecond(currentDate, PRE_SIGN_URL_EXPIRE);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(storageConfig.getBucket(), objectKey)
.withExpiration(expireDate).withMethod(HttpMethod.PUT);
if (params != null) {
params.forEach(request::addRequestParameter);
}
URL preSignedUrl = amazonS3.generatePresignedUrl(request);
return preSignedUrl.toString();
}
@Override
public void uploadPart(String uploadId, Integer partNumber, MultipartFile partFile) {
// MinIO 通过前端使用预签名 URL 直接上传分片到 MinIO,服务器端无需处理
}
/**
* 获取文件路径
*/
private String getPath(String objectKey) {
return StrUtil.format("{}/{}/{}", storageConfig.getEndpoint(), storageConfig.getBucket(), objectKey);
}
private String getUploadId(String key) {
String contentType = MediaTypeFactory.getMediaType(key).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(contentType);
InitiateMultipartUploadResult initiateMultipartUploadResult = amazonS3
.initiateMultipartUpload(new InitiateMultipartUploadRequest(storageConfig.getBucket(), key).withObjectMetadata(objectMetadata));
return initiateMultipartUploadResult.getUploadId();
}
@Scheduled(fixedRate = 60000) // 每分钟检查一次
public void refreshStorageConfig() {
StorageConfig newConfig = storageConfigService.getStorageConfigByType(StorageConfig.MINIO);
if (!newConfig.equals(this.storageConfig)) {
this.storageConfig = newConfig;
this.minioClient = MinioClient.builder()
.endpoint(newConfig.getEndpoint())
.credentials(newConfig.getAccessKey(), newConfig.getSecretKey())
.build();
log.info("StorageConfig refreshed successfully");
}
}
/**
* 获取文件访问URL
*/
private String getFileAccessUrl(String objectName) {
return storageConfig.getEndpoint() + "/" + storageConfig.getBucket() + "/" + objectName;
}
/**
* 获取文件下载URL
*/
private String getFileDownloadUrl(String objectName) {
return String.format(DOWNLOAD_URL, objectName);
}
/**
* 设置文件对象
*/
private PutObjectArgs getPutObjectArgs(MultipartFile file, String objectName) throws IOException {
InputStream stream = file.getInputStream();
String fileContentType = ImgUtils.getFileContentType(objectName);
return PutObjectArgs.builder()
.object(objectName)
.bucket(storageConfig.getBucket())
.contentType(Objects.isNull(fileContentType) ? file.getContentType() : fileContentType)
.stream(stream, stream.available(), PART_MAX_SIZE).build();
}
private PutObjectArgs getPutObjectArgs(ByteArrayInputStream bais, String objectName) {
return PutObjectArgs.builder()
.bucket(storageConfig.getBucket())
.object(objectName)
.stream(bais, bais.available(), -1)
.contentType("image/jpeg")
.build();
}
}
@@ -0,0 +1,306 @@
package cn.czh.service.impl;
import cn.czh.base.BusinessException;
import cn.czh.dto.FileRecordDTO;
import cn.czh.dto.MyPartSummary;
import cn.czh.dto.TaskInfoDTO;
import cn.czh.dto.TaskRecordDTO;
import cn.czh.dto.aws.OssPartSummaryWrapper;
import cn.czh.dto.req.CreateMultipartUpload;
import cn.czh.entity.StorageConfig;
import cn.czh.entity.UploadFile;
import cn.czh.mapper.UploadFileMapper;
import cn.czh.service.IStorageConfigService;
import cn.czh.service.IStorageService;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 阿里云 OSS 文件上传服务
*/
@Service("ossStorageService")
@Slf4j
public class OssStorageService implements IStorageService {
@Resource
private UploadFileMapper uploadFileMapper;
@Resource
private IStorageConfigService storageConfigService;
private StorageConfig storageConfig;
private OSS ossClient;
/** 预签名 URL 过期时间:10 分钟 */
private static final long PRE_SIGN_URL_EXPIRE = 10 * 60 * 1000;
@PostConstruct
public void init() {
// 获取 OSS 配置
this.storageConfig = storageConfigService.getStorageConfigByType(StorageConfig.OSS);
// 初始化 OSS 客户端
this.ossClient = new OSSClientBuilder().build(
storageConfig.getEndpoint(),
storageConfig.getAccessKey(),
storageConfig.getSecretKey()
);
}
/**
* 上传单个文件
*/
@Override
public FileRecordDTO uploadFile(MultipartFile file, String md5, String objectName) {
UploadFile uploadFile = getByIdentifier(md5);
if (uploadFile != null) {
FileRecordDTO fileRecord = new FileRecordDTO();
BeanUtils.copyProperties(uploadFile, fileRecord);
fileRecord.setFileId(uploadFile.getId());
fileRecord.setOriginalName(uploadFile.getFileName());
return fileRecord;
}
try {
InputStream inputStream = file.getInputStream();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());
// 上传文件到 OSS
PutObjectRequest putObjectRequest = new PutObjectRequest(
storageConfig.getBucket(),
objectName,
inputStream,
metadata
);
ossClient.putObject(putObjectRequest);
// 记录上传信息到数据库
uploadFile = new UploadFile();
uploadFile.setUploadId(md5);
uploadFile.setFileName(file.getOriginalFilename());
uploadFile.setObjectKey(objectName);
uploadFile.setContentType(file.getContentType());
uploadFile.setAccessUrl(getFileAccessUrl(objectName));
uploadFile.setDownloadUrl(getFileDownloadUrl(objectName));
uploadFile.setIsFinish(1);
uploadFile.setTotalSize(file.getSize());
uploadFile.setChunkNum(1);
uploadFile.setChunkSize(file.getSize());
uploadFile.setFileIdentifier(md5);
uploadFile.setCreateTime(new Date());
uploadFile.setStorageType(StorageConfig.OSS);
uploadFileMapper.insert(uploadFile);
FileRecordDTO fileRecord = new FileRecordDTO();
BeanUtils.copyProperties(uploadFile, fileRecord);
fileRecord.setFileId(uploadFile.getId());
fileRecord.setOriginalName(file.getOriginalFilename());
return fileRecord;
} catch (IOException e) {
throw new BusinessException("文件上传失败", e);
}
}
/**
* 获取上传进度
*/
@Override
public TaskInfoDTO getUploadProgress(String identifier) {
UploadFile task = getByIdentifier(identifier);
if (task == null) {
return null;
}
TaskInfoDTO result = new TaskInfoDTO()
.setFinished(task.getIsFinish() == 1)
.setTaskRecord(TaskRecordDTO.convertFromEntity(task))
.setPath(getPath(task.getObjectKey()));
if (task.getIsFinish() == 0) {
// 获取已上传的分片
ListPartsRequest listPartsRequest = new ListPartsRequest(
storageConfig.getBucket(),
task.getObjectKey(),
task.getUploadId()
);
PartListing partListing = ossClient.listParts(listPartsRequest);
List<MyPartSummary> partList = partListing.getParts().stream()
.map(OssPartSummaryWrapper::new)
.collect(Collectors.toList());
result.setFinished(false).getTaskRecord().setExitPartList(partList);
}
return result;
}
/**
* 创建分片上传任务
*/
@Override
public TaskInfoDTO createMultipartUpload(CreateMultipartUpload param) {
String identifier = param.getIdentifier();
String fileName = param.getFileName();
String objectKey = generateObjectKey(param.getFolder(), fileName);
// 初始化分片上传
InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(
storageConfig.getBucket(),
objectKey
);
InitiateMultipartUploadResult initiateResult = ossClient.initiateMultipartUpload(initiateRequest);
String uploadId = initiateResult.getUploadId();
// 记录任务信息
UploadFile task = new UploadFile();
int chunkNum = (int) Math.ceil(param.getTotalSize() * 1.0 / param.getChunkSize());
task.setChunkNum(chunkNum)
.setUploadId(uploadId)
.setChunkSize(param.getChunkSize())
.setTotalSize(param.getTotalSize())
.setFileIdentifier(identifier)
.setFileName(fileName)
.setObjectKey(objectKey)
.setIsFinish(0)
.setStorageType(StorageConfig.OSS)
.setCreateTime(new Date())
.setContentType(param.getContentType());
uploadFileMapper.insert(task);
return new TaskInfoDTO()
.setFinished(false)
.setTaskRecord(TaskRecordDTO.convertFromEntity(task))
.setPath(getPath(objectKey));
}
/**
* 合并分片
*/
@Override
public UploadFile merge(String identifier) {
UploadFile uploadFile = getByIdentifier(identifier);
if (uploadFile == null) {
throw new BusinessException("上传任务不存在");
}
// 检查分片是否完整
ListPartsRequest listPartsRequest = new ListPartsRequest(
storageConfig.getBucket(),
uploadFile.getObjectKey(),
uploadFile.getUploadId()
);
PartListing partListing = ossClient.listParts(listPartsRequest);
List<PartSummary> parts = partListing.getParts();
if (parts.size() != uploadFile.getChunkNum()) {
throw new BusinessException("分片缺失");
}
// 合并分片
CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(
storageConfig.getBucket(),
uploadFile.getObjectKey(),
uploadFile.getUploadId(),
parts.stream().map(part -> new PartETag(part.getPartNumber(), part.getETag())).collect(Collectors.toList())
);
ossClient.completeMultipartUpload(completeRequest);
// 更新任务状态
uploadFile.setAccessUrl(getFileAccessUrl(uploadFile.getObjectKey()));
uploadFile.setDownloadUrl(getFileDownloadUrl(uploadFile.getObjectKey()));
uploadFile.setIsFinish(1);
uploadFileMapper.updateById(uploadFile);
return uploadFile;
}
/**
* 根据标识符查询上传任务
*/
@Override
public UploadFile getByIdentifier(String identifier) {
return uploadFileMapper.selectOne(
new LambdaQueryWrapper<UploadFile>()
.eq(UploadFile::getFileIdentifier, identifier)
);
}
/**
* 生成预签名上传 URL
*/
@Override
public String genPreSignUploadUrl(String objectKey, Map<String, String> params) {
Date expiration = new Date(new Date().getTime() + PRE_SIGN_URL_EXPIRE);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(
storageConfig.getBucket(),
objectKey
);
// 设置 HTTP 方法为 PUT
request.setMethod(com.aliyun.oss.HttpMethod.PUT);
// 设置过期时间
request.setExpiration(expiration);
request.setContentType("application/octet-stream");
if (params != null) {
params.forEach(request::addQueryParameter);
}
// 生成预签名 URL
URL url = ossClient.generatePresignedUrl(request);
return url.toString();
}
/**
* 上传分片(OSS 使用预签名 URL 上传,服务器端无需实现)
*/
@Override
public void uploadPart(String uploadId, Integer partNumber, MultipartFile partFile) {
// OSS 通过前端使用预签名 URL 直接上传分片到 OSS,服务器端无需处理
}
/**
* 生成对象键(Object Key
*/
private String generateObjectKey(String folder, String fileName) {
String suffix = fileName.substring(fileName.lastIndexOf("."));
return StrUtil.format("{}/{}/{}", folder, DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN), IdUtil.randomUUID() + suffix);
}
/**
* 获取文件路径
*/
private String getPath(String objectKey) {
return StrUtil.format("{}/{}/{}", storageConfig.getEndpoint(), storageConfig.getBucket(), objectKey);
}
/**
* 获取文件访问 URL
*/
private String getFileAccessUrl(String objectName) {
// 返回预签名 URL,过期时间为 1 小时
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
return ossClient.generatePresignedUrl(storageConfig.getBucket(), objectName, expiration).toString();
}
/**
* 获取文件下载 URL
*/
private String getFileDownloadUrl(String objectName) {
// 自定义下载路径,可根据实际需求调整
return "/downloadFile?objectName=" + objectName;
}
}
@@ -0,0 +1,30 @@
package cn.czh.service.impl;
import cn.czh.base.BusinessException;
import cn.czh.entity.StorageConfig;
import cn.czh.mapper.StorageConfigMapper;
import cn.czh.service.IStorageConfigService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class StorageConfigServiceImpl extends ServiceImpl<StorageConfigMapper, StorageConfig> implements IStorageConfigService {
@Override
public StorageConfig getStorageConfigByType(String type) {
LambdaQueryWrapper<StorageConfig> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(StorageConfig::getType, type);
StorageConfig config = getOne(queryWrapper);
if (config == null) {
throw new BusinessException("未配置存储配置");
}
if (!StorageConfig.LOCAL.equals(config.getType())) {
if (StrUtil.isBlank(config.getSecretKey()) || StrUtil.isBlank(config.getAccessKey()) || StrUtil.isBlank(config.getEndpoint())) {
throw new BusinessException("存储配置错误");
}
}
return config;
}
}
@@ -0,0 +1,161 @@
package cn.czh.utils;
/**
* 文件类型的工具类
*/
public class FileTypeUtil {
/**
* 图片类型
*/
public final static String[] IMG_FILE_TYPE = { "jpg", "bmp", "png", "gif" ,"jpeg"};
/**
* 视频类型
*/
public final static String[] VIDEO_FILE_TYPE = { "mp4", "gmf", "wmv", "avi" };
/**
* 日志文件
*/
public final static String[] LOG_FILE_TYPE = { "log", "txt", "doc", "docx" };
/**
* 音频文件
*/
public final static String[] AUDIO_FILE_TYPE = { "wav", "mp3" };
/**
* 图片文件
*/
public final static int IMG_FILE = 1;
/**
* 视频文件
*/
public final static int VIDEO_FILE = 2;
/**
* 日志文件
*/
public final static int LOG_FILE = 3;
/**
* 音频文件
*/
public final static int AUDIO_FILE = 4;
/**
* 通过文件类型判断文件是否符合所属类型
* @param fileType 文件类型 为FileTypeUtil中的 IMG_FILE ,VIDEO_FILE,VIDEO_FILE
*/
public static String isFileTypeExists(int fileType, String fileName) {
switch (fileType) {
case LOG_FILE:
if (bLogFileType(fileName)) {
return null;
} else {
return "日志类型不正确!";
}
case IMG_FILE:
if (bImgFileType(fileName)) {
return null;
} else {
return "图片类型不正确!";
}
case VIDEO_FILE:
if (bVideoFileType(fileName)) {
return null;
} else {
return "视频类型不正确!";
}
case AUDIO_FILE:
if (bAudioFileType(fileName)) {
return null;
} else {
return "视频类型不正确!";
}
default:
return "没有指定文件类型";
}
}
/**
* 检查是否日志文件
*/
public static boolean bLogFileType(String fileName) {
String fileSuffix = getFileSuffix(fileName);
for (String s : LOG_FILE_TYPE) {
if (s.equals(fileSuffix)) {
return true;
}
}
return false;
}
/**
* 检查是否音频文件
*/
public static boolean bAudioFileType(String fileName) {
String fileSuffix = getFileSuffix(fileName);
for (String s : AUDIO_FILE_TYPE) {
if (s.equals(fileSuffix)) {
return true;
}
}
return false;
}
/**
* 判断文件名是否为图片类型
*/
public static boolean bImgFileType(String fileName) {
String fileSuffix = getFileSuffix(fileName);
return isImgType(fileSuffix);
}
/**
* 视频格式
*/
public static boolean bVideoFileType(String fileName) {
String fileSuffix = getFileSuffix(fileName);
for (String s : VIDEO_FILE_TYPE) {
if (s.equals(fileSuffix)) {
return true;
}
}
return false;
}
/**
* 获取文件后缀
*/
public static String getFileSuffix(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
/**
* 获取文件名
*/
public static String getFileName(String fileName) {
int dot = fileName.lastIndexOf('.');
String filename = null;
if (dot > -1) {
filename = fileName.substring(0, dot);
}
return filename;
}
/**
* 判断文件名是否为图片类型
*/
public static boolean isImgType(String fileSuffix) {
for (String s : IMG_FILE_TYPE) {
if (s.equals(fileSuffix)) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,710 @@
package cn.czh.utils;
import com.google.common.collect.Maps;
import java.util.Map;
public class ImgUtils {
private static final Map<String, String> contentType = Maps.newHashMap();
static {
contentType.put("load" , "text/html");
contentType.put("123" , "application/vnd.lotus-1-2-3");
contentType.put("3ds" , "image/x-3ds");
contentType.put("3g2" , "video/3gpp");
contentType.put("3ga" , "video/3gpp");
contentType.put("3gp" , "video/3gpp");
contentType.put("3gpp" , "video/3gpp");
contentType.put("602" , "application/x-t602");
contentType.put("669" , "audio/x-mod");
contentType.put("7z" , "application/x-7z-compressed");
contentType.put("a" , "application/x-archive");
contentType.put("aac" , "audio/mp4");
contentType.put("abw" , "application/x-abiword");
contentType.put("abw.crashed" , "application/x-abiword");
contentType.put("abw.gz" , "application/x-abiword");
contentType.put("ac3" , "audio/ac3");
contentType.put("ace" , "application/x-ace");
contentType.put("adb" , "text/x-adasrc");
contentType.put("ads" , "text/x-adasrc");
contentType.put("afm" , "application/x-font-afm");
contentType.put("ag" , "image/x-applix-graphics");
contentType.put("ai" , "application/illustrator");
contentType.put("aif" , "audio/x-aiff");
contentType.put("aifc" , "audio/x-aiff");
contentType.put("aiff" , "audio/x-aiff");
contentType.put("al" , "application/x-perl");
contentType.put("alz" , "application/x-alz");
contentType.put("amr" , "audio/amr");
contentType.put("ani" , "application/x-navi-animation");
contentType.put("anim[1-9j]" , "video/x-anim");
contentType.put("anx" , "application/annodex");
contentType.put("ape" , "audio/x-ape");
contentType.put("arj" , "application/x-arj");
contentType.put("arw" , "image/x-sony-arw");
contentType.put("as" , "application/x-applix-spreadsheet");
contentType.put("asc" , "text/plain");
contentType.put("asf" , "video/x-ms-asf");
contentType.put("asp" , "application/x-asp");
contentType.put("ass" , "text/x-ssa");
contentType.put("asx" , "audio/x-ms-asx");
contentType.put("atom" , "application/atom+xml");
contentType.put("au" , "audio/basic");
contentType.put("avi" , "video/x-msvideo");
contentType.put("aw" , "application/x-applix-word");
contentType.put("awb" , "audio/amr-wb");
contentType.put("awk" , "application/x-awk");
contentType.put("axa" , "audio/annodex");
contentType.put("axv" , "video/annodex");
contentType.put("bak" , "application/x-trash");
contentType.put("bcpio" , "application/x-bcpio");
contentType.put("bdf" , "application/x-font-bdf");
contentType.put("bib" , "text/x-bibtex");
contentType.put("bin" , "application/octet-stream");
contentType.put("blend" , "application/x-blender");
contentType.put("blender" , "application/x-blender");
contentType.put("bmp" , "image/bmp");
contentType.put("bz" , "application/x-bzip");
contentType.put("bz2" , "application/x-bzip");
contentType.put("c" , "text/x-csrc");
contentType.put("c++" , "text/x-c++src");
contentType.put("cab" , "application/vnd.ms-cab-compressed");
contentType.put("cb7" , "application/x-cb7");
contentType.put("cbr" , "application/x-cbr");
contentType.put("cbt" , "application/x-cbt");
contentType.put("cbz" , "application/x-cbz");
contentType.put("cc" , "text/x-c++src");
contentType.put("cdf" , "application/x-netcdf");
contentType.put("cdr" , "application/vnd.corel-draw");
contentType.put("cer" , "application/x-x509-ca-cert");
contentType.put("cert" , "application/x-x509-ca-cert");
contentType.put("cgm" , "image/cgm");
contentType.put("chm" , "application/x-chm");
contentType.put("chrt" , "application/x-kchart");
contentType.put("class" , "application/x-java");
contentType.put("cls" , "text/x-tex");
contentType.put("cmake" , "text/x-cmake");
contentType.put("cpio" , "application/x-cpio");
contentType.put("cpio.gz" , "application/x-cpio-compressed");
contentType.put("cpp" , "text/x-c++src");
contentType.put("cr2" , "image/x-canon-cr2");
contentType.put("crt" , "application/x-x509-ca-cert");
contentType.put("crw" , "image/x-canon-crw");
contentType.put("cs" , "text/x-csharp");
contentType.put("csh" , "application/x-csh");
contentType.put("css" , "text/css");
contentType.put("cssl" , "text/css");
contentType.put("csv" , "text/csv");
contentType.put("cue" , "application/x-cue");
contentType.put("cur" , "image/x-win-bitmap");
contentType.put("cxx" , "text/x-c++src");
contentType.put("d" , "text/x-dsrc");
contentType.put("dar" , "application/x-dar");
contentType.put("dbf" , "application/x-dbf");
contentType.put("dc" , "application/x-dc-rom");
contentType.put("dcl" , "text/x-dcl");
contentType.put("dcm" , "application/dicom");
contentType.put("dcr" , "image/x-kodak-dcr");
contentType.put("dds" , "image/x-dds");
contentType.put("deb" , "application/x-deb");
contentType.put("der" , "application/x-x509-ca-cert");
contentType.put("desktop" , "application/x-desktop");
contentType.put("dia" , "application/x-dia-diagram");
contentType.put("diff" , "text/x-patch");
contentType.put("divx" , "video/x-msvideo");
contentType.put("djv" , "image/vnd.djvu");
contentType.put("djvu" , "image/vnd.djvu");
contentType.put("dng" , "image/x-adobe-dng");
contentType.put("doc" , "application/msword");
contentType.put("docbook" , "application/docbook+xml");
contentType.put("docm" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
contentType.put("docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
contentType.put("dot" , "text/vnd.graphviz");
contentType.put("dsl" , "text/x-dsl");
contentType.put("dtd" , "application/xml-dtd");
contentType.put("dtx" , "text/x-tex");
contentType.put("dv" , "video/dv");
contentType.put("dvi" , "application/x-dvi");
contentType.put("dvi.bz2" , "application/x-bzdvi");
contentType.put("dvi.gz" , "application/x-gzdvi");
contentType.put("dwg" , "image/vnd.dwg");
contentType.put("dxf" , "image/vnd.dxf");
contentType.put("e" , "text/x-eiffel");
contentType.put("egon" , "application/x-egon");
contentType.put("eif" , "text/x-eiffel");
contentType.put("el" , "text/x-emacs-lisp");
contentType.put("emf" , "image/x-emf");
contentType.put("emp" , "application/vnd.emusic-emusic_package");
contentType.put("ent" , "application/xml-external-parsed-entity");
contentType.put("eps" , "image/x-eps");
contentType.put("eps.bz2" , "image/x-bzeps");
contentType.put("eps.gz" , "image/x-gzeps");
contentType.put("epsf" , "image/x-eps");
contentType.put("epsf.bz2" , "image/x-bzeps");
contentType.put("epsf.gz" , "image/x-gzeps");
contentType.put("epsi" , "image/x-eps");
contentType.put("epsi.bz2" , "image/x-bzeps");
contentType.put("epsi.gz" , "image/x-gzeps");
contentType.put("epub" , "application/epub+zip");
contentType.put("erl" , "text/x-erlang");
contentType.put("es" , "application/ecmascript");
contentType.put("etheme" , "application/x-e-theme");
contentType.put("etx" , "text/x-setext");
contentType.put("exe" , "application/x-ms-dos-executable");
contentType.put("exr" , "image/x-exr");
contentType.put("ez" , "application/andrew-inset");
contentType.put("f" , "text/x-fortran");
contentType.put("f90" , "text/x-fortran");
contentType.put("f95" , "text/x-fortran");
contentType.put("fb2" , "application/x-fictionbook+xml");
contentType.put("fig" , "image/x-xfig");
contentType.put("fits" , "image/fits");
contentType.put("fl" , "application/x-fluid");
contentType.put("flac" , "audio/x-flac");
contentType.put("flc" , "video/x-flic");
contentType.put("fli" , "video/x-flic");
contentType.put("flv" , "video/x-flv");
contentType.put("flw" , "application/x-kivio");
contentType.put("fo" , "text/x-xslfo");
contentType.put("for" , "text/x-fortran");
contentType.put("g3" , "image/fax-g3");
contentType.put("gb" , "application/x-gameboy-rom");
contentType.put("gba" , "application/x-gba-rom");
contentType.put("gcrd" , "text/directory");
contentType.put("ged" , "application/x-gedcom");
contentType.put("gedcom" , "application/x-gedcom");
contentType.put("gen" , "application/x-genesis-rom");
contentType.put("gf" , "application/x-tex-gf");
contentType.put("gg" , "application/x-sms-rom");
contentType.put("gif" , "image/gif");
contentType.put("glade" , "application/x-glade");
contentType.put("gmo" , "application/x-gettext-translation");
contentType.put("gnc" , "application/x-gnucash");
contentType.put("gnd" , "application/gnunet-directory");
contentType.put("gnucash" , "application/x-gnucash");
contentType.put("gnumeric" , "application/x-gnumeric");
contentType.put("gnuplot" , "application/x-gnuplot");
contentType.put("gp" , "application/x-gnuplot");
contentType.put("gpg" , "application/pgp-encrypted");
contentType.put("gplt" , "application/x-gnuplot");
contentType.put("gra" , "application/x-graphite");
contentType.put("gsf" , "application/x-font-type1");
contentType.put("gsm" , "audio/x-gsm");
contentType.put("gtar" , "application/x-tar");
contentType.put("gv" , "text/vnd.graphviz");
contentType.put("gvp" , "text/x-google-video-pointer");
contentType.put("gz" , "application/x-gzip");
contentType.put("h" , "text/x-chdr");
contentType.put("h++" , "text/x-c++hdr");
contentType.put("hdf" , "application/x-hdf");
contentType.put("hh" , "text/x-c++hdr");
contentType.put("hp" , "text/x-c++hdr");
contentType.put("hpgl" , "application/vnd.hp-hpgl");
contentType.put("hpp" , "text/x-c++hdr");
contentType.put("hs" , "text/x-haskell");
contentType.put("htm" , "text/html");
contentType.put("html" , "text/html");
contentType.put("hwp" , "application/x-hwp");
contentType.put("hwt" , "application/x-hwt");
contentType.put("hxx" , "text/x-c++hdr");
contentType.put("ica" , "application/x-ica");
contentType.put("icb" , "image/x-tga");
contentType.put("icns" , "image/x-icns");
contentType.put("ico" , "image/vnd.microsoft.icon");
contentType.put("ics" , "text/calendar");
contentType.put("idl" , "text/x-idl");
contentType.put("ief" , "image/ief");
contentType.put("iff" , "image/x-iff");
contentType.put("ilbm" , "image/x-ilbm");
contentType.put("ime" , "text/x-imelody");
contentType.put("imy" , "text/x-imelody");
contentType.put("ins" , "text/x-tex");
contentType.put("iptables" , "text/x-iptables");
contentType.put("iso" , "application/x-cd-image");
contentType.put("iso9660" , "application/x-cd-image");
contentType.put("it" , "audio/x-it");
contentType.put("j2k" , "image/jp2");
contentType.put("jad" , "text/vnd.sun.j2me.app-descriptor");
contentType.put("jar" , "application/x-java-archive");
contentType.put("java" , "text/x-java");
contentType.put("jng" , "image/x-jng");
contentType.put("jnlp" , "application/x-java-jnlp-file");
contentType.put("jp2" , "image/jp2");
contentType.put("jpc" , "image/jp2");
contentType.put("jpe" , "image/jpeg");
contentType.put("jpeg" , "image/jpeg");
contentType.put("jpf" , "image/jp2");
contentType.put("jpg" , "image/jpeg");
contentType.put("jpr" , "application/x-jbuilder-project");
contentType.put("jpx" , "image/jp2");
contentType.put("js" , "application/javascript");
contentType.put("json" , "application/json");
contentType.put("jsonp" , "application/jsonp");
contentType.put("k25" , "image/x-kodak-k25");
contentType.put("kar" , "audio/midi");
contentType.put("karbon" , "application/x-karbon");
contentType.put("kdc" , "image/x-kodak-kdc");
contentType.put("kdelnk" , "application/x-desktop");
contentType.put("kexi" , "application/x-kexiproject-sqlite3");
contentType.put("kexic" , "application/x-kexi-connectiondata");
contentType.put("kexis" , "application/x-kexiproject-shortcut");
contentType.put("kfo" , "application/x-kformula");
contentType.put("kil" , "application/x-killustrator");
contentType.put("kino" , "application/smil");
contentType.put("kml" , "application/vnd.google-earth.kml+xml");
contentType.put("kmz" , "application/vnd.google-earth.kmz");
contentType.put("kon" , "application/x-kontour");
contentType.put("kpm" , "application/x-kpovmodeler");
contentType.put("kpr" , "application/x-kpresenter");
contentType.put("kpt" , "application/x-kpresenter");
contentType.put("kra" , "application/x-krita");
contentType.put("ksp" , "application/x-kspread");
contentType.put("kud" , "application/x-kugar");
contentType.put("kwd" , "application/x-kword");
contentType.put("kwt" , "application/x-kword");
contentType.put("la" , "application/x-shared-library-la");
contentType.put("latex" , "text/x-tex");
contentType.put("ldif" , "text/x-ldif");
contentType.put("lha" , "application/x-lha");
contentType.put("lhs" , "text/x-literate-haskell");
contentType.put("lhz" , "application/x-lhz");
contentType.put("log" , "text/x-log");
contentType.put("ltx" , "text/x-tex");
contentType.put("lua" , "text/x-lua");
contentType.put("lwo" , "image/x-lwo");
contentType.put("lwob" , "image/x-lwo");
contentType.put("lws" , "image/x-lws");
contentType.put("ly" , "text/x-lilypond");
contentType.put("lyx" , "application/x-lyx");
contentType.put("lz" , "application/x-lzip");
contentType.put("lzh" , "application/x-lha");
contentType.put("lzma" , "application/x-lzma");
contentType.put("lzo" , "application/x-lzop");
contentType.put("m" , "text/x-matlab");
contentType.put("m15" , "audio/x-mod");
contentType.put("m2t" , "video/mpeg");
contentType.put("m3u" , "audio/x-mpegurl");
contentType.put("m3u8" , "audio/x-mpegurl");
contentType.put("m4" , "application/x-m4");
contentType.put("m4a" , "audio/mp4");
contentType.put("m4b" , "audio/x-m4b");
contentType.put("m4v" , "video/mp4");
contentType.put("mab" , "application/x-markaby");
contentType.put("man" , "application/x-troff-man");
contentType.put("mbox" , "application/mbox");
contentType.put("md" , "application/x-genesis-rom");
contentType.put("mdb" , "application/vnd.ms-access");
contentType.put("mdi" , "image/vnd.ms-modi");
contentType.put("me" , "text/x-troff-me");
contentType.put("med" , "audio/x-mod");
contentType.put("metalink" , "application/metalink+xml");
contentType.put("mgp" , "application/x-magicpoint");
contentType.put("mid" , "audio/midi");
contentType.put("midi" , "audio/midi");
contentType.put("mif" , "application/x-mif");
contentType.put("minipsf" , "audio/x-minipsf");
contentType.put("mka" , "audio/x-matroska");
contentType.put("mkv" , "video/x-matroska");
contentType.put("ml" , "text/x-ocaml");
contentType.put("mli" , "text/x-ocaml");
contentType.put("mm" , "text/x-troff-mm");
contentType.put("mmf" , "application/x-smaf");
contentType.put("mml" , "text/mathml");
contentType.put("mng" , "video/x-mng");
contentType.put("mo" , "application/x-gettext-translation");
contentType.put("mo3" , "audio/x-mo3");
contentType.put("moc" , "text/x-moc");
contentType.put("mod" , "audio/x-mod");
contentType.put("mof" , "text/x-mof");
contentType.put("moov" , "video/quicktime");
contentType.put("mov" , "video/quicktime");
contentType.put("movie" , "video/x-sgi-movie");
contentType.put("mp+" , "audio/x-musepack");
contentType.put("mp2" , "video/mpeg");
contentType.put("mp3" , "audio/mpeg");
contentType.put("mp4" , "video/mp4");
contentType.put("mpc" , "audio/x-musepack");
contentType.put("mpe" , "video/mpeg");
contentType.put("mpeg" , "video/mpeg");
contentType.put("mpg" , "video/mpeg");
contentType.put("mpga" , "audio/mpeg");
contentType.put("mpp" , "audio/x-musepack");
contentType.put("mrl" , "text/x-mrml");
contentType.put("mrml" , "text/x-mrml");
contentType.put("mrw" , "image/x-minolta-mrw");
contentType.put("ms" , "text/x-troff-ms");
contentType.put("msi" , "application/x-msi");
contentType.put("msod" , "image/x-msod");
contentType.put("msx" , "application/x-msx-rom");
contentType.put("mtm" , "audio/x-mod");
contentType.put("mup" , "text/x-mup");
contentType.put("mxf" , "application/mxf");
contentType.put("n64" , "application/x-n64-rom");
contentType.put("nb" , "application/mathematica");
contentType.put("nc" , "application/x-netcdf");
contentType.put("nds" , "application/x-nintendo-ds-rom");
contentType.put("nef" , "image/x-nikon-nef");
contentType.put("nes" , "application/x-nes-rom");
contentType.put("nfo" , "text/x-nfo");
contentType.put("not" , "text/x-mup");
contentType.put("nsc" , "application/x-netshow-channel");
contentType.put("nsv" , "video/x-nsv");
contentType.put("o" , "application/x-object");
contentType.put("obj" , "application/x-tgif");
contentType.put("ocl" , "text/x-ocl");
contentType.put("oda" , "application/oda");
contentType.put("odb" , "application/vnd.oasis.opendocument.database");
contentType.put("odc" , "application/vnd.oasis.opendocument.chart");
contentType.put("odf" , "application/vnd.oasis.opendocument.formula");
contentType.put("odg" , "application/vnd.oasis.opendocument.graphics");
contentType.put("odi" , "application/vnd.oasis.opendocument.image");
contentType.put("odm" , "application/vnd.oasis.opendocument.text-master");
contentType.put("odp" , "application/vnd.oasis.opendocument.presentation");
contentType.put("ods" , "application/vnd.oasis.opendocument.spreadsheet");
contentType.put("odt" , "application/vnd.oasis.opendocument.text");
contentType.put("oga" , "audio/ogg");
contentType.put("ogg" , "video/x-theora+ogg");
contentType.put("ogm" , "video/x-ogm+ogg");
contentType.put("ogv" , "video/ogg");
contentType.put("ogx" , "application/ogg");
contentType.put("old" , "application/x-trash");
contentType.put("oleo" , "application/x-oleo");
contentType.put("opml" , "text/x-opml+xml");
contentType.put("ora" , "image/openraster");
contentType.put("orf" , "image/x-olympus-orf");
contentType.put("otc" , "application/vnd.oasis.opendocument.chart-template");
contentType.put("otf" , "application/x-font-otf");
contentType.put("otg" , "application/vnd.oasis.opendocument.graphics-template");
contentType.put("oth" , "application/vnd.oasis.opendocument.text-web");
contentType.put("otp" , "application/vnd.oasis.opendocument.presentation-template");
contentType.put("ots" , "application/vnd.oasis.opendocument.spreadsheet-template");
contentType.put("ott" , "application/vnd.oasis.opendocument.text-template");
contentType.put("owl" , "application/rdf+xml");
contentType.put("oxt" , "application/vnd.openofficeorg.extension");
contentType.put("p" , "text/x-pascal");
contentType.put("p10" , "application/pkcs10");
contentType.put("p12" , "application/x-pkcs12");
contentType.put("p7b" , "application/x-pkcs7-certificates");
contentType.put("p7s" , "application/pkcs7-signature");
contentType.put("pack" , "application/x-java-pack200");
contentType.put("pak" , "application/x-pak");
contentType.put("par2" , "application/x-par2");
contentType.put("pas" , "text/x-pascal");
contentType.put("patch" , "text/x-patch");
contentType.put("pbm" , "image/x-portable-bitmap");
contentType.put("pcd" , "image/x-photo-cd");
contentType.put("pcf" , "application/x-cisco-vpn-settings");
contentType.put("pcf.gz" , "application/x-font-pcf");
contentType.put("pcf.z" , "application/x-font-pcf");
contentType.put("pcl" , "application/vnd.hp-pcl");
contentType.put("pcx" , "image/x-pcx");
contentType.put("pdb" , "chemical/x-pdb");
contentType.put("pdc" , "application/x-aportisdoc");
contentType.put("pdf" , "application/pdf");
contentType.put("pdf.bz2" , "application/x-bzpdf");
contentType.put("pdf.gz" , "application/x-gzpdf");
contentType.put("pef" , "image/x-pentax-pef");
contentType.put("pem" , "application/x-x509-ca-cert");
contentType.put("perl" , "application/x-perl");
contentType.put("pfa" , "application/x-font-type1");
contentType.put("pfb" , "application/x-font-type1");
contentType.put("pfx" , "application/x-pkcs12");
contentType.put("pgm" , "image/x-portable-graymap");
contentType.put("pgn" , "application/x-chess-pgn");
contentType.put("pgp" , "application/pgp-encrypted");
contentType.put("php" , "application/x-php");
contentType.put("php3" , "application/x-php");
contentType.put("php4" , "application/x-php");
contentType.put("pict" , "image/x-pict");
contentType.put("pict1" , "image/x-pict");
contentType.put("pict2" , "image/x-pict");
contentType.put("pickle" , "application/python-pickle");
contentType.put("pk" , "application/x-tex-pk");
contentType.put("pkipath" , "application/pkix-pkipath");
contentType.put("pkr" , "application/pgp-keys");
contentType.put("pl" , "application/x-perl");
contentType.put("pla" , "audio/x-iriver-pla");
contentType.put("pln" , "application/x-planperfect");
contentType.put("pls" , "audio/x-scpls");
contentType.put("pm" , "application/x-perl");
contentType.put("png" , "image/png");
contentType.put("pnm" , "image/x-portable-anymap");
contentType.put("pntg" , "image/x-macpaint");
contentType.put("po" , "text/x-gettext-translation");
contentType.put("por" , "application/x-spss-por");
contentType.put("pot" , "text/x-gettext-translation-template");
contentType.put("ppm" , "image/x-portable-pixmap");
contentType.put("pps" , "application/vnd.ms-powerpoint");
contentType.put("ppt" , "application/vnd.ms-powerpoint");
contentType.put("pptm" , "application/vnd.openxmlformats-officedocument.presentationml.presentation");
contentType.put("pptx" , "application/vnd.openxmlformats-officedocument.presentationml.presentation");
contentType.put("ppz" , "application/vnd.ms-powerpoint");
contentType.put("prc" , "application/x-palm-database");
contentType.put("ps" , "application/postscript");
contentType.put("ps.bz2" , "application/x-bzpostscript");
contentType.put("ps.gz" , "application/x-gzpostscript");
contentType.put("psd" , "image/vnd.adobe.photoshop");
contentType.put("psf" , "audio/x-psf");
contentType.put("psf.gz" , "application/x-gz-font-linux-psf");
contentType.put("psflib" , "audio/x-psflib");
contentType.put("psid" , "audio/prs.sid");
contentType.put("psw" , "application/x-pocket-word");
contentType.put("pw" , "application/x-pw");
contentType.put("py" , "text/x-python");
contentType.put("pyc" , "application/x-python-bytecode");
contentType.put("pyo" , "application/x-python-bytecode");
contentType.put("qif" , "image/x-quicktime");
contentType.put("qt" , "video/quicktime");
contentType.put("qtif" , "image/x-quicktime");
contentType.put("qtl" , "application/x-quicktime-media-link");
contentType.put("qtvr" , "video/quicktime");
contentType.put("ra" , "audio/vnd.rn-realaudio");
contentType.put("raf" , "image/x-fuji-raf");
contentType.put("ram" , "application/ram");
contentType.put("rar" , "application/x-rar");
contentType.put("ras" , "image/x-cmu-raster");
contentType.put("raw" , "image/x-panasonic-raw");
contentType.put("rax" , "audio/vnd.rn-realaudio");
contentType.put("rb" , "application/x-ruby");
contentType.put("rdf" , "application/rdf+xml");
contentType.put("rdfs" , "application/rdf+xml");
contentType.put("reg" , "text/x-ms-regedit");
contentType.put("rej" , "application/x-reject");
contentType.put("rgb" , "image/x-rgb");
contentType.put("rle" , "image/rle");
contentType.put("rm" , "application/vnd.rn-realmedia");
contentType.put("rmj" , "application/vnd.rn-realmedia");
contentType.put("rmm" , "application/vnd.rn-realmedia");
contentType.put("rms" , "application/vnd.rn-realmedia");
contentType.put("rmvb" , "application/vnd.rn-realmedia");
contentType.put("rmx" , "application/vnd.rn-realmedia");
contentType.put("roff" , "text/troff");
contentType.put("rp" , "image/vnd.rn-realpix");
contentType.put("rpm" , "application/x-rpm");
contentType.put("rss" , "application/rss+xml");
contentType.put("rt" , "text/vnd.rn-realtext");
contentType.put("rtf" , "application/rtf");
contentType.put("rtx" , "text/richtext");
contentType.put("rv" , "video/vnd.rn-realvideo");
contentType.put("rvx" , "video/vnd.rn-realvideo");
contentType.put("s3m" , "audio/x-s3m");
contentType.put("sam" , "application/x-amipro");
contentType.put("sami" , "application/x-sami");
contentType.put("sav" , "application/x-spss-sav");
contentType.put("scm" , "text/x-scheme");
contentType.put("sda" , "application/vnd.stardivision.draw");
contentType.put("sdc" , "application/vnd.stardivision.calc");
contentType.put("sdd" , "application/vnd.stardivision.impress");
contentType.put("sdp" , "application/sdp");
contentType.put("sds" , "application/vnd.stardivision.chart");
contentType.put("sdw" , "application/vnd.stardivision.writer");
contentType.put("sgf" , "application/x-go-sgf");
contentType.put("sgi" , "image/x-sgi");
contentType.put("sgl" , "application/vnd.stardivision.writer");
contentType.put("sgm" , "text/sgml");
contentType.put("sgml" , "text/sgml");
contentType.put("sh" , "application/x-shellscript");
contentType.put("shar" , "application/x-shar");
contentType.put("shn" , "application/x-shorten");
contentType.put("siag" , "application/x-siag");
contentType.put("sid" , "audio/prs.sid");
contentType.put("sik" , "application/x-trash");
contentType.put("sis" , "application/vnd.symbian.install");
contentType.put("sisx" , "x-epoc/x-sisx-app");
contentType.put("sit" , "application/x-stuffit");
contentType.put("siv" , "application/sieve");
contentType.put("sk" , "image/x-skencil");
contentType.put("sk1" , "image/x-skencil");
contentType.put("skr" , "application/pgp-keys");
contentType.put("slk" , "text/spreadsheet");
contentType.put("smaf" , "application/x-smaf");
contentType.put("smc" , "application/x-snes-rom");
contentType.put("smd" , "application/vnd.stardivision.mail");
contentType.put("smf" , "application/vnd.stardivision.math");
contentType.put("smi" , "application/x-sami");
contentType.put("smil" , "application/smil");
contentType.put("sml" , "application/smil");
contentType.put("sms" , "application/x-sms-rom");
contentType.put("snd" , "audio/basic");
contentType.put("so" , "application/x-sharedlib");
contentType.put("spc" , "application/x-pkcs7-certificates");
contentType.put("spd" , "application/x-font-speedo");
contentType.put("spec" , "text/x-rpm-spec");
contentType.put("spl" , "application/x-shockwave-flash");
contentType.put("spx" , "audio/x-speex");
contentType.put("sql" , "text/x-sql");
contentType.put("sr2" , "image/x-sony-sr2");
contentType.put("src" , "application/x-wais-source");
contentType.put("srf" , "image/x-sony-srf");
contentType.put("srt" , "application/x-subrip");
contentType.put("ssa" , "text/x-ssa");
contentType.put("stc" , "application/vnd.sun.xml.calc.template");
contentType.put("std" , "application/vnd.sun.xml.draw.template");
contentType.put("sti" , "application/vnd.sun.xml.impress.template");
contentType.put("stm" , "audio/x-stm");
contentType.put("stw" , "application/vnd.sun.xml.writer.template");
contentType.put("sty" , "text/x-tex");
contentType.put("sub" , "text/x-subviewer");
contentType.put("sun" , "image/x-sun-raster");
contentType.put("sv4cpio" , "application/x-sv4cpio");
contentType.put("sv4crc" , "application/x-sv4crc");
contentType.put("svg" , "image/svg+xml");
contentType.put("svgz" , "image/svg+xml-compressed");
contentType.put("swf" , "application/x-shockwave-flash");
contentType.put("sxc" , "application/vnd.sun.xml.calc");
contentType.put("sxd" , "application/vnd.sun.xml.draw");
contentType.put("sxg" , "application/vnd.sun.xml.writer.global");
contentType.put("sxi" , "application/vnd.sun.xml.impress");
contentType.put("sxm" , "application/vnd.sun.xml.math");
contentType.put("sxw" , "application/vnd.sun.xml.writer");
contentType.put("sylk" , "text/spreadsheet");
contentType.put("t" , "text/troff");
contentType.put("t2t" , "text/x-txt2tags");
contentType.put("tar" , "application/x-tar");
contentType.put("tar.bz" , "application/x-bzip-compressed-tar");
contentType.put("tar.bz2" , "application/x-bzip-compressed-tar");
contentType.put("tar.gz" , "application/x-compressed-tar");
contentType.put("tar.lzma" , "application/x-lzma-compressed-tar");
contentType.put("tar.lzo" , "application/x-tzo");
contentType.put("tar.xz" , "application/x-xz-compressed-tar");
contentType.put("tar.z" , "application/x-tarz");
contentType.put("tbz" , "application/x-bzip-compressed-tar");
contentType.put("tbz2" , "application/x-bzip-compressed-tar");
contentType.put("tcl" , "text/x-tcl");
contentType.put("tex" , "text/x-tex");
contentType.put("texi" , "text/x-texinfo");
contentType.put("texinfo" , "text/x-texinfo");
contentType.put("tga" , "image/x-tga");
contentType.put("tgz" , "application/x-compressed-tar");
contentType.put("theme" , "application/x-theme");
contentType.put("themepack" , "application/x-windows-themepack");
contentType.put("tif" , "image/tiff");
contentType.put("tiff" , "image/tiff");
contentType.put("tk" , "text/x-tcl");
contentType.put("tlz" , "application/x-lzma-compressed-tar");
contentType.put("tnef" , "application/vnd.ms-tnef");
contentType.put("tnf" , "application/vnd.ms-tnef");
contentType.put("toc" , "application/x-cdrdao-toc");
contentType.put("torrent" , "application/x-bittorrent");
contentType.put("tpic" , "image/x-tga");
contentType.put("tr" , "text/troff");
contentType.put("ts" , "application/x-linguist");
contentType.put("tsv" , "text/tab-separated-values");
contentType.put("tta" , "audio/x-tta");
contentType.put("ttc" , "application/x-font-ttf");
contentType.put("ttf" , "application/x-font-ttf");
contentType.put("ttx" , "application/x-font-ttx");
contentType.put("txt" , "text/plain");
contentType.put("txz" , "application/x-xz-compressed-tar");
contentType.put("tzo" , "application/x-tzo");
contentType.put("ufraw" , "application/x-ufraw");
contentType.put("ui" , "application/x-designer");
contentType.put("uil" , "text/x-uil");
contentType.put("ult" , "audio/x-mod");
contentType.put("uni" , "audio/x-mod");
contentType.put("uri" , "text/x-uri");
contentType.put("url" , "text/x-uri");
contentType.put("ustar" , "application/x-ustar");
contentType.put("vala" , "text/x-vala");
contentType.put("vapi" , "text/x-vala");
contentType.put("vcf" , "text/directory");
contentType.put("vcs" , "text/calendar");
contentType.put("vct" , "text/directory");
contentType.put("vda" , "image/x-tga");
contentType.put("vhd" , "text/x-vhdl");
contentType.put("vhdl" , "text/x-vhdl");
contentType.put("viv" , "video/vivo");
contentType.put("vivo" , "video/vivo");
contentType.put("vlc" , "audio/x-mpegurl");
contentType.put("vob" , "video/mpeg");
contentType.put("voc" , "audio/x-voc");
contentType.put("vor" , "application/vnd.stardivision.writer");
contentType.put("vst" , "image/x-tga");
contentType.put("wav" , "audio/x-wav");
contentType.put("wax" , "audio/x-ms-asx");
contentType.put("wb1" , "application/x-quattropro");
contentType.put("wb2" , "application/x-quattropro");
contentType.put("wb3" , "application/x-quattropro");
contentType.put("wbmp" , "image/vnd.wap.wbmp");
contentType.put("wcm" , "application/vnd.ms-works");
contentType.put("wdb" , "application/vnd.ms-works");
contentType.put("webm" , "video/webm");
contentType.put("wk1" , "application/vnd.lotus-1-2-3");
contentType.put("wk3" , "application/vnd.lotus-1-2-3");
contentType.put("wk4" , "application/vnd.lotus-1-2-3");
contentType.put("wks" , "application/vnd.ms-works");
contentType.put("wma" , "audio/x-ms-wma");
contentType.put("wmf" , "image/x-wmf");
contentType.put("wml" , "text/vnd.wap.wml");
contentType.put("wmls" , "text/vnd.wap.wmlscript");
contentType.put("wmv" , "video/x-ms-wmv");
contentType.put("wmx" , "audio/x-ms-asx");
contentType.put("wp" , "application/vnd.wordperfect");
contentType.put("wp4" , "application/vnd.wordperfect");
contentType.put("wp5" , "application/vnd.wordperfect");
contentType.put("wp6" , "application/vnd.wordperfect");
contentType.put("wpd" , "application/vnd.wordperfect");
contentType.put("wpg" , "application/x-wpg");
contentType.put("wpl" , "application/vnd.ms-wpl");
contentType.put("wpp" , "application/vnd.wordperfect");
contentType.put("wps" , "application/vnd.ms-works");
contentType.put("wri" , "application/x-mswrite");
contentType.put("wrl" , "model/vrml");
contentType.put("wv" , "audio/x-wavpack");
contentType.put("wvc" , "audio/x-wavpack-correction");
contentType.put("wvp" , "audio/x-wavpack");
contentType.put("wvx" , "audio/x-ms-asx");
contentType.put("x3f" , "image/x-sigma-x3f");
contentType.put("xac" , "application/x-gnucash");
contentType.put("xbel" , "application/x-xbel");
contentType.put("xbl" , "application/xml");
contentType.put("xbm" , "image/x-xbitmap");
contentType.put("xcf" , "image/x-xcf");
contentType.put("xcf.bz2" , "image/x-compressed-xcf");
contentType.put("xcf.gz" , "image/x-compressed-xcf");
contentType.put("xhtml" , "application/xhtml+xml");
contentType.put("xi" , "audio/x-xi");
contentType.put("xla" , "application/vnd.ms-excel");
contentType.put("xlc" , "application/vnd.ms-excel");
contentType.put("xld" , "application/vnd.ms-excel");
contentType.put("xlf" , "application/x-xliff");
contentType.put("xliff" , "application/x-xliff");
contentType.put("xll" , "application/vnd.ms-excel");
contentType.put("xlm" , "application/vnd.ms-excel");
contentType.put("xls" , "application/vnd.ms-excel");
contentType.put("xlsm" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
contentType.put("xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
contentType.put("xlt" , "application/vnd.ms-excel");
contentType.put("xlw" , "application/vnd.ms-excel");
contentType.put("xm" , "audio/x-xm");
contentType.put("xmf" , "audio/x-xmf");
contentType.put("xmi" , "text/x-xmi");
contentType.put("xml" , "application/xml");
contentType.put("xpm" , "image/x-xpixmap");
contentType.put("xps" , "application/vnd.ms-xpsdocument");
contentType.put("xsl" , "application/xml");
contentType.put("xslfo" , "text/x-xslfo");
contentType.put("xslt" , "application/xml");
contentType.put("xspf" , "application/xspf+xml");
contentType.put("xul" , "application/vnd.mozilla.xul+xml");
contentType.put("xwd" , "image/x-xwindowdump");
contentType.put("xyz" , "chemical/x-pdb");
contentType.put("xz" , "application/x-xz");
contentType.put("w2p" , "application/w2p");
contentType.put("z" , "application/x-compress");
contentType.put("zabw" , "application/x-abiword");
contentType.put("zip" , "application/zip");
}
/**
* 根据文件流获取文件类型
* @param objectName
* @return
*/
public static String getFileContentType(String objectName) {
return contentType.get(FileTypeUtil.getFileSuffix(objectName));
}
}
@@ -0,0 +1,19 @@
server:
port: 10086
spring:
datasource:
url: jdbc:mysql://localhost:3306/upload-file?useSSL=false&serverTimezone=Asia/Shanghai
username: ${MYSQL_USERNAME:root}
password: ${MYSQL_password:root}
driver-class-name: com.mysql.cj.jdbc.Driver
servlet:
multipart:
max-file-size: 500MB
max-request-size: 500MB
main:
allow-bean-definition-overriding: true
logging:
level:
cn.czh.mapper: debug
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.czh.mapper.UploadFileMapper">
<select id="pageFiles" resultType="cn.czh.entity.UploadFile">
select * from upload_file
<where>
<if test="storageType != null and storageType != ''">
and storage_type = #{storageType}
</if>
<if test="fileName != null and fileName !=''">
and file_name like concat('%', #{fileName}, '%')
</if>
and is_finish = 1
</where>
</select>
</mapper>