A. Outils
emballer com.ahtcm.util; importation javax.servlet.http.HttpServletRequest; importation java.io.UnsupportedEncodingException; publique classe BrowserEncodeSwitchUtil { publique statique Chaîne getContentDisposition (String fileName, HttpServletRequest demande) plaids UnsupportedEncodingException { chaîne content_disposition = "" ; Chaîne userAgent = request.getHeader ( "User-Agent" ); si (userAgent.contains ( "Safari" )) { byte [] bytes = fileName.getBytes ( "UTF-8" ); nom de fichier= Nouvelle chaîne de caractères (octets, "ISO-8859-1" ); content_disposition = String.format ( "fixation; filename = \" % s \ "" , filename); } Autre { fileName = java.net.URLEncoder.encode (fileName, "UTF-8" ); content_disposition = "fixation; filename =" + fileName; } Retour content_disposition; } }
II. Cette méthode est appelée
@Override publique vide downloadExcelTpl (demande HttpServletRequest, HttpServletResponse la réponse) { FileInputStream IS = null ; try { chaîne fileName = ".xls modèle de dossier médical d'importation résident" ; Chaîne ContentDisposition = BrowserEncodeSwitchUtil.getContentDisposition (fileName, demande); response.setHeader ( « Contenu -disposition "ContentDisposition); / * obtenir le chemin de fichier * / String realpath = request.getSession () getServletContext () getRealPath (.." / static / modèle d'importation résidents en médecine .xls " ); / * 读取文件* / est = nouveau FileInputStream (realpath); IOUtils.copy (est, response.getOutputStream ()); } Captures (Exception e) { e.printStackTrace (); } Enfin { si (est =! Null ) { try { is.close (); } Captures (IOException e) { e.printStackTrace (); } } } }