简体   繁体   English

在Spring Boot中处理上传的文件

[英]Handling uploaded files in Spring Boot

I am making a spring boot application and I want to be able to upload image files in a form, save them somewhere on the server and the display these images. 我正在制作一个Spring Boot应用程序,我希望能够以一种形式上载图像文件,将它们保存在服务器上的某个位置并显示这些图像。 I managed to get the files using MultipartFile into my controller, however I am not sure how to save the files locally so that I can use them later. 我设法使用MultipartFile将文件获取到控制器中,但是我不确定如何在本地保存文件,以便以后使用。

I am guessing I have to somehow save the files in the resources directory so I can access them in my views. 我猜我必须以某种方式将文件保存在资源目录中,以便可以在视图中访问它们。 I will also be hosting my application on heroku and I am not sure what kind of access to the file system I have there. 我还将在Heroku上托管我的应用程序,但不确定我对那里的文件系统有什么样的访问权限。

Can somebody point me towards a solution? 有人可以指出我的解决方案吗?

一旦有了MultipartFile,就可以使用FileCopyUtils.copy静态方法将其上传到指定目录:

FileCopyUtils.copy(multipartFile.getBytes(), new File("src/main/resources"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM