The simplest way to download a Web-based file is to download it directly in HTML as a tag, just assign the full path of the file to the href, and give the download attribute a file name. But some browsers (Chrome, Firefox, IE edge, etc.) can be downloaded, some browsers (IE8-11, etc.) are opened directly on the Web page, and sometimes garbled. So in order to completely solve this, we need to find a solution on the server side. In the spring framework, when downloading via the href link, set the servlet to intercept the corresponding URL and enter the background for the appropriate input. The sample code is as follows: public void FileOutputStream (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {String filepath = Req.getrequesturi (); int index = Filepath.indexof (Global.userfiles_base_url); if (Index &G t;= 0) {filepath = filepath.substring (index + Global.USERFILES_BASE_URL.length ());} try {filepath = Uriutils.decode (fil Epath, "UTF-8"); } catch (Unsupportedencodingexception E1) {logger.error ("interpreted file path failed, URL address is%s", filepath), E1);} File File = new file (Global.getuserfilesbasedir () + Global.userfiles_base_url + filepath); try {resp.setcontenttype ("application/x-msdownload; Charset=utf-8 "); Resp.setheader ("Content-disposition", "attachment;"); Filecopyutils.copy (new FileInputStream (file), Resp.getoutputstream ()); ReturN } catch (FileNotFoundException e) {Req.setattribute ("exception", New FileNotFoundException ("The file requested does not exist"); Req.getrequestdispatcher ("/web-inf/views/error/404.jsp"). Forward (req, resp); }} Key code: Resp.setheader ("Content-disposition", "attachment;"); This specifies that the output is an attachment. In this case, it is clear to tell the browser that this is the download attachment, which solves the problem that different browsers may appear directly to open files or even garbled characters. For Tomcat, view conf/ Web. XML, looking for all MIME types, if there is a file you need to download, that is, do not have to modify, if not exist, need to add, and then restart the server, give a tag to download the full path of the file (such as the XLS file does not have to modify the configuration, and RAR needs to be configured, ZIP does not need to , if the path contains Chinese words, but also need a slight change, otherwise it may not be downloaded, need in the Server.xml file, in the HTTP port settings, need to add code, as follows: <connector port= "8080" protocol= "http/ 1.1 "connectiontimeout=" 20000 "redirectport=" 8443 "uriencoding=" UTF-8 "/> Note uriencoding assigned to UTF-8. Because of the code requested by the Get method, if Chinese is present in the file path, garbled characters may occur. The encoding for post requests can be configured in a traditional request.setcontent** manner.
A label for HTML cannot download a file solution