本作品采用 知识共享署名 4.0 国际许可协议
进行许可。
本文作者:低调小熊猫
文章链接: https://aodeng.cc/archives/springbootqi
转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032
单一文件上传
@PostMapping("uploadOne") public String uploadOne(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes){ if(file.isEmpty()){ redirectAttributes.addFlashAttribute("message","请选择上传文件"); return "redirect:uploadShow"; } try { //获取文件并保存 byte[] bytes = file.getBytes(); Path path= Paths.get(UPLOADED_FOLDER+file.getOriginalFilename()); Files.write(path,bytes); redirectAttributes.addFlashAttribute("message",file.getOriginalFilename()+"文件上传完成"); }catch (IOException e){ e.printStackTrace(); } return "redirect:uploadShow"; }
Base64文件上传
@PostMapping("/uploadBase") @ResponseBody public void upload2(String base64) throws IOException { // TODO BASE64 方式的 格式和名字需要自己控制(如 png 图片编码后前缀就会是 data:image/png;base64,) final File tempFile = new File("c://temp//test.jpg");//上传文件保存的路径 // TODO 防止有的传了 data:image/png;base64, 有的没传的情况 String[] d = base64.split("base64,"); final byte[] bytes = Base64Utils.decodeFromString(d.length > 1 ? d[1] : d[0]); FileCopyUtils.copy(bytes, tempFile); }
原文
https://aodeng.cc/archives/springbootqi
本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » SpringBoot(七):文件上传