// filePath是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
// 用available()方法来获取文件大小,进而用来定义缓冲数组的长度
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
// response.reset();
response.setCharacterEncoding(CharEncoding.UTF_8);
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"),"ISO-8859-1"));//防止中文乱码
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
原文:https://www.cnblogs.com/wangSoft/p/14637108.html