需求:把每天产生的日志文件,从服务器上下载下来
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();