1 @Component 2 public class DownLoadUtil { 3 /** 4 * 文件下载编码 5 * 该编码告诉浏览器文件名的编码方式,以防下载中文文件名时有乱码 6 */ 7 private static String encoding = "utf-8"; 8 9 /** 10 * 文件下载 11 * @param response 12 * @param filePath 文件在服务器上的路径,包含文件名 13 */ 14 public static void download(HttpServletResponse response, String filePath){ 15 File file = new File(filePath.toString()); 16 download(response, file, null, encoding); 17 } 18 19 /** 20 * 文件下载 21 * @param response 22 * @param filePath 文件在服务器上的路径,包括文件名称 23 * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 24 */ 25 public static void download(HttpServletResponse response, String filePath, String fileName){ 26 File file = new File(filePath.toString()); 27 download(response, file, fileName, encoding); 28 } 29 30 /** 31 * 文件下载 32 * @param response 33 * @param filePath 文件在服务器上的路径,包括文件名称 34 * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 35 * @param encoding 文件名称编码 36 */ 37 public static void download(HttpServletResponse response, String filePath, 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 * 文件下载 53 * @param response 54 * @param file 文件 55 * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 56 */ 57 public static void download(HttpServletResponse response, File file, String fileName) { 58 download(response, file, fileName, encoding); 59 } 60 61 /** 62 * 文件下载 63 * @param response 64 * @param file 文件 65 * @param fileName 文件下载到浏览器的名称,如果不想让浏览器下载的文件名称和服务器上的文件名称一样,请设置该参数 66 * @param encoding 文件名称编码 67 */ 68 public static void download(HttpServletResponse response, File file, String fileName, String encoding) { 69 if(file == null || !file.exists() || file.isDirectory()){ 70 return; 71 } 72 73 // 如果不指定文件下载到浏览器的名称,则使用文件的默认名称 74 if (StringUtils.isBlank(fileName)) { 75 fileName = file.getName(); 76 } 77 78 try { 79 InputStream is = new FileInputStream(file); 80 download(response, is, fileName, encoding); 81 } catch (IOException e) { 82 e.printStackTrace(); 83 } 84 } 85 86 /** 87 * 文件下载 88 * @param response 89 * @param is 文件输入流 90 * @param fileName 下载的文件名称 91 * @throws IOException 92 */ 93 public static void download(HttpServletResponse response, InputStream is, String fileName){ 94 download(response, is, fileName, encoding); 95 } 96 97 /** 98 * 文件下载 99 * @param response100 * @param is 文件输入流101 * @param fileName 下载的文件名称102 * @param encoding 编码格式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 }