1 @Component 2 public class DownLoadUtil { 3 / ** 4 * file download Code 5 * encoding the coding tell the browser file name, in case there garbled file name when downloading Chinese 6 * / 7 Private static String encoding = "UTF -8 " ; . 8 . 9 / ** 10 * file download . 11 * @param Response 12 is * @param path filePath file on the server, including the file name 13 is * / 14 public static void downloads (the HttpServletResponse Response, String filePath) { 15 File = File new new File (filePath.toString ()); 16 downloads (Response, File, null , encoding); . 17 } 18 is . 19 / ** 20 is * Downloads 21 is * @param Response 22 is * @param filePath on the file server the path, including the file name 23 * @param fileName file name to download the browser, you do not want to make the file name on the browser to download the same file name and server, set the parameter 24- * / 25 public static void download (HttpServletResponse Response, filePath String, String fileName) { 26 is File File =new new File (filePath.toString ()); 27 downloads (Response, File, fileName, encoding); 28 } 29 30 / ** 31 is * Download 32 * @param Response 33 is * @param path filePath file on the server, including the file name 34 * @param fileName file name to download the browser, you do not want to make the file name on the browser to download the same file name and server, set the parameter 35 * @param encoding encoding the file name 36 * / 37 public static void downloads (the HttpServletResponse Response, filePath String, String fileName, String encoding) { 38 File file = new File(filePath.toString()); 39 download(response, file, fileName, encoding); 40 } 41 42 /** 43 * 文件下载 44 * @param response 45 * @param file 文件 46 */ 47 public static void download(HttpServletResponse response, File file) { 48 download(response, file, null, encoding); 49 } 50 51 /** 52 * Download 53 * @param the Response 54 * @param File File 55 * @param fileName file name to download the browser, you do not want to make the file name on the browser to download the same file name and server, set the parameter 56 * / 57 is public static void downloads (the HttpServletResponse Response, file file, String fileName) { 58 downloads (Response, file, fileName, encoding); 59 } 60 61 is / ** 62 is * Downloads 63 is * @param Response 64 * @param file file 65 * @param fileName file name to download the browser, you do not want to make the file name on the browser to download the same file name and server, set the parameter 66 * @param encoding encoding the file name 67 * / 68 public static void download (the HttpServletResponse Response, File File, fileName String, String encoding) { 69 IF (File == null || File.Exists () ||! file.isDirectory ()) { 70 return ; 71 is } 72 73 is // if not specified Download the file to the name of the browser, the default file name is used 74 IF (StringUtils.isBlank (fileName)) { 75 fileName =file.getName (); 76 } 77 78 the try { 79 the InputStream IS = new new the FileInputStream (File); 80 downloads (Response, IS, fileName, encoding); 81 } the catch (IOException E) { 82 e.printStackTrace (); 83 } 84 } 85 86 / * 87 * documents 88 * @param Response 89 * @param IS file input stream 90 * @paramfileName the downloaded file name 91 is * @throws IOException 92 * / 93 public static void downloads (the HttpServletResponse Response, the InputStream IS, String fileName) { 94 downloads (Response, IS, fileName, encoding); 95 } 96 97 / ** 98 * Download 99 * @param Response 100 * @param iS file input stream 101 * @param file name fileName downloaded 102 * @param encoding encoding format 103 */ 104 public static void download(HttpServletResponse response, InputStream is, String fileName, String encoding){ 105 if(is == null || StringUtils.isBlank(fileName)){ 106 return; 107 } 108 109 BufferedInputStream bis = null; 110 OutputStream os = null; 111 BufferedOutputStream bos = null; 112 113 try{ 114 bis = new BufferedInputStream(is); 115 os = response.getOutputStream(); 116 bos = new BufferedOutputStream(os); 117 response.setContentType("application/octet-stream;charset=" + encoding); 118 response.setCharacterEncoding(encoding); 119 response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, encoding)); 120 byte[] buffer = new byte[1024]; 121 int len = bis.read(buffer); 122 while(len != -1){ 123 bos.write(buffer, 0, len); 124 len = bis.read(buffer); 125 } 126 127 bos.flush(); 128 }catch(IOException e){ 129 e.printStackTrace(); 130 }finally{ 131 if(bis != null){ 132 try{ 133 bis.close(); 134 }catch(IOException e){} 135 } 136 137 if(is != null){ 138 try{ 139 is.close(); 140 }catch(IOException e){} 141 } 142 } 143 } 144 145 public static String getEncoding() { 146 return encoding; 147 } 148 149 public static void setEncoding(String encoding) { 150 DownLoadUtil.encoding = encoding; 151 } 152 }