try {
File file = new File(“D://test.tar”);
InputStream is = new FileInputStream(file);
response.reset(); // 必要地清除response中的缓存信息
response.setHeader(“Content-Disposition”, “attachment; filename=” + fileName);// 在浏览器提示用户是保存还是下载
response.setContentType(“application/octet-stream; charset=UTF-8”);// 根据个人需要,这个是下载文件的类型
response.setHeader(“Content-Length”, String.valueOf(file.length()));// 告诉浏览器下载文件的大小
OutputStream out = response.getOutputStream();
byte[] content = new byte[1024];
int length = 0;
while ((length = is.read(content)) != -1) {
out.write(content, 0, length);
}
out.write(content);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}