zoukankan      html  css  js  c++  java
  • javaweb下载文件

    //读取文件->写出文件

    public static void main(String[] args) {

     InputStream in =null;

     OutputStream out = null;

    try{

    File file = new File("c:\123.doc");

    in = new FileInputStream(file);

    out = new FileOutputStream("c:\666.doc");

     int len = 0;

     byte buffer[] = new byte[1024];

     while((len=in.read(buffer))>0){

     out.write(buffer, 0, len);

     }

    }catch (Exception e) {

    e.printStackTrace();

    }finally{

    try {

    out.close();

    in.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    //web文件下载

    public void download() {

      InputStream in = null ;

      OutputStream out = null;

      try{

      in = new FileInputStream("c:\123.doc");

      int len =0;

      byte[] buffer = new byte[1024];

      out = getResponse().getOutputStream();

      getResponse().setHeader("Content-Disposition", "attachment;filename=aaa.doc"); //告诉浏览器以什么方式打开文件

      while((len=in.read(buffer))>0){

      out.write(buffer, 0, len);

      }

      }catch (Exception e) {

      e.printStackTrace();

    }

      }

    //web文件下载(文件名称乱码解决)

    public void download() {

      InputStream in = null ;

      OutputStream out = null;

      try{

      in = new FileInputStream("c:\123.doc");

      int len =0;

      byte[] buffer = new byte[1024];

      out = getResponse().getOutputStream();      //将文件写出response的输出流

      getResponse().setContentType("text/html;charset=UTF-8");

      getResponse().setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("作业本.doc","UTF-8"));   //告诉浏览器以什么方式打开文件 和 文件名乱码解决

      while((len=in.read(buffer))>0){

      out.write(buffer, 0, len);

      }

      }catch (Exception e) {

      e.printStackTrace();

    }

      }

  • 相关阅读:
    MySql 应用语句
    MySql 存储过程 退出
    MySql 存储过程 光标只循环一次
    MySql获取两个日期间的时间差
    VM VirtualBox 全屏模式 && 自动缩放模式 相互切换
    区分不同操作系统、编译器不同版本的宏
    debian下配置网络 安装无线网卡驱动 Broadcom BCMXX系列
    YII 主题设置
    Unix线程概念、控制原语、属性
    远程IPC种植木马
  • 原文地址:https://www.cnblogs.com/chenweichu/p/5566100.html
Copyright © 2011-2022 走看看