java 实现文件下载

需求:把每天产生的日志文件,从服务器上下载下来

File file = new File(path); // 根据路径,获取File
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()]; // 一次读取整个文件
fis.read(buffer); fis.close(); response.reset(); // 清空response // 文件名去掉空格,解决中文乱码问题

response.addHeader(
"Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
response.addHeader(
"Content-Length", "" + file.length()); OutputStream os = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); os.write(buffer); os.flush(); os.close();

 

posted @ 2013-09-18 17:11  周雷  阅读(391)  评论(0编辑  收藏  举报
友情链接