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/01 Report--
Editor to share with you how to customize the tool class in the Springboot project to achieve the operation of uploading pictures in the background, I believe that most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.
1. Configure the startup class first
Inherit WebMvcConfigurer
Rewriting method
@ SpringBootApplication//@MapperScan ("com.example.demo.Mapper") public class DemoApplication implements WebMvcConfigurer {public static void main (String [] args) {SpringApplication.run (DemoApplication.class, args) } @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {/ * addResoureHandler: refers to the exposed access path addResourceLocations: refers to the directory where the internal files are placed * / registry.addResourceHandler ("/ imctemp-rainy/**"). AddResourceLocations ("file:D:/image");}} 2. Add a UploadUtil file upload utility class public class UploadUtil {/ / source file name private String originalFilename; / / source file suffix private String suffix; / / tomcat virtual path private String dbPath; / / file size private long size; / / actual storage path private String realPath stored in the database / * * File upload tool class * @ param attach * @ param request * @ param uploader File uploader * @ return * / public boolean doUpload (MultipartFile attach, HttpServletRequest request, String uploader) {if (! attach.isEmpty ()) {originalFilename = attach.getOriginalFilename (); System.out.println ("= > uploaded file name:" + originalFilename) Suffix = originalFilename.substring (originalFilename.lastIndexOf ("."); System.out.println ("= = > uploaded file suffix:" + suffix); size = attach.getSize (); System.out.println ("= = > size of uploaded file:" + size); String currentFilename = System.currentTimeMillis () + UUID.randomUUID (). ToString () + suffix System.out.println ("= = > uploaded file name:" + currentFilename); realPath = "D:/image/" + uploader; System.out.println ("= = > the real path where the uploaded file is saved:" + realPath); File targetFile = new File (realPath, currentFilename); if (! targetFile.exists ()) {targetFile.mkdirs () } try {attach.transferTo (targetFile);} catch (Exception e) {e.printStackTrace (); return false;} realPath = realPath + "/" + currentFilename / / dbPath = request.getContextPath () + "/" + uploader + "/" + new SimpleDateFormat ("yyyy-MM-dd") .format (new Date ()) + "/" + currentFilename; dbPath = "/" + uploader + "/" + currentFilename; return true;} else {return false;}} public String getUploadFile () {return dbPath;}}
Among them, all the paths need to be changed to their own paths for storing pictures.
3.Controller layer @ RestControllerpublic class UserPhotoController {@ RequestMapping (value = "/ upload", method = RequestMethod.POST) @ ResponseBody public String testUpload (@ RequestParam ("file") MultipartFile file, HttpServletRequest request) {UploadUtil uploadUtil = new UploadUtil (); String fileName = ""; if (uploadUtil.doUpload (file, request, "uploadImg")) {fileName = uploadUtil.getUploadFile ();} else {fileName = "file" } return fileName;}}
Done.
The above is all the contents of this article entitled "how to customize the tool class in the Springboot project to upload pictures in the background". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.