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

239
README.md Normal file
View File

@@ -0,0 +1,239 @@
# upload-hub
一个支持多种存储方式的文件上传系统,提供简单易用的文件管理功能。
## 演示
![ev](images/ev_1.gif)
## 项目简介
本项目是一个基于前后端分离架构的文件上传系统,支持多种存储方式,包括本地存储、阿里云 OSS 和 MinIO。用户可以通过直观的界面上传文件并管理已上传的文件。
### 技术栈
- **前端**:
- Vue 2
- ElementUI
- **后端**:
- JDK 1.8
- Spring Boot 2.3.12.RELEASE
- MySQL 8.0.26
- MyBatis-Plus
- **文件存储**:
- Local本地存储
- OSS阿里云对象存储
- MinIO分布式对象存储
- 敬请期待。。。
## 开发者的话
尊敬的读者:
首先,请允许我向您致以诚挚的问候,并对项目中可能存在的不足表达由衷的歉意。作为一名主要专注于后端开发的工程师,我在前端设计与用户界面的呈现上仍处于学习与成长的阶段。因此,您在体验本项目时,可能会注意到页面布局不够精致、样式设计略显简朴、交互体验有待流畅,或是响应式设计尚未完美适配等问题。这些瑕疵,或许无法完全展现我对这个项目的完整构想,也可能在某种程度上影响您的使用体验。
我深知,一个优雅、直观的用户界面是产品体验不可或缺的一部分。为此,我正全力以赴提升前端开发技能,并积极探索更优的解决方案,以弥补现阶段的不足,逐步提升页面的美观度与实用性。
您的每一条意见与建议对我来说都是弥足珍贵的财富。如果您在使用过程中有任何想法或改进建议欢迎随时与我联系邮箱chenzhihua0123@gmail.com 或 GitHub Issues。我承诺将认真倾听您的需求并尽我所能优化这个项目力求为您带来一个更加美观、友好且高效的文件上传工具。
感谢您的耐心与包容。正是因为有您的支持,我才有动力不断前行。我坚信,随着时间的推移,我的技能会逐步精进,项目的品质也将日益提升。希望我们能一起见证这份作品从青涩走向成熟的旅程,每一个小小的进步都将成为我们共同的成就。
再次感谢您的理解与陪伴,期待与您携手共创更美好的体验!
此致,
czh-dev
2025年3月
## 快速开始
### 前端
1. 克隆项目到本地:
```bash
git clone https://gitee.com/czh-dev/upload-hub.git
cd upload-file-frontend
```
2. 安装依赖:
```bash
npm install
```
3. 启动开发服务器:
```bash
npm run serve
```
4. 访问应用: 在浏览器中打开 http://localhost:8080。
### 后端
1. 进入后端目录:
```bash
cd upload-file-backend
```
2. 配置环境:
确保已安装 JDK 1.8 和 Maven。
在 application.yml 中配置数据库和存储服务参数(见下方配置说明)。
3. 构建并运行:
```bash
mvn clean install
mvn spring-boot:run
```
4. 接口地址: 默认运行在 http://localhost:10086。
## 目录结构
```text
.
├── upload-file-backend # 后端代码
│ ├── src # 源代码
│ └── data # 本地上传的数据文件
│ └── sql # SQL 脚本
│ └── pom.xml # Maven 配置文件
├── upload-file-frontend # 前端代码
│ ├── src # 源代码
│ ├── public # 静态资源
│ └── package.json # NPM 配置文件
└── README.md # 项目说明文档
```
## 前期准备
### 数据库
1. 安装MySQL8.0或兼容版本
2. 创建数据库upload_file
```sql
CREATE DATABASE upload_file DEFAULT CHARACTER SET utf8mb4;
```
3. 执行 `sql` 目录下的 SQL 脚本初始化表结构。
### 本地存储
- 修改数据库配置(`storage_config`表):
**type=local**
- Bucket项目中data目录的绝对路径
### MinIO
> 可选
#### 创建MinIO容器
```bash
docker run -d --name upload-file \
-p 9000:9000 \
-p 9090:9090 \
-e MINIO_ROOT_USER=minio \
-e MINIO_ROOT_PASSWORD=minio123 \
-v E:\develop\MinIO\data:/data \
minio/minio server /data --console-address ":9090"
```
- 访问地址http://localhost:9000API / http://localhost:9090控制台
- 默认凭证:用户名 minio密码 minio123
- 存储路径:映射到本地 E:\develop\MinIO\dataWindows 示例,根据实际情况调整)。
- 创建Bucket并且设置权限为`public`
- 修改数据库配置(`storage_config`表):
**type=minio**
- endpointhttp://localhost:9000
- access_keyminio
- secret_keyminio123
- bucket你的Bucket名称
### OSS阿里云对象存储
> 可选
1. 开通服务:
- 登录 阿里云控制台 开通 OSS 服务。
- 创建 Bucket如 upload-file-bucket
2. 获取凭证:
- 在「访问控制 RAM」中创建 AccessKey记录 AccessKey ID 和 AccessKey Secret。
3. 设置跨域请求 (CORS)
- 进入 OSS 控制台,找到目标 Bucket。
- 在「基础设置」->「跨域设置」中添加规则:
- 来源:*(或指定域名)
- 允许 MethodsGET, POST, PUT
- 允许 Headers*
- 示例截图:
![img.png](images/img.png)
4. 修改数据库配置(`storage_config`表):
**type=oss**
- endpoint你的OSS外网访问地域节点OSS控制台概览处查看
- access_key你的AccessKey ID
- secret_key你的AccessKey Secret
- bucket你的Bucket名称
## 配置说明
### 后端配置文件 (application.yml)
```yaml
server:
port: 10086
spring:
datasource:
url: jdbc:mysql://localhost:3306/upload-file?useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: your-password
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
```
## 功能特性
- 断点续传
- 文件分片
- 文件校验
- 文件秒传
- 多文件上传
- 支持多种存储方式切换
- 文件预览和管理
- 分页加载已上传文件
## 贡献
欢迎提交 Issue 或 Pull Request

BIN
images/ev_1.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

BIN
images/img.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

67
sql/upload-file.sql Normal file
View File

@@ -0,0 +1,67 @@
/*
Navicat Premium Dump SQL
Source Server : czh
Source Server Type : MySQL
Source Server Version : 80025 (8.0.25)
Source Host : localhost:3306
Source Schema : upload-file
Target Server Type : MySQL
Target Server Version : 80025 (8.0.25)
File Encoding : 65001
Date: 09/03/2025 00:03:50
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for storage_config
-- ----------------------------
DROP TABLE IF EXISTS `storage_config`;
CREATE TABLE `storage_config` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '配置id',
`type` enum('local','minio','oss','obs') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '存储类型(\'local\',\'minio\',\'oss\',\'obs\')',
`endpoint` varchar(5000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '访问地址',
`access_key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '用户名',
`secret_key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '密码',
`bucket` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '',
PRIMARY KEY (`id`) USING BTREE,
INDEX `status+name`(`access_key` ASC) USING BTREE,
INDEX `name`(`access_key` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '配置表' ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of storage_config
-- ----------------------------
INSERT INTO `storage_config` VALUES (1, 'local', 'http://localhost:10086', '', '', 'E:\\develop\\Java\\xxxxxx\\upload-file-backend\\data');
INSERT INTO `storage_config` VALUES (2, 'minio', 'http://localhost:9000', 'minio', 'minio123', 'upload-file');
INSERT INTO `storage_config` VALUES (3, 'oss', 'https://oss-cn-guangzhou.aliyuncs.com', 'ossAccessKeyID', 'ossAccessKeySecret', 'yourBucket');
-- ----------------------------
-- Table structure for upload_file
-- ----------------------------
DROP TABLE IF EXISTS `upload_file`;
CREATE TABLE `upload_file` (
`id` bigint NOT NULL AUTO_INCREMENT,
`upload_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '分片上传的uploadId',
`file_identifier` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '文件唯一标识md5',
`file_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '文件名',
`object_key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '文件的key',
`total_size` bigint NOT NULL COMMENT '文件大小byte',
`chunk_size` bigint NOT NULL COMMENT '每个分片大小byte',
`chunk_num` int NOT NULL COMMENT '分片数量',
`is_finish` int NOT NULL COMMENT '是否已完成上传(完成合并),1是0否',
`content_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文件类型',
`access_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '访问地址',
`download_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '下载地址',
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
`storage_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '上传类型',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `uq_file_identifier`(`file_identifier` ASC) USING BTREE,
UNIQUE INDEX `uq_upload_id`(`upload_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '上传文件记录表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

83
upload-file-backend/.gitignore vendored Normal file
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

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>

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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));
}
}

View File

@@ -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));
}
}
}

View File

@@ -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;
}

View File

@@ -0,0 +1,15 @@
package cn.czh.dto;
import java.util.Date;
public interface MyPartSummary {
int getPartNumber();
Date getLastModified();
String getETag();
long getSize();
}

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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> {
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -0,0 +1,12 @@
package cn.czh.service;
import cn.czh.entity.StorageConfig;
public interface IStorageConfigService {
/**
* 根据type获取存储配置
*/
StorageConfig getStorageConfigByType(String type);
}

View File

@@ -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);
}

View File

@@ -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("不支持的存储类型");
}
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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));
}
}

View File

@@ -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

View File

@@ -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>

View File

@@ -0,0 +1,3 @@
> 1%
last 2 versions
not dead

View File

@@ -0,0 +1,17 @@
module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/essential',
'eslint:recommended'
],
parserOptions: {
parser: '@babel/eslint-parser'
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}

23
upload-file-frontend/.gitignore vendored Normal file
View File

@@ -0,0 +1,23 @@
.DS_Store
node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View File

@@ -0,0 +1,5 @@
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
}

View File

@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"baseUrl": "./",
"moduleResolution": "node",
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
}
}

View File

@@ -0,0 +1,36 @@
{
"name": "upload-file-component",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"axios": "^1.8.1",
"axios-extra": "^0.0.8",
"core-js": "^3.8.3",
"element-ui": "^2.15.14",
"promise-queue-plus": "^1.2.2",
"spark-md5": "^3.0.2",
"vue": "^2.6.14",
"vue-router": "^3.5.1",
"vue-virtual-scroller": "^1.1.2",
"vuex": "^3.6.2"
},
"devDependencies": {
"@babel/core": "^7.12.16",
"@babel/eslint-parser": "^7.12.16",
"@vue/cli-plugin-babel": "~5.0.0",
"@vue/cli-plugin-eslint": "~5.0.0",
"@vue/cli-plugin-router": "~5.0.0",
"@vue/cli-plugin-vuex": "~5.0.0",
"@vue/cli-service": "~5.0.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"sass": "^1.32.7",
"sass-loader": "^12.0.0",
"vue-template-compiler": "^2.6.14"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

View File

@@ -0,0 +1,46 @@
<template>
<div id="app">
<div class="app-container">
<router-view />
</div>
</div>
</template>
<script>
export default {
name: 'App',
components: {
}
};
</script>
<style scoped>
#app {
box-sizing: border-box;
background: linear-gradient(135deg, #f5f9ff 0%, #e6f0ff 100%);
height: 100vh;
padding: 0;
font-family: 'Segoe UI', system-ui, sans-serif;
display: flex;
justify-content: center;
align-items: flex-start;
overflow: auto;
padding-top: 80px;
}
.app-container {
width: 90%;
max-width: 1000px;
margin: 0 auto;
}
</style>
<style>
/* 全局样式 */
body, html {
margin: 0;
padding: 0;
height: 100%;
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

@@ -0,0 +1,60 @@
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<p>
For a guide and recipes on how to configure / customize this project,<br>
check out the
<a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
</p>
<h3>Installed CLI Plugins</h3>
<ul>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-router" target="_blank" rel="noopener">router</a></li>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-vuex" target="_blank" rel="noopener">vuex</a></li>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
</ul>
<h3>Essential Links</h3>
<ul>
<li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
<li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
<li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
<li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
<li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
</ul>
<h3>Ecosystem</h3>
<ul>
<li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
<li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
<li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
<li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
<li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg: String
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,16 @@
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')

View File

@@ -0,0 +1,21 @@
import Vue from 'vue'
import VueRouter from 'vue-router'
import FileUpload from '@/views/FileUpload.vue';
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: FileUpload
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router

View File

@@ -0,0 +1,17 @@
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
getters: {
},
mutations: {
},
actions: {
},
modules: {
}
})

View File

@@ -0,0 +1,138 @@
import { http, httpExtra } from './http';
/**
* 获取上传进度
* @param {string} identifier 文件的 MD5 标识
* @param {string} storageType 存储类型
* @returns {Promise<any>} 上传进度信息
*/
const getUploadProgress = (identifier, storageType) => {
return http.get('/upload/getUploadProgress', {
params: { identifier },
headers: { 'Storage-Type': storageType }
});
};
/**
* 创建分片上传任务
* @param {Object} params 参数对象
* @param {string} params.identifier 文件的 MD5 标识
* @param {string} params.fileName 文件名称
* @param {number} params.totalSize 文件总大小 (字节)
* @param {number} params.chunkSize 分片大小 (字节)
* @param {string} params.contentType 文件 MIME 类型
* @param {string} params.folder 文件存储文件夹
* @param {string} params.storageType 存储类型
* @returns {Promise<any>} 分片上传任务信息
*/
const createMultipartUpload = ({ identifier, fileName, totalSize, chunkSize, contentType, folder, storageType }) => {
return http.post('/upload/createMultipartUpload', {
identifier,
fileName,
totalSize,
chunkSize,
contentType,
folder
}, {
headers: { 'Storage-Type': storageType }
});
};
/**
* 获取预签名分片上传 URL
* @param {Object} params 参数对象
* @param {string} params.identifier 文件的 MD5 标识
* @param {number} params.partNumber 分片编号
* @param {string} params.storageType 存储类型
* @returns {Promise<any>} 预签名上传 URL
*/
const getPreSignUploadUrl = ({ identifier, partNumber, storageType }) => {
return http.get('/upload/getPreSignUploadUrl', {
params: { identifier, partNumber },
headers: { 'Storage-Type': storageType }
});
};
/**
* 合并分片
* @param {string} identifier 文件的 MD5 标识
* @param {string} storageType 存储类型
* @returns {Promise<any>} 合并结果
*/
const merge = (identifier, storageType) => {
return http.post('/upload/merge', null, {
params: { identifier },
headers: { 'Storage-Type': storageType }
});
};
/**
* 上传文件到后端 /upload 接口
* @param {Object} params 参数对象
* @param {File} params.file 文件对象
* @param {string} [params.folder] 文件夹名称 (可选)
* @param {number} [params.fileType] 文件类型 (可选)
* @param {string} params.storageType 存储类型
* @returns {Promise<any>} 上传结果
*/
const uploadFile = ({ file, folder, fileType, storageType }) => {
const formData = new FormData();
formData.append('file', file); // 文件字段
if (folder) formData.append('folder', folder); // 可选的 folder 参数
if (fileType !== undefined) formData.append('fileType', fileType); // 可选的 fileType 参数
return http.post('/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data',
'Storage-Type': storageType
}
});
};
/**
* 上传单个分片
* @param {Object} params 参数对象
* @param {string} params.uploadId 上传任务 ID
* @param {number} params.partNumber 分片编号
* @param {File} params.partFile 分片文件
* @param {string} params.storageType 存储类型
* @returns {Promise<any>} 上传结果
*/
const uploadPart = ({ uploadId, partNumber, partFile, storageType }) => {
const formData = new FormData();
formData.append('uploadId', uploadId);
formData.append('partNumber', partNumber);
formData.append('file', partFile);
return http.post('/upload/part', formData, {
headers: {
'Content-Type': 'multipart/form-data',
'Storage-Type': storageType
}
});
};
/**
* 分页获取文件列表
* @param {Object} params 参数对象
* @param {number} [params.page=1] 页码,默认 1
* @param {number} [params.pageSize=10] 每页大小,默认 10
* @param {string} params.storageType 存储类型
* @returns {Promise<any>} 文件列表
*/
const pageFiles = ({ page = 1, pageSize = 10, storageType, fileName }) => {
return http.get('/file/page', {
params: { page, pageSize, storageType, fileName }
});
};
export {
getUploadProgress,
createMultipartUpload,
getPreSignUploadUrl,
merge,
uploadFile,
uploadPart,
pageFiles,
httpExtra
};

View File

@@ -0,0 +1,25 @@
import axios from 'axios';
import axiosExtra from 'axios-extra';
const baseUrl = 'http://localhost:10086';
// 基础 Axios 实例
const http = axios.create({
baseURL: baseUrl
});
// 配置拦截器,返回 response.data
http.interceptors.response.use(response => {
return response.data;
});
// 配置 axios-extra 实例,用于控制并发和重试
const httpExtra = axiosExtra.create({
maxConcurrent: 5, // 并发数为 5
queueOptions: {
retry: 3, // 请求失败时最多重试 3 次
retryIsJump: false // 重试时插入队列尾部而非立即重试
}
});
export { http, baseUrl, httpExtra };

View File

@@ -0,0 +1,40 @@
import SparkMD5 from 'spark-md5'
const DEFAULT_SIZE = 20 * 1024 * 1024
const md5 = (file, chunkSize = DEFAULT_SIZE) => {
return new Promise((resolve, reject) => {
const startMs = new Date().getTime();
let blobSlice =
File.prototype.slice ||
File.prototype.mozSlice ||
File.prototype.webkitSlice;
let chunks = Math.ceil(file.size / chunkSize);
let currentChunk = 0;
let spark = new SparkMD5.ArrayBuffer(); //追加数组缓冲区。
let fileReader = new FileReader(); //读取文件
fileReader.onload = function (e) {
spark.append(e.target.result);
currentChunk++;
if (currentChunk < chunks) {
loadNext();
} else {
const md5 = spark.end(); //完成md5的计算返回十六进制结果。
console.log('文件md5计算结束总耗时', (new Date().getTime() - startMs) / 1000, 's')
resolve(md5);
}
};
fileReader.onerror = function (e) {
reject(e);
};
function loadNext() {
console.log('当前part number', currentChunk, '总块数:', chunks);
let start = currentChunk * chunkSize;
let end = start + chunkSize;
(end > file.size) && (end = file.size);
fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
}
loadNext();
});
}
export default md5

View File

@@ -0,0 +1,5 @@
<template>
<div class="about">
<h1>This is an about page</h1>
</div>
</template>

View File

@@ -0,0 +1,707 @@
<template>
<div class="upload-container">
<div class="upload-window">
<!-- 标题栏 -->
<div class="header-wrapper">
<h1 class="page-title">
<span class="cloud-icon"></span>
文件上传中心
</h1>
<div class="menu-container" @mouseenter="showMenu = true" @mouseleave="showMenu = false">
<div class="menu-icon"></div>
<div v-show="showMenu" class="dropdown-menu">
<div class="menu-item" @click="openSettings">设置</div>
<div class="menu-item" @click="openAbout">关于</div>
</div>
</div>
</div>
<!-- 标签导航 -->
<div class="tab-bar">
<div v-for="tab in tabs" :key="tab.value" :class="['tab', { 'active': activeTab === tab.value }]"
@click="switchTab(tab.value)">
{{ tab.label }}
<div class="tab-underline"></div>
</div>
</div>
<!-- 上传区域 -->
<div v-if="activeTab !== 'gallery' && !isWipTab" class="upload-content">
<upload-file :file-list.sync="fileList" :accept="allowedFormats" :max-size="maxUploadSize" width="100%"
height="400px" :storage-type="activeTab" />
</div>
<!-- 未实现功能提示 -->
<div v-else-if="isWipTab" class="wip-container">
<div class="wip-message">
<span class="wip-icon">🚧</span>
<h2>功能建设中</h2>
<p v-if="activeTab === 'obs'">正在加班加点搬砖中敬请期待</p>
<p v-else-if="activeTab === 'qiniu'">七牛云功能正在被疯狂调教马上就能和大家见面啦</p>
</div>
</div>
<!-- 图片展示区域 -->
<div v-else class="gallery-container" @scroll="handleScroll">
<div class="gallery-header">
<label for="storage-select">存储类型:</label>
<select id="storage-select" v-model="selectedStorageType" @change="fetchFiles(true)">
<option v-for="option in storageOptions" :key="option.value" :value="option.value">
{{ option.label }}
</option>
</select>
<label for="storage-select">&nbsp;&nbsp;&nbsp;&nbsp;文件名:</label>
<el-input v-model="selectedFileName" placeholder="请输入文件名" class="file-name-input" style="width: 150px;" @change="fetchFiles(true)"></el-input>
</div>
<div v-if="filePage.length === 0 && !loading" class="empty-tips">
🖼 暂无已上传的文件
</div>
<div v-else-if="loading && filePage.length === 0" class="loading-tips">
加载中...
</div>
<div v-else class="file-grid">
<div v-for="file in filePage" :key="file.id" class="file-card">
<div class="preview-wrapper">
<img v-if="isImage(file)" :src="file.accessUrl" :alt="file.fileName" class="preview-image" />
<div v-else class="file-icon">
📄
</div>
</div>
<div class="file-meta">
<div class="filename">{{ file.fileName }}</div>
<div class="file-size-status">
<div class="file-size">{{ formatSize(file.totalSize) }}</div>
<div class="status-indicator">{{ formattedDate(file.createTime) }}</div>
</div>
</div>
</div>
</div>
<div v-if="loading && filePage.length > 0" class="loading-more">
加载更多...
</div>
</div>
<!-- 设置表单弹窗形式 -->
<div v-if="showSettings" class="settings-modal" @click="closeSettingsOnOutside">
<div class="settings-content" @click.stop>
<h2>上传设置</h2>
<form @submit.prevent="saveSettings">
<div class="form-group">
<label>允许上传的文件格式用英文逗号分隔例如 .jpg,.png:</label>
<input v-model="tempAllowedFormats" type="text" placeholder=".jpg,.png,.mp4" />
</div>
<div class="form-group">
<label>最大上传大小MB:</label>
<input v-model.number="tempMaxSizeMB" type="number" min="1" step="1" />
</div>
<div class="form-actions">
<button type="submit">保存</button>
<button type="button" @click="closeSettings">取消</button>
</div>
</form>
</div>
</div>
<!-- 关于弹窗 -->
<div v-if="showAbout" class="about-modal" @click="closeAboutOnOutside">
<div class="about-content" @click.stop>
<h2>关于</h2>
<p>这个人很懒什么都没有留下只说自己爱吃炸排骨</p>
</div>
</div>
</div>
</div>
</template>
<script>
import UploadFile from '@/components/UploadFile/UploadFile.vue';
import { pageFiles } from '@/utils/api';
export default {
name: 'FileUpload',
components: { UploadFile },
data() {
return {
fileList: [],
activeTab: 'local',
tabs: [
{ label: 'Local', value: 'local' },
{ label: 'MinIO', value: 'minio' },
{ label: 'OSS', value: 'oss' },
{ label: 'OBS', value: 'obs' },
{ label: 'QiNiu', value: 'qiniu' },
{ label: '已上传文件', value: 'gallery' }
],
showMenu: false,
showSettings: false,
showAbout: false,
allowedFormats: '.jpg,.png,.mp4',
maxUploadSize: 100 * 1024 * 1024,
tempAllowedFormats: '',
tempMaxSizeMB: 100,
// 分页相关
selectedStorageType: 'local', // 默认存储类型
selectedFileName: '',
storageOptions: [
{ label: 'Local', value: 'local' },
{ label: 'MinIO', value: 'minio' },
{ label: 'OSS', value: 'oss' }
],
currentPage: 1,
pageSize: 10,
loading: false,
hasMore: true, // 是否还有更多数据
filePage: []
};
},
computed: {
formattedDate() {
return (date) => {
const d = new Date(date);
const year = d.getFullYear();
const month = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
},
isWipTab() {
return this.activeTab === 'obs' || this.activeTab === 'qiniu';
}
},
methods: {
switchTab(tabValue) {
this.activeTab = tabValue;
if (tabValue === 'gallery') {
this.fetchFiles(true);
} else if (this.isWipTab) {
this.$message({
message: tabValue === 'obs'
? 'OBS功能正在施工中小哥哥们正在挥汗如雨'
: '七牛云功能开发中,程序员正在和咖啡斗智斗勇!',
type: 'info',
duration: 2000
});
}
},
isImage(file) {
return file.contentType.startsWith('image/');
},
formatSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
},
openSettings() {
this.tempAllowedFormats = this.allowedFormats;
this.tempMaxSizeMB = this.maxUploadSize / (1024 * 1024);
this.showSettings = true;
this.showMenu = false;
},
saveSettings() {
this.allowedFormats = this.tempAllowedFormats;
this.maxUploadSize = this.tempMaxSizeMB * 1024 * 1024;
this.showSettings = false;
},
closeSettings() {
this.showSettings = false;
},
closeSettingsOnOutside(event) {
if (event.target.classList.contains('settings-modal')) {
this.closeSettings();
}
},
openAbout() {
this.showAbout = true;
this.showMenu = false;
},
closeAboutOnOutside(event) {
if (event.target.classList.contains('about-modal')) {
this.showAbout = false;
}
},
// 获取文件列表
async fetchFiles(reset = false) {
console.log('fetchFiles called with reset:', reset, this.loading, this.hasMore); // 调试日志
if (this.loading || (!reset && !this.hasMore)) return;
this.loading = true;
if (reset) {
this.currentPage = 1;
this.filePage = [];
this.hasMore = true;
}
try {
const response = await pageFiles({
page: this.currentPage,
pageSize: this.pageSize,
storageType: this.selectedStorageType,
fileName: this.selectedFileName
});
const files = response.data.records || [];
this.filePage = reset ? files : this.filePage.concat(files);
this.hasMore = files.length === this.pageSize;
if (this.hasMore) this.currentPage++;
} catch (error) {
console.error('获取文件列表失败:', error);
this.$message.error('加载文件列表失败');
} finally {
this.loading = false;
}
},
// 滚动触底加载
handleScroll(event) {
const container = event.target;
const isBottom =
container.scrollTop + container.clientHeight >= container.scrollHeight - 10; // 提前 10px 触发
if (isBottom && !this.loading && this.hasMore) {
this.fetchFiles();
}
}
}
};
</script>
<style scoped>
.wip-container {
padding: 2rem;
height: 400px;
background: #fcfdff;
border-top: 1px solid #f0f7ff;
display: flex;
justify-content: center;
align-items: center;
}
.wip-message {
text-align: center;
color: #666;
}
.wip-icon {
font-size: 3rem;
margin-bottom: 1rem;
display: block;
}
.wip-message h2 {
font-size: 1.5rem;
color: #1976d2;
margin: 0.5rem 0;
}
.wip-message p {
font-size: 1rem;
max-width: 400px;
margin: 0 auto;
line-height: 1.5;
}
.upload-container {
width: 100%;
background: #ffffff;
border-radius: 14px;
box-shadow: 0 12px 24px rgba(25, 118, 210, 0.1);
overflow: hidden;
border: 1px solid #e3f2fd;
}
/* 标题栏布局 */
.header-wrapper {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1.5rem 2rem;
background: #1976d2;
}
.page-title {
margin: 0;
color: white;
font-size: 1.5rem;
font-weight: 600;
display: flex;
align-items: center;
gap: 12px;
}
.cloud-icon {
font-size: 1.8rem;
filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
}
/* 菜单图标和下拉样式 */
.menu-container {
position: relative;
}
.menu-icon {
color: white;
font-size: 1.8rem;
cursor: pointer;
padding: 0 10px;
}
.dropdown-menu {
position: absolute;
top: 100%;
right: 0;
background: #fff;
border-radius: 6px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
min-width: 120px;
z-index: 10;
}
.menu-item {
padding: 8px 16px;
color: #333;
cursor: pointer;
font-size: 14px;
transition: background 0.2s;
}
.menu-item:hover {
background: #f0f7ff;
}
/* 标签导航样式 */
.tab-bar {
display: flex;
padding: 0 2rem;
background: #f8fafd;
border-bottom: 1px solid #e0eefc;
gap: 8px;
position: relative;
}
.tab {
position: relative;
padding: 14px 32px;
font-size: 14px;
color: #607d9f;
cursor: pointer;
background: transparent;
border: none;
border-radius: 6px 6px 0 0;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
display: flex;
flex-direction: column;
gap: 4px;
}
.tab:hover {
color: #1565c0;
background: rgba(25, 118, 210, 0.05);
}
.tab.active {
color: #1976d2;
font-weight: 500;
}
.tab.active .tab-underline {
width: 100%;
background: #1976d2;
}
.tab-underline {
height: 2px;
width: 0;
background: transparent;
transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
/* 上传和展示区域样式 */
.upload-content {
padding: 2rem;
background: #fcfdff;
min-height: 400px;
border-top: 1px solid #f0f7ff;
}
.gallery-container {
padding: 1rem 2rem 2rem;
height: 400px;
background: #fcfdff;
border-top: 1px solid #f0f7ff;
overflow-y: auto;
}
.gallery-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 1rem;
}
.gallery-header label {
font-size: 14px;
color: #333;
}
.gallery-header select {
padding: 6px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
background: #fff;
cursor: pointer;
}
.empty-tips,
.loading-tips {
text-align: center;
color: #999;
font-size: 1.2rem;
padding: 4rem 0;
}
.file-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 1rem;
}
.loading-more {
text-align: center;
color: #666;
font-size: 14px;
padding: 1rem 0;
}
.file-card {
background: #fff;
border-radius: 6px;
box-shadow: 0 1px 6px rgba(0, 0, 0, 0.1);
overflow: hidden;
transition: transform 0.2s, box-shadow 0.2s;
}
.file-card:hover {
transform: translateY(-2px);
box-shadow: 0 3px 8px rgba(25, 118, 210, 0.2);
}
.preview-wrapper {
position: relative;
padding-top: 100%;
background: #f8fafd;
}
.preview-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
border-bottom: 1px solid #eee;
}
.file-icon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 2rem;
opacity: 0.6;
}
.file-meta {
padding: 0.8rem;
display: flex;
flex-direction: column;
}
.file-size-status {
display: flex;
justify-content: space-between;
align-items: center;
}
.filename {
font-weight: 500;
color: #333;
font-size: 0.9rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.file-size {
font-size: 0.7rem;
color: #666;
margin: 0.2rem 0;
}
.status-indicator {
display: inline-block;
padding: 0.15rem 0.4rem;
border-radius: 3px;
font-size: 0.65rem;
background: #e8f5e9;
color: #2e7d32;
}
/* 设置弹窗样式 */
.settings-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
}
.settings-content {
background: #fff;
padding: 20px;
border-radius: 8px;
width: 400px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.settings-content h2 {
margin-top: 0;
font-size: 1.2rem;
color: #1976d2;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-size: 14px;
color: #333;
}
.form-group input {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
.form-actions {
display: flex;
justify-content: flex-end;
gap: 10px;
}
.form-actions button {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.form-actions button[type="submit"] {
background: #1976d2;
color: white;
}
.form-actions button[type="button"] {
background: #eee;
color: #333;
}
/* 关于弹窗样式 */
.about-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
}
.about-content {
background: #fff;
padding: 20px;
border-radius: 8px;
width: 400px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
text-align: center;
}
.about-content h2 {
margin-top: 0;
font-size: 1.2rem;
color: #1976d2;
}
.about-content p {
margin: 10px 0;
font-size: 14px;
color: #666;
}
/* 响应式调整 */
@media (max-width: 768px) {
.file-grid {
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
}
.gallery-container {
padding: 1rem;
height: 400px;
}
.upload-container {
width: 95%;
border-radius: 12px;
}
.header-wrapper {
padding: 1rem;
}
.page-title {
font-size: 1.2rem;
}
.tab-bar {
padding: 0 1rem;
overflow-x: auto;
}
.tab {
padding: 12px 20px;
font-size: 13px;
flex-shrink: 0;
}
.upload-content {
padding: 1.5rem;
}
.settings-content,
.about-content {
width: 90%;
}
}
@media (max-width: 480px) {
.tab {
padding: 10px 16px;
}
}
</style>

View File

@@ -0,0 +1,18 @@
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name: 'HomeView',
components: {
HelloWorld
}
}
</script>

View File

@@ -0,0 +1,4 @@
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true
})