zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    字符串案例1
    字符串1
    标准类制作
    构造方法
    封装
    成员变量和局部变量
    类和对象的案例

    案例
    方法的参数传递
  • 原文地址:https://www.cnblogs.com/zhouyalei/p/3328759.html
Copyright © 2011-2022 走看看