8年低代码折腾实践,带大家玩转低代码。
同学们在使用低代码平台开发过程中可能会遇到附件上传的问题,但是又不知道使用什么方式来解决,可能您会使用一个工具类来对附件进行上传,但是上传后发现无法解析附件,JEPaaS就提供了一套文件上传、下载的机制,本章就给大家介绍一下JEPaaS如何上传附件,并且保存到数据库中。
一、实现思路
核心:DocumentBusService使用JEPaaS提供的文件管理类,完成对附件的上传和保存。
二、具体操作
1.使用File解析文件
2.转换为FileUpload对象
3.调用saveSingleFile方法保存单个附件
4.将文件key保存到数据库,单附件为文件名*文件key 例如 示例.png*7fFiISr9mnQt3ZTBkMv
多附件为:数组中放对象 对象键有:name文件名,path文件key,id文件key
例如: [{name: '示例.png', path: '7fFiISr9mnQt3ZTBkMv', cls: '', id: '7fFiISr9mnQt3ZTBkMv', extend: ''}]
@Autowired
private PCDynaServiceTemplate serviceTemplate;
@Autowired
private DocumentBusService documentBusService;
public FileBO doUpdateDocFile(String filePath) {
EndUser currentUser= SecurityUserHolder.getCurrentUser();
//1.保存一个文件到业务表字段中
File file=new File(filePath);
FileUpload uploadFile= null;
try {
Path path = new File(file.getName()).toPath();
String mimeType = Files.probeContentType(path);
uploadFile = new FileUpload(file.getName(),mimeType,file.length(),new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FileBO fileBO = null;
if(null != uploadFile){
fileBO = documentBusService.saveSingleFile(uploadFile,currentUser.getUserId());
DynaBean ywBean=serviceTemplate.selectOneByPk("JE_CORE_ENDUSER"," AND USERCODE='admin'");
ywBean.set("PHOTO",fileBO.getRelName() + "*" + fileBO.getFileKey());
ywBean=serviceTemplate.update(ywBean);
//2.删除一个文件
List<String> fileKeys=new ArrayList<>();
fileKeys.add("文件key");
documentBusService.delFilesByKey(fileKeys,currentUser.getUserId());
//3.获取文件
FileBO downloadFileBO=documentBusService.readFile("文件key");
InputStream downloadFile=downloadFileBO.getFile();
}
return fileBO;
}
5.通过返回的fileBo对象,可以获取到附件的名称和JEPaaS储存后的文件key,这时候经过第四步的拼接,就可以保存到数据库当中,这样就完成了附件上传。
三、总结
使用该方法可以做到最简便的开发,开发人员只需要关注自己的业务处理即可,做到真正的低代码开发。