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

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();

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-12-05
  • 2021-07-27
  • 2021-10-09
  • 2022-02-07
  • 2022-01-16
猜你喜欢
  • 2021-09-14
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-12-06
  • 2022-01-19
相关资源
相似解决方案