Java代码
1. InputStream fis = new BufferedInputStream(new FileInputStream(path));
2. response.reset();
3. response.setContentType("text/html;charset=utf-8");
4. request.setCharacterEncoding("UTF-8");
5. response.addHeader("Content-Length", "" + file.length());
6. OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
7. response.setContentType("application/octet-stream");
8. response.setHeader("Content-disposition", "attachment;filename="+ new String(filename.getBytes("utf-8"), "ISO8859-1"));
9. int bytesRead = 0;
10. byte[] buffer = new byte[8192];
11. while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
12. toClient.write(buffer, 0, bytesRead);
13. }
14. toClient.write(buffer);
15. toClient.flush();
16. toClient.close();
17. fis.close();
这种方法试过了,仍然内存溢出
在网上找了半天,才找到解决办法,运行项目。
文件已下载完成。设置的下载目录:C:UsersliuDesktop工程项目
大小一模一样,可以看出文件真的被完整的下载下来了。
DEMO下载地址:https://dwz.cn/Jw3z6fVq