In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to solve the problem of uploading and saving springboot files. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Upload and save path of springboot file
Recently, we used springboot to integrate the rich text editor wangeditor. During the integration, there were some problems with the save path when the picture was uploaded. The code is as follows
@ PostMapping ("/ upload") public Result upload (MultipartFile [] file, HttpServletRequest request) throws IOException {Result result = new Result (); String path = request.getServletContext () .getRealPath ("/ public/image/upload"); System.out.println (file.length); for (MultipartFile f: file) {String name = f.getOriginalFilename (); f.transferTo (new File (path+File.separator+name)) Result.getData () .add ("/ public/image/upload/" + name);} return result;}
The following exception was reported
Java.io.IOException: java.io.FileNotFoundException: C:\ Users\ 22374\ AppData\ Local\ Temp\
Tomcat-docbase.262090075120668420.9003\ public\ image\ upload\ demo0.png (the system cannot find the specified path.)
I went to the corresponding path to check, there are really no these files under the path, because springboot runs directly as a jar package, which should be the relevant path generated when the jar package runs, and there is no in-depth study of the principle. Here, unlike deployment under tomcat, folders do exist and are readable. Therefore, other options can only be adopted.
When we upload files under tomcat, we will not directly save the uploaded files under the relevant path under the project, because if we save the files in this way, the project will be redeployed and the files will be gone. Typically, tomcat is configured to map a static folder to hold the uploaded files, so that if the project is upgraded and redeployed, the files still exist. The problem now is how to configure static folders using springboot.
The configuration code is as follows: spring: mvc: static-path-pattern: / * resources: static-locations:-classpath:/META-INF/resources/-classpath:/static-classpath:/resources/-file:$ {upload-path} upload-path: F:/Java/upload/
The key code is file:$ {web.upload-path}, which configures a file path as a static resource mapping, and upload-path: F:/Java/upload/ is the actual location of the configuration path. The rest of the code, because it reconfigures the static resource mapping, so the default configuration is invalid, and it needs to be reconfigured.
Of course, in addition to the configuration file configuration, you can also inherit the WebMvcConfigurerAdapter class to configure static resource mapping. of course, this is a lower version of springboot and a higher version of inherited WebMvcConfigurerationSupport implementation.
The problem of Springboot uploading files (uploading to a local folder) first create a controller package
As shown in the figure:
The FileUploadController.java code is as follows:
Package com.zcc.springboot_uploadfiles.controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.UUID;@RestControllerpublic class FileUploadController {SimpleDateFormat sdf = new SimpleDateFormat ("yyy/MM/dd/") @ PostMapping ("/ upload") public String upload (MultipartFile uploadFile, HttpServletRequest req) {String realPath = req.getSession () .getServletContext () .getRealPath ("/ uploadFile/"); String format = sdf.format (new Date ()); String file = realPath + format; File folder = new File (realPath + format); if (! folder.isDirectory ()) {folder.mkdirs () } String oldName = uploadFile.getOriginalFilename (); String newName = UUID.randomUUID (). ToString () + oldName.substring (oldName.lastIndexOf ("."), oldName.length (); try {/ / file save operation uploadFile.transferTo (new File (folder, newName)); return file } catch (IOException e) {e.printStackTrace ();} return "upload failed!";}}
Here we uploaded it to the default folder.
Mine is
Under this folder.
Of course, you can also put it under the file you want, and you need to change the above code ():
/ / file here is the folder I want String file = "E:\\ IDEA01\\ learn springboot\\ springboot_uploadfiles\\ src\\ main\\ resources\\ static\\ uploadFile"; File folder = new File (file)
The uploaded file will appear.
The static resource directory is as follows
Because the main purpose is to demonstrate how to upload files, so HTML files are relatively crude.
On "how to solve the problem of springboot file upload and save path" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.