java实现文件下载的两种方式

作者:pangqiandou 时间:2023-11-11 06:37:14 

本文实例为大家分享了java实现文件下载的具体代码,供大家参考,具体内容如下


public HttpServletResponse download(String path, HttpServletResponse response) {
   try {
     // path是指欲下载的文件的路径。
     File file = new File(path);
     // 取得文件名。
     String filename = file.getName();
     // 取得文件的后缀名。
     String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 以流的形式下载文件。
     InputStream fis = new BufferedInputStream(new FileInputStream(path));
     byte[] buffer = new byte[fis.available()];
     fis.read(buffer);
     fis.close();
     // 清空response
     response.reset();
     // 设置response的Header
     response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
     response.addHeader("Content-Length", "" + file.length());
     OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
     response.setContentType("application/octet-stream");
     toClient.write(buffer);
     toClient.flush();
     toClient.close();
   } catch (IOException ex) {
     ex.printStackTrace();
   }
   return response;
 }

public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
   // 下载本地文件
   String fileName = "Operator.doc".toString(); // 文件的默认保存名
   // 读到流中
   InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
   // 设置输出的格式
   response.reset();
   response.setContentType("bin");
   response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
   // 循环取出流中的数据
   byte[] b = new byte[100];
   int len;
   try {
     while ((len = inStream.read(b)) > 0)
       response.getOutputStream().write(b, 0, len);
     inStream.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }

public void downloadNet(HttpServletResponse response) throws MalformedURLException {
   // 下载网络文件
   int bytesum = 0;
   int byteread = 0;

URL url = new URL("windine.blogdriver.com/logo.gif");

try {
     URLConnection conn = url.openConnection();
     InputStream inStream = conn.getInputStream();
     FileOutputStream fs = new FileOutputStream("c:/abc.gif");

byte[] buffer = new byte[1204];
     int length;
     while ((byteread = inStream.read(buffer)) != -1) {
       bytesum += byteread;
       System.out.println(bytesum);
       fs.write(buffer, 0, byteread);
     }
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }

//支持在线打开文件的一种方式


public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
   File f = new File(filePath);
   if (!f.exists()) {
     response.sendError(404, "File not found!");
     return;
   }
   BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
   byte[] buf = new byte[1024];
   int len = 0;

response.reset(); // 非常重要
   if (isOnLine) { // 在线打开方式
     URL u = new URL("file:///" + filePath);
     response.setContentType(u.openConnection().getContentType());
     response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
     // 文件名应该编码成UTF-8
   } else { // 纯下载方式
     response.setContentType("application/x-msdownload");
     response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
   }
   OutputStream out = response.getOutputStream();
   while ((len = br.read(buf)) > 0)
     out.write(buf, 0, len);
   br.close();
   out.close();
 }

来源:http://blog.csdn.net/pangqiandou/article/details/53234586

标签:java,文件下载
0
投稿

猜你喜欢

  • springboot动态调用实现类方式

    2021-07-05 21:33:17
  • C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能

    2022-01-09 15:30:45
  • Android 自定义View实现任意布局的RadioGroup效果

    2021-08-07 16:20:24
  • java swing GUI窗口美化方式

    2023-04-03 00:13:56
  • 解决Unity无限滚动复用列表的问题

    2022-12-26 04:42:18
  • Java实现解出世界最难九宫格问题

    2022-06-14 19:47:10
  • Spring Boot与Spring MVC Spring对比及核心概念

    2023-09-27 05:14:36
  • 剖析SpringCloud Feign中所隐藏的坑

    2023-11-19 05:32:03
  • Java精品项目瑞吉外卖之员工信息管理篇

    2023-07-29 07:43:36
  • Android Studio轻松构建自定义模板的步骤记录

    2023-07-19 01:11:51
  • Netty实现简易版的RPC框架过程详解

    2023-05-23 23:19:58
  • mybatis注入Date日期值为null的解决方法

    2021-12-08 01:49:17
  • C语言实现通讯管理系统设计

    2022-12-29 13:04:50
  • Kotlin 编程三分钟入门

    2021-06-27 13:22:35
  • 一文带你搞懂Java定时器Timer的使用

    2022-09-08 01:18:16
  • IDEA 插件 mapper和xml互相跳转操作

    2021-07-19 02:48:13
  • springboot整合mybatis实现简单的一对多级联查询功能

    2023-11-07 13:30:22
  • 多线程如何解决for循环效率的问题

    2023-12-02 08:31:55
  • C#中4种深拷贝方法介绍

    2023-01-02 21:16:11
  • 使用java反射将结果集封装成为对象和对象集合操作

    2022-03-11 18:30:26
  • asp之家 软件编程 m.aspxhome.com