文件上传接口保留path参数,方便覆盖文件
This commit is contained in:
parent
20411fa6b5
commit
250db847f6
@ -14,16 +14,28 @@ public interface FileApi {
|
||||
* @return 文件路径
|
||||
*/
|
||||
default String createFile(byte[] content) throws Exception {
|
||||
return createFile(null, content);
|
||||
return createFile(null, null, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件,并返回文件的访问路径
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
default String createFile(String path, byte[] content) throws Exception {
|
||||
return createFile(null, path, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件,并返回文件的访问路径
|
||||
*
|
||||
* @param name 原文件名称
|
||||
* @param path 文件路径
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
String createFile(String name, byte[] content) throws Exception;
|
||||
String createFile(String name, String path, byte[] content) throws Exception;
|
||||
|
||||
}
|
||||
|
@ -19,8 +19,8 @@ public class FileApiImpl implements FileApi {
|
||||
private FileService fileService;
|
||||
|
||||
@Override
|
||||
public String createFile(String name, byte[] content) throws Exception {
|
||||
return fileService.createFile(name, content);
|
||||
public String createFile(String name, String path, byte[] content) throws Exception {
|
||||
return fileService.createFile(name, path, content);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,11 +40,13 @@ public class FileController {
|
||||
@PostMapping("/upload")
|
||||
@ApiOperation("上传文件")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class)
|
||||
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
|
||||
@ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
|
||||
})
|
||||
@OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
|
||||
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
|
||||
return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream())));
|
||||
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(value = "path", required = false) String path) throws Exception {
|
||||
return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
|
@ -23,10 +23,11 @@ public interface FileService {
|
||||
* 保存文件,并返回文件的访问路径
|
||||
*
|
||||
* @param name 原文件名称
|
||||
* @param path 文件路径
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
String createFile(String name, byte[] content) throws Exception;
|
||||
String createFile(String name, String path, byte[] content) throws Exception;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
|
@ -37,10 +37,12 @@ public class FileServiceImpl implements FileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createFile(String name, byte[] content) throws Exception {
|
||||
public String createFile(String name, String path, byte[] content) throws Exception {
|
||||
// 计算默认的 path 名
|
||||
String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
|
||||
String path = DigestUtil.md5Hex(content) + '.' + type;
|
||||
if (StrUtil.isEmpty(path)) {
|
||||
path = DigestUtil.md5Hex(content) + '.' + type;
|
||||
}
|
||||
|
||||
// 上传到文件存储器
|
||||
FileClient client = fileConfigService.getMasterFileClient();
|
||||
|
@ -82,7 +82,7 @@ public class FileServiceTest extends BaseDbUnitTest {
|
||||
when(client.getId()).thenReturn(10L);
|
||||
String name = "单测文件名";
|
||||
// 调用
|
||||
String result = fileService.createFile(name, content);
|
||||
String result = fileService.createFile(name, path, content);
|
||||
// 断言
|
||||
assertEquals(result, url);
|
||||
// 校验数据
|
||||
|
@ -161,7 +161,7 @@ export default {
|
||||
},
|
||||
/** 处理上传的文件发生变化 */
|
||||
handleFileChange(file, fileList) {
|
||||
this.upload.data.path = file.name;
|
||||
|
||||
},
|
||||
/** 处理文件上传中 */
|
||||
handleFileUploadProgress(event, file, fileList) {
|
||||
|
Loading…
Reference in New Issue
Block a user