Commit ed7aec49 authored by 马超's avatar 马超

feat(基于LDP开发文档): 添加上传下载文件章节

parent e749d30a
...@@ -1359,6 +1359,102 @@ Controller ...@@ -1359,6 +1359,102 @@ Controller
``` ```
### 5.7 文件上传与下载
#### 5.7.1 IRepoFileService介绍
IRepoFileService是LDP框架提供的一个文件上传下载的服务,文件存放路径在基础服务配置文件中
```yml
ldp:
query:
location: query/*.xml
dialect: mysql
# 上传文件存放地址
file:
home-dir: .ldp
file-dir: repo
```
#### 5.7.2 文件上传
使用 **org.apache.dubbo.config.annotation** 包中的 **@Reference** 注解,调用upload方法后,获取到一个 **ProcessResult** 对象,判断upload是否正常执行result.isExecute(),以下是代码案例:
```java
/**
* 文件上传下载服务
*/
@Reference
IRepoFileService repoFileService;
/**
* 上传文件
*
* @param file
* @return 文件ID
*/
@Override
public String upload(MultipartFile file) {
try {
//上传后获取到ProcessResult
ProcessResult<String> result = repoFileService.upload(file.getOriginalFilename(), file.getBytes());
//判断是否上传成功
if (result.isExecute()) {
return result.getResultData();
} else {
logger.error(result.getStackTraceString());
throw new RuntimeException(result.getMessage());
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return "";
}
```
#### 5.7.3 文件下载
下载接口有2个方法,一个是只下载文件数据,另外一个是包含文件的其它信息(文件名、格式等),同样是获取到 **ProcessResult** 对象,后续判断后进行拆包,拿到下载后的数据。
```java
/**
* 下载文件
*
* @param fileId 文件ID
* @return 文件数据
*/
@Override
public LdpSysFileResource download(String fileId) {
// 仅包含文件数据
//ProcessResult<byte[]> result = repoFileService.download(fileId);
//包含文件名、文件类型、文件数据
ProcessResult<LdpSysFileResource> result = repoFileService.downloadFileResource(fileId);
if (result.isExecute()) {
return result.getResultData();
} else {
logger.error(result.getStackTraceString());
throw new RuntimeException(result.getMessage());
}
}
```
#### 5.7.4 文件删除
```java
/**
* 删除文件
*/
@Override
public boolean delFile(String fileId) {
ProcessResult result = repoFileService.del(fileId);
return result.isExecute();
}
```
## 六、子模块创建 ## 六、子模块创建
### 6.1. 子工程创建步骤 ### 6.1. 子工程创建步骤
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment