1.你得有个文件(File)对象
File file=new File(“你得路径”);
2.获取到HttpServletResponse对象并设置属性
response.setHeader(“conent-type”, “application/octet-stream”);
response.setContentType(“application/octet-stream”);
response.setHeader(“Content-Disposition”, “attachment; filename=”+template.getName());
3.输出流
OutputStream os;
try {
os = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
InputStream is;
is = new FileInputStream(template);
BufferedInputStream bis = new BufferedInputStream(is);
int length;
byte[] temp = new byte[1 * 1024 * 10];
while ((length = bis.read(temp)) != -1) {
bos.write(temp, 0, length);
}
bos.flush();
bis.close();
bos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}