Introduction
Spring Boot is a new framework provided by the Pivotal team. It is designed to simplify the initial construction and development process of new Spring applications. The framework uses a specific way to configure it, so that developers no longer need to define boilerplate configurations. In this way, Spring Boot is committed to becoming a leader in the booming rapid application development.
Features
1. Create a standalone Spring application
2. Embed Tomcat, no need to deploy WAR files
3. Simplify Maven configuration
4. Automatically configure Spring
5. Provide production-ready features such as metrics, health checks and external configurations
6. There is absolutely no code generation and no requirements for XML configuration
The following code introduces Spring boot's uploading function, and the specific code is as follows:
@ResponseBody @RequestMapping(path = "/save_photo", method={RequestMethod.POST}) public void addDish(@RequestParam("photos") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = null;// File path String json = ""; if (file!=null) {// Determine whether the uploaded file is empty String type = null;// File type String fileName = file.getOriginalFilename();// The original file name System.out.println("The original file name of the uploaded file:"+fileName); // Determine file type type = fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null; if (type!=null) {// Determine whether the file type is empty if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase()))) { // The root path of the project in the container is actually published in the container String realPath = request.getSession().getServletContext().getRealPath("/"); // Custom file name String trueFileName = String.valueOf(System.currentTimeMillis()) + "." + type; // Set the path path to store image files = realPath+/*System.getProperty("file.separator")+*/trueFileName; System.out.println("Path to store image files:"+path); // Transfer to the specified path file.transferTo(new File(path)); System.out.println("File successfully uploaded to the specified directory"); } json = "{/"res/":1}"; }else { System.out.println("Not the file type we want, please upload it as required"); //return null; json = "{/"res/":0}"; } }else { System.out.println("File type is empty"); //return null; json = "{/"res/":0}"; } }else { System.out.println("No corresponding file found"); json = "{/"res/":0}"; //return null; } response.setContentType("application/json;charset=UTF-8"); response.getWriter().print(json); }The first thing to note is that the parameters need to be added
@RequestParam("photos") MultipartFile fileYour html might be like this
<form action="/save_photo" enctype="multipart/form-data" method="post"><input type="file" name="photos" /> <br> <input type="submit" value="upload" /> </form>
Summarize
The above is a detailed explanation of the uploaded image function example of Spring boot introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!