Importing a Configuration File

UploadFileConfig class

@Configuration public class UploadFileConfig { @Value("${file.uploadFolder}") private String uploadFolder; @Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setLocation(uploadFolder); SetMaxFileSize ("20MB"); return factory.createMultipartConfig(); }}Copy the code

UploadFilePathConfig class

@Configuration public class UploadFilePathConfig extends WebMvcConfigurerAdapter { @Value("${file.staticAccessPath}") private String staticAccessPath; @Value("${file.uploadFolder}") private String uploadFolder; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder); }}Copy the code

Add the passed configuration items to application.properties

File. StaticAccessPath =/upload/** # The file upload directory needs to be changed to your own actual directory File. UploadFolder = / Users/leichunhong/Documents/HLP/zx # upload files of local domain name Rewritten into your backend service address and port of the file, upload the nama = http://127.0.0.1:9090 # # springBOOt upload file size of a single file spring. The maximum servlet. Multipart. Max - file - size = total maximum 10 MB # upload file spring.servlet.multipart.max-request-size=100MBCopy the code

Write the upload Controller

@RestController @RequestMapping("/test") public class UploadController { @Value("${file.uploadFolder}") private String uploadFolder; @Value("${file.staticAccessPath}") private String staticAccessPath; @Value("${file.upload.nama}") private String reurl; @RequestMapping(value = "/upload", method = {RequestMethod.POST, RequestMethod.GET}) public List<String> upload(HttpServletRequest request) throws MultipartException { String path = "";  List<String> ksfs = new ArrayList<>(); // Initialize the current context to CommonsMutipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); / / check whether there is in the form enctype = "multipart/form - the data" if (multipartResolver. IsMultipart (request)) {/ / request will become part of the request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; / / get all file names in the multiRequest Iterator < String > iter. = multiRequest getFileNames (); List<MultipartFile> fileList = multirequest.getFiles (iter. Next()); for (MultipartFile multipartFile : fileList) { String realPath = uploadFolder; String trueFileName = System.currentTimeMillis() + "." + FilenameUtils.getExtension(multipartFile.getOriginalFilename()); Path = realPath + trueFileName; File file = new File(path); multipartFile.transferTo(file); path = reurl + "/upload/" + trueFileName; ksfs.add(path); } // Single file /* MultipartFile MultipartFile = multirequest.getFile (ite.next ().toString()); if (multipartFile ! = null) { excelFile = File.createTempFile(prefix, ".jpg"); multipartFile.transferTo(excelFile); path = KSFileSave.uploadFile(prefix, excelFile, "jpg"); ksfs.add(path); }*/ } } } catch (Exception e) { e.printStackTrace(); } finally { } return ksfs; }}Copy the code

Call the interface test with your own HTML page

The HTML page is as follows:

< form method = "POST" enctype = "multipart/form - the data" action = "HTTP: 127.0.0.1:9090 / test/upload" > < table > < tr > < td > File to upload:</td><td><input type="file" name="file" /></td></tr> <tr><td></td><td><input type="submit" value="Upload" /></td></tr> </table> </form>Copy the code

Open the HTML as follows:

Click Select File upload to verify return results

Viewing the Upload Directory

Click return interface access

After the