简易的springboot上传头像(图片)代码片段

编程入门 行业动态 更新时间:2024-10-21 20:36:57

<a href=https://www.elefans.com/category/jswz/34/1769136.html style=简易的springboot上传头像(图片)代码片段"/>

简易的springboot上传头像(图片)代码片段

 /*** 上传头像** @throws IOException* @throws IllegalStateException*/public String upload(MultipartFile file) throws IllegalStateException, IOException {File dir = new File(rootpath);if (!dir.exists()) {dir.mkdirs();}//读取文件名String fileName = file.getOriginalFilename();if (!StringUtil.isEmpty(fileName)) {//获取文件的后缀String suffix = FilenameUtils.getExtension(fileName);//UUID.randomUUID().toString()是javaJDK提供的一个自动生成主键的方法,生成唯一的标识//修改后的文件名(带后缀)String newFileName = UUID.randomUUID().toString().toLowerCase() + "." + suffix;//public File(File parent, String child)--->dir就是相当于这个文件的路径(不包含这个文件的文件名),newFileName指的是文件名;File targetFile = new File(dir, newFileName);//将文件提交到目的地文件系统中file.transferTo(targetFile);System.out.println(newFileName + "mmm");//图片上传以后,“\\”要变成“/”、而且前面的根路径也不需要了;String imgpath = targetFile.getPath().replace("\\", "/").replace(rootpath, "");System.out.println(imgpath);return imgpath;} else {return null;}}

更多推荐

简易的springboot上传头像(图片)代码片段

本文发布于:2024-02-12 16:05:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1688479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简易   片段   头像   上传   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!