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.
[Java]View PlainCopy
- /**
- * 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 responseentity<byte[]> Download (@RequestParam ("name") String name,
- @RequestParam ("FilePath") String path) throws IOException {
- 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.setcontentdispositionformdata ("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:
[HTML]View PlainCopy
- 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