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

     1 public HttpServletResponse download(String path, HttpServletResponse response) {
     2         try {
     3             // path是指欲下载的文件的路径。
     4             File file = new File(path);
     5             // 取得文件名。
     6             String filename = file.getName();
     7             // 取得文件的后缀名。
     8             String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
     9 
    10             // 以流的形式下载文件。
    11             InputStream fis = new BufferedInputStream(new FileInputStream(path));
    12             byte[] buffer = new byte[fis.available()];
    13             fis.read(buffer);
    14             fis.close();
    15             // 清空response
    16             response.reset();
    17             // 设置response的Header
    18             response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
    19             response.addHeader("Content-Length", "" + file.length());
    20             OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
    21             response.setContentType("application/octet-stream");
    22             toClient.write(buffer);
    23             toClient.flush();
    24             toClient.close();
    25         } catch (IOException ex) {
    26             ex.printStackTrace();
    27         }
    28         return response;
    29     }
    30 
    31     public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
    32         // 下载本地文件
    33         String fileName = "Operator.doc".toString(); // 文件的默认保存名
    34         // 读到流中
    35         InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
    36         // 设置输出的格式
    37         response.reset();
    38         response.setContentType("bin");
    39         response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """);
    40         // 循环取出流中的数据
    41         byte[] b = new byte[100];
    42         int len;
    43         try {
    44             while ((len = inStream.read(b)) > 0)
    45                 response.getOutputStream().write(b, 0, len);
    46             inStream.close();
    47         } catch (IOException e) {
    48             e.printStackTrace();
    49         }
    50     }
    51 
    52     public void downloadNet(HttpServletResponse response) throws MalformedURLException {
    53         // 下载网络文件
    54         int bytesum = 0;
    55         int byteread = 0;
    56 
    57         URL url = new URL("windine.blogdriver.com/logo.gif");
    58 
    59         try {
    60             URLConnection conn = url.openConnection();
    61             InputStream inStream = conn.getInputStream();
    62             FileOutputStream fs = new FileOutputStream("c:/abc.gif");
    63 
    64             byte[] buffer = new byte[1204];
    65             int length;
    66             while ((byteread = inStream.read(buffer)) != -1) {
    67                 bytesum += byteread;
    68                 System.out.println(bytesum);
    69                 fs.write(buffer, 0, byteread);
    70             }
    71         } catch (FileNotFoundException e) {
    72             e.printStackTrace();
    73         } catch (IOException e) {
    74             e.printStackTrace();
    75         }
    76     }

     程序员的基础教程:菜鸟程序员

  • 相关阅读:
    The resource identified by this request is only capable of generating responses with characteristics
    javaweb写的在线聊天应用
    JavaScript写一个拼图游戏
    jQ插件--时间线插件和拖拽API
    Javascript写俄罗斯方块游戏
    详解jQ的support模块
    磁盘IO的性能指标 阻塞与非阻塞、同步与异步 I/O模型
    Airflow Python工作流引擎的重要概念介绍
    DEVOPS 运维开发系列
    MYSQL 两表 排除 重复记录
  • 原文地址:https://www.cnblogs.com/guohu/p/4045697.html
Copyright © 2011-2022 走看看