java中使用输出流下载文件弹出打开保存框

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

}

    原文作者:赖进杰
    原文地址: https://blog.csdn.net/ljj_9/article/details/74442972
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞