mirror of https://gitee.com/stylefeng/roses
【8.3.0】【file】更新多文件上传的方法
parent
9d4f773c6a
commit
fdc1a5b3c8
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.file.modular.controller;
|
||||
|
||||
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
|
||||
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
|
||||
import cn.stylefeng.roses.kernel.file.modular.service.MultipleFileService;
|
||||
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
|
||||
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多文件上传和下载的接口
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 9:00
|
||||
*/
|
||||
@RestController
|
||||
@ApiResource(name = "多文件上传和下载的接口")
|
||||
public class MultipleFileController {
|
||||
|
||||
@Resource
|
||||
private MultipleFileService multipleFileService;
|
||||
|
||||
/**
|
||||
* 多文件上传,并同时携带json的body体
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 9:06
|
||||
*/
|
||||
@PostResource(name = "多文件上传-带body体", path = "/sysFileInfo/multipleUploadFileList")
|
||||
public ResponseData<List<SysFileInfoResponse>> upload(@RequestPart("fileList") MultipartFile[] fileList,
|
||||
@RequestPart("bodyJson") SysFileInfoRequest sysFileInfoRequest) {
|
||||
List<SysFileInfoResponse> fileResult = this.multipleFileService.batchUploadFile(fileList, sysFileInfoRequest);
|
||||
return new SuccessResponseData<>(fileResult);
|
||||
}
|
||||
|
||||
}
|
|
@ -41,6 +41,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.stylefeng.roses.kernel.file.api.constants.FileConstants.FILE_POSTFIX_SEPARATOR;
|
||||
|
||||
|
@ -52,6 +54,20 @@ import static cn.stylefeng.roses.kernel.file.api.constants.FileConstants.FILE_PO
|
|||
*/
|
||||
public class FileInfoFactory {
|
||||
|
||||
/**
|
||||
* 批量创建上传文件的信息
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 12:32
|
||||
*/
|
||||
public static Map<MultipartFile, SysFileInfo> batchCreateFileInfo(MultipartFile[] fileList, SysFileInfoRequest sysFileInfoRequest) {
|
||||
Map<MultipartFile, SysFileInfo> result = new HashMap<>();
|
||||
for (MultipartFile multipartFile : fileList) {
|
||||
result.put(multipartFile, createSysFileInfo(multipartFile, sysFileInfoRequest));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件信息
|
||||
*
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.file.modular.service;
|
||||
|
||||
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
|
||||
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多文件上传和下载的业务
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 9:02
|
||||
*/
|
||||
public interface MultipleFileService {
|
||||
|
||||
/**
|
||||
* 批量上传文件
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 12:05
|
||||
*/
|
||||
List<SysFileInfoResponse> batchUploadFile(MultipartFile[] fileList, SysFileInfoRequest sysFileInfoRequest);
|
||||
|
||||
}
|
|
@ -159,4 +159,20 @@ public interface SysFileInfoService extends IService<SysFileInfo>, FileInfoApi {
|
|||
*/
|
||||
List<SysFileInfoResponse> getFileInfoListByFileIds(List<Long> fileIdList);
|
||||
|
||||
/**
|
||||
* 存储文件到本地或者云
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 12:38
|
||||
*/
|
||||
void storageFile(MultipartFile file, SysFileInfo sysFileInfo);
|
||||
|
||||
/**
|
||||
* 获取文件的响应信息
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 12:43
|
||||
*/
|
||||
SysFileInfoResponse getFileInfoResponse(SysFileInfo sysFileInfo);
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package cn.stylefeng.roses.kernel.file.modular.service.impl;
|
||||
|
||||
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
|
||||
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
|
||||
import cn.stylefeng.roses.kernel.file.modular.entity.SysFileInfo;
|
||||
import cn.stylefeng.roses.kernel.file.modular.factory.FileInfoFactory;
|
||||
import cn.stylefeng.roses.kernel.file.modular.service.MultipleFileService;
|
||||
import cn.stylefeng.roses.kernel.file.modular.service.SysFileInfoService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 多文件上传和下载的业务
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/8/20 9:03
|
||||
*/
|
||||
@Service
|
||||
public class MultipleFileServiceImpl implements MultipleFileService {
|
||||
|
||||
@Resource
|
||||
private SysFileInfoService sysFileInfoService;
|
||||
|
||||
@Override
|
||||
public List<SysFileInfoResponse> batchUploadFile(MultipartFile[] fileList, SysFileInfoRequest sysFileInfoRequest) {
|
||||
|
||||
// 1. 创建文件基本信息
|
||||
Map<MultipartFile, SysFileInfo> multipartFileSysFileInfoMap = FileInfoFactory.batchCreateFileInfo(fileList, sysFileInfoRequest);
|
||||
Collection<SysFileInfo> sysFileInfos = multipartFileSysFileInfoMap.values();
|
||||
this.sysFileInfoService.saveBatch(sysFileInfos);
|
||||
|
||||
// 2. 进行批量保存文件
|
||||
for (Map.Entry<MultipartFile, SysFileInfo> entry : multipartFileSysFileInfoMap.entrySet()) {
|
||||
MultipartFile file = entry.getKey();
|
||||
SysFileInfo fileInfo = entry.getValue();
|
||||
this.sysFileInfoService.storageFile(file, fileInfo);
|
||||
}
|
||||
|
||||
// 3. 获取文件信息
|
||||
List<SysFileInfoResponse> fileInfoResponses = new ArrayList<>();
|
||||
for (SysFileInfo sysFileInfo : sysFileInfos) {
|
||||
SysFileInfoResponse fileInfoResponse = sysFileInfoService.getFileInfoResponse(sysFileInfo);
|
||||
fileInfoResponses.add(fileInfoResponse);
|
||||
}
|
||||
return fileInfoResponses;
|
||||
}
|
||||
|
||||
}
|
|
@ -146,34 +146,10 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
|
|||
this.save(sysFileInfo);
|
||||
|
||||
// 存储文件到云或者本地
|
||||
try {
|
||||
byte[] bytes = file.getBytes();
|
||||
|
||||
// 如果是存在数据库库里,单独处理
|
||||
if (FileLocationEnum.DB.getCode().equals(sysFileInfoRequest.getFileLocation())) {
|
||||
sysFileStorageService.saveFile(sysFileInfo.getFileId(), bytes);
|
||||
} else {
|
||||
fileOperatorApi.storageFile(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(), bytes);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new FileException(FileExceptionEnum.ERROR_FILE, e.getMessage());
|
||||
}
|
||||
this.storageFile(file, sysFileInfo);
|
||||
|
||||
// 返回文件信息体
|
||||
SysFileInfoResponse fileUploadInfoResult = new SysFileInfoResponse();
|
||||
BeanUtil.copyProperties(sysFileInfo, fileUploadInfoResult);
|
||||
|
||||
// 拼接文件可直接访问的url
|
||||
String fileAuthUrl;
|
||||
if (YesOrNotEnum.Y.getCode().equals(sysFileInfoRequest.getSecretFlag())) {
|
||||
fileAuthUrl = fileOperatorApi.getFileAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(),
|
||||
FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
|
||||
} else {
|
||||
fileAuthUrl = fileOperatorApi.getFileUnAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName());
|
||||
}
|
||||
fileUploadInfoResult.setFileUrl(fileAuthUrl);
|
||||
|
||||
return fileUploadInfoResult;
|
||||
return getFileInfoResponse(sysFileInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -624,6 +600,39 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
|
|||
return fileUploadInfoResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storageFile(MultipartFile file, SysFileInfo sysFileInfo) {
|
||||
try {
|
||||
byte[] bytes = file.getBytes();
|
||||
|
||||
// 如果是存在数据库库里,单独处理
|
||||
if (FileLocationEnum.DB.getCode().equals(sysFileInfo.getFileLocation())) {
|
||||
sysFileStorageService.saveFile(sysFileInfo.getFileId(), bytes);
|
||||
} else {
|
||||
fileOperatorApi.storageFile(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(), bytes);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new FileException(FileExceptionEnum.ERROR_FILE, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysFileInfoResponse getFileInfoResponse(SysFileInfo sysFileInfo) {
|
||||
SysFileInfoResponse fileUploadInfoResult = new SysFileInfoResponse();
|
||||
BeanUtil.copyProperties(sysFileInfo, fileUploadInfoResult);
|
||||
|
||||
// 拼接文件可直接访问的url
|
||||
String fileAuthUrl;
|
||||
if (YesOrNotEnum.Y.getCode().equals(sysFileInfo.getSecretFlag())) {
|
||||
fileAuthUrl = fileOperatorApi.getFileAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(),
|
||||
FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
|
||||
} else {
|
||||
fileAuthUrl = fileOperatorApi.getFileUnAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName());
|
||||
}
|
||||
fileUploadInfoResult.setFileUrl(fileAuthUrl);
|
||||
return fileUploadInfoResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染被预览的文件到servlet的response流中
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue