简体   繁体   中英

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

My application is writtent in Spring boot. I follow this tutorial https://spring.io/guides/gs/uploading-files/ to write an endpoint to upload image.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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