public static void down(HttpServletRequest request,
   HttpServletResponse
response) throws Exception {
  
  
  String name="aaa.*";//文件名
  String uploadPath =
UploadFileHelper.getRepositoryPath()+"//";//文件来源

String
filePath = name;
  String fileName = name;
  
  
  if
(request.getHeader("User-Agent").toLowerCase().indexOf("firefox")
> 0){
   fileName =
new String(fileName.getBytes("UTF-8"),
"ISO8859-1");//firefox浏览器
  }else {
      
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE")
> 0){
     
  fileName = URLEncoder.encode(fileName,
"UTF-8");//IE浏览器
      
}
  }

response.setContentType("text/plain");

response.setHeader("Location",fileName);

response.reset();
  response.setHeader("Cache-Control",
"max-age=0" );
  response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
 BufferedInputStream bis = null;
  BufferedOutputStream bos =
null;
  OutputStream fos = null;
  InputStream fis = null;
  filePath = uploadPath +
filePath;
  fis = new
FileInputStream(filePath);
    
bis = new BufferedInputStream(fis);
    
fos = response.getOutputStream();
    
bos = new BufferedOutputStream(fos);

int bytesRead = 0;
    
byte[] buffer = new byte[5 * 1024];
    
while ((bytesRead = bis.read(buffer)) != -1) {
        
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
    
}
    
bos.close();
    
bis.close();
    
fos.close();
    
fis.close();

}

java 下载文件功能代码例子的更多相关文章

  1. java下载文件工具类

    java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  2. php下载文件的代码示例

    php下载文件的代码示例,需要的朋友可以参考下 <?php  $file = 'monkey.gif';  if (file_exists($file)) {  header('Content- ...

  3. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  4. springboot项目下载文件功能中-切面-导致的下载文件失败的bug

    背景:使用spring提供的 ResponseEntity 和Resource结合,实现的下载文件功能 bug:Resource已经加载到了文件, 并且通过 ResponseEntity 构建了响应, ...

  5. java下载文件时文件名出现乱码的解决办法

    转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249   java下载文件时文件名出现乱码的解决办法: String userAgent ...

  6. 批量去除Teleport Pro整站下载文件冗余代码

    teleport pro tppabs标签批量删除 teleport pro tppabs标签批量删除 使 用Teleport Pro下载的网页代码中包含了很多垃圾代码,比如下载的html网页代码中会 ...

  7. Java 下载文件

    public @ResponseBody void exportExcel(HttpServletRequest request, HttpServletResponse response, Khxx ...

  8. java下载文件demo

    java通过http方式下载文件 https://www.cnblogs.com/tiancai/p/7942201.html

  9. Java下载文件(流的形式)

    @RequestMapping("download") @ResponseBody public void download(HttpServletResponse respons ...

随机推荐

  1. java实现抓取某公司官网新闻

    这里先说一下,实习期的一个项目,当时并没有该合作公司的获取新闻的接口,但是项目又急着上线,所以总监就让我来做一个简单的抓取,现将主要的工具类NewsUtil.java贴出来供大家参考. NewsUti ...

  2. linux杂谈(十八):DNS服务器的配置(一)

    原文地址: http://blog.chinaunix.net/uid-29622064-id-4242123.html 1.DNS服务器简介 域名系统(英文:Domain Name System,縮 ...

  3. SCOM2007R2安装和报表服务器配置

    SCOM2007R2默认安装不可以直接支持SQL Server2008R2,需要SQL Server 2008SP1. 如果数据库安装在另一台计算机上,则在安装了SQL Server的计算机上先运行S ...

  4. uoj #31. 【UR #2】猪猪侠再战括号序列 贪心

    #31. [UR #2]猪猪侠再战括号序列 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/31 Descript ...

  5. GB2312 Unicode转换表实现跨平台utf8转码unicode

    在GSM模块中,为发送中文短信,采用pdu发送,需要unicode编码.源程序编辑软件将中文转化为GB2312编码,再下列的GB2312 Unicode转换表转为unicode. 实现2维数值,GB2 ...

  6. 【zabbix系列】报警系统的设置和排除

    关于邮件报警,有非常多方案,这里选择的是稳定性较好.使用较多的msmtp+mutt方案. 该方案有一个非常好的地方在于不用自己来搭建独立的mailserver,能够使用第三方mail.这样的方法不仅能 ...

  7. MySQL · 引擎特性 · InnoDB COUNT(*) 优化(?)

    http://mysql.taobao.org/monthly/2016/06/10/ 在5.7版本中,InnoDB实现了新的handler的records接口函数,当你需要表上的精确记录个数时,会直 ...

  8. Panopticon跨平台的逆向工程反汇编工具

    http://www.freebuf.com/sectool/104045.html Panopticon 使用GPLv3授权许可,其免费. 项目文档:https://panopticon.re. 问 ...

  9. leetcode -- Merge k Sorted Lists add code

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [ ...

  10. Linux 进程管理剖析--转

    地址:http://www.ibm.com/developerworks/cn/linux/l-linux-process-management/index.html Linux 是一种动态系统,能够 ...