java 用若依框架從app添加數據和圖片


@PostMapping("/addfile")
@ResponseBody
public AjaxResult addSave(@RequestParam(value = "file") MultipartFile[] files, @RequestParam(value = "json") String json)
{
String code = "";
String msg = "";
String url = "";
AnjianLvhua anjianLvhua = JSON.parseObject(json, AnjianLvhua.class);
int a=anjianLvhuaService.insertAnjianLvhua(anjianLvhua);
if(files.length>0) {
try {
for(MultipartFile file : files) {
AnjianLvhuaFile weifaImgFile = new AnjianLvhuaFile();
// 上傳文件路徑
String filePath = RuoYiConfig.getUploadPath();
// 上傳並返回新文件名稱
String fileName = null;
try {
fileName = FileUploadUtils.upload(filePath, file);
} catch (IOException e) {
e.printStackTrace();
}
url = serverConfig.getUrl() + fileName;
//上傳圖片成功以后,開始保存圖片地址到對應的項中
weifaImgFile.setWeifaId(anjianLvhua.getLvhuaId());
weifaImgFile.setFilePath(url);
weifaImgFile.setFileName(fileName);
weifaImgFile.setFileTime(new Date());
int flag = anjianLvhuaFileService.insertAnjianLvhuaFile(weifaImgFile);
}

} catch (Exception e) {
e.printStackTrace();
}
}

} catch (Exception e) {
e.printStackTrace();
}

}
if(a>0){
msg = "提交成功";

code = AjaxResult.SUCCESS_STATUS;
}else{
msg = "提交失敗";
code = AjaxResult.ERROR_STATUS;
}
return new AjaxResult(code, msg, anjianLvhua);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM