java浏览器下载

 2023-09-05 阅读 75 评论 0

摘要:JAVA读取指定路径下文件,实现模拟浏览器下载文件,核心代码如下: /*** 浏览器下载** @param sourceFilePath 源文件路径:"D:\\demo.txt"* @param fileName 下载生成的文件名* @param response 响应浏览器*/public void browse

JAVA读取指定路径下文件,实现模拟浏览器下载文件,核心代码如下:

/*** 浏览器下载** @param sourceFilePath 源文件路径:"D:\\demo.txt"* @param fileName       下载生成的文件名* @param response       响应浏览器*/public void browserDownload(String sourceFilePath, String fileName, HttpServletResponse response) {try (InputStream is = new FileInputStream(sourceFilePath);OutputStream os = response.getOutputStream();BufferedInputStream bis = new BufferedInputStream(is);BufferedOutputStream bos = new BufferedOutputStream(os)) {response.setContentType("application/octet-stream");response.setHeader("content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), StandardCharsets.ISO_8859_1));int length = 0;byte[] temp = new byte[1024 * 10];while ((length = bis.read(temp)) != -1) {bos.write(temp, 0, length);}} catch (IOException e) {e.printStackTrace();}}

 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/7.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息