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