简体   繁体   English

如何防止MultipartFile在Spring Boot中自动保存到本地目录中?

[英]How to prevent MultipartFile to be automatically saved into local directory in Spring Boot?

My application is writtent in Spring boot. 我的应用程序是在Spring Boot中编写的。 I follow this tutorial https://spring.io/guides/gs/uploading-files/ to write an endpoint to upload image. 我按照本教程https://spring.io/guides/gs/uploading-files/编写了一个端点来上传图像。

I was able to receive the image . 我能够收到图像。 All I want to do is to upload it to my own storage in another host and I also can do this. 我要做的就是将其上传到另一台主机上自己的存储中,我也可以这样做。 But I don't understand why the file is automatically saved in my project's directory. 但是我不明白为什么文件会自动保存在我的项目目录中。 The directory will always contain images (sample.png , sample1.png) like this and it's not good on the production server. 该目录将始终包含这样的图像(sample.png,sample1.png),并且在生产服务器上效果不佳。

The directory looks like this. 目录如下所示。

在此处输入图片说明

How can i prevent this?. 我该如何预防?

If you simply copied the source code of the guide on Spring's website, then that's the expected behaviour since that's what the code in the guide does. 如果您只是在Spring网站上复制了该指南的源代码,那么这就是预期的行为,因为这就是该指南中的代码。

BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(Application.ROOT + "/" + name)));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();

You need to pass a different path to FileOutputStream where the file will be written. 您需要将另一个路径传递到FileOutputStream ,以将文件写入文件。

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

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