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,這時候經過第四步的拼接,就可以儲存到資料庫當中,這樣就完成了附件上傳。
三、總結
使用該方法可以做到最簡便的開發,開發人員隻需要關注自己的業務處理即可,做到真正的低代碼開發。