Java Spring Boot upload file and preview file address resolution

Source: Internet
Author: User

    @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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.