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();}}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态