java web下载文件

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();
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20154251/article/details/80338111
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭