@RequestMapping (value = "/upload", method = requestmethod.post) @Permission (Isajax=false) Public String Upload (httpservletrequest request, httpservletresponse response, @RequestParam ("Files") multipartfile[] files) {
List<string> fileurl=new arraylist<string> ();
try {
if (files.length>0 ) { String uploaddir= "d:/upload/2017-12-18/" ; File dir=new file (uploaddir); if (! Dir.exists ()) {dir.mkdirs (); } for (Multipartfile file:files) {if (! File.isempty ()) {String suffix=file.getoriginalfilename (). substring (File.getoriginalfilename (). LastIndexOf ("." )); String Filename=string.format ("%s%s" , Uuid.randomuuid (). toString (), suffix); File Fileinfo=new file (String.Format ("%s%s" , Uploaddir,filename)); Fileutils.writebytearraytofile (FileInfo, File.getbytes ()); String Url=string.format ("/2017-12-18/%s" , filename); Fileurl.add (URL);}} }
// fileurl picture Address List
Catch
}
Here is the resolution method for the image preview address
@Controller Public classHomeController {Private FinalResourceloader Resourceloader; @Autowired PublicHomeController (Resourceloader resourceloader) { This. Resourceloader =Resourceloader; } @RequestMapping (Method= Requestmethod.get, value = "/{yyyy}-{mm}-{dd}/{filename:.+}") @ResponseBody PublicResponseentity<?>getFile (@PathVariable string yyyy, @PathVariable string MM, @PathVariable string dd, @PathVariable string filename) { Try { returnResponseentity.ok (Resourceloader.getresource ("File:" + paths.get ("d:/upload/" + yyyy+ "-" +mm+ "-" +DD + "/", filename). toString ()); } Catch(Exception e) {returnResponseentity.notfound (). build (); } }}
To access the image address: http://localhost:8080/2017-12-18/956f293b-876e-4347-9fae-29ac290f357b.jpg
Direct access to the jar package, uploaded files, pictures
Java Spring Boot upload file and preview file address resolution