Next, the file download needs to obtain the download file storage path, here is only manually fill in, if it is in a specific project, you can save the file name and upload the storage path in the database. Then add a file list of the page to show the file name and file path, and then click on the download when the corresponding file name and path to the background operation.
/** * File download, requires file name and file address * * @author: Tuzongxun * @Title: Download * @[email protected] Name * @[email Protected] path * @[email protected] * @[email protected] IOException * @returnResponseEntity <byte[]> * @date APR 28,2016 1:21:32 PM * @throws * * * @RequestMapping (value = "/downloadfile.do") public Responseen tity<byte[]> Download (@RequestParam ("name") string name, @RequestParam ("FilePath") string path) throws IOE xception {File File = new file (path); Httpheaders headers = new Httpheaders (); String fileName = new String (Name.getbytes ("UTF-8"), "iso-8859-1");//In order to solve the problem of Chinese name garbled Headers.setcontentdispositionform Data ("Attachment", fileName); Headers.setcontenttype (Mediatype.application_octet_stream); return new responseentity<byte[]> (Fileutils.readfiletobytearray (file), headers, httpstatus.created); }
HTML page, here is simply fill in the filename and file path, with form form submitted to the background, and then the background will control response in the page pop-up Save file path selection box, of course, here in the background I did not do what processing, if the file does not exist will be error, can be processed accordingly:
File Download: </br> </br> <form action= "./downloadfile.do" style= "border:1px solid Grey;width:auto;" Method= "POST" > File Name: <input type= "text" name= "name"/></br></br> file path: <input type= " Text "Name=" FilePath "/></br></br> <input type=" Submit "value=" Confirm Download "/> </form>
The page view is as follows:
If the file does not exist, the error is as follows:
File download with Springmvc for file upload and download