实体类有id,name,detail,picture。其中picture是的String类。如何设计Java代码实现这个接口,可以将图片的url传到数据库,代码层次如图,来个大佬帮帮忙吧
,
springboot注解方式上传图片url到数据库
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
java通俗易懵 2021-07-02 18:34关注大概思路就是你本地保存图片,数据库存储你保存图片的地址
/*
博客图片保存绝对地址
*/
@Value("${imgaes.save.path}")
private String filePath;/* 博客映射的相对地址 */ @Value("${imgaes.path.relative}") private String filePathRe; public String imgageUp(MultipartFile file){ /* 文件不能为空 */ if(file.isEmpty()){ System.out.println("文件为空"); return " "; } /* 为映射数据库和保存地址准备 */ String fileName=file.getOriginalFilename();//获取文件名 String suffixName=fileName.substring(fileName.lastIndexOf("."));//获取后缀名 //String filePath="E://Idea workspace//demoweb2//src//main//resources//static//images//image//"; fileName= UUID.randomUUID().toString().substring(0,4)+suffixName;//新图片名 File dest=new File(filePath+fileName); //创建新文件 if(!dest.getParentFile().exists()){ dest.getParentFile().mkdirs(); } try { file.transferTo(dest); //保存图片 System.out.println("file = " + "图片保存成功"); } catch (IOException e) { e.printStackTrace(); } String NewfiLiePath=filePathRe+fileName;//映射到数据库的地址 return NewfiLiePath; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报