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. CCBReader

    #ifndef _CCB_CCBREADER_H_ #define _CCB_CCBREADER_H_ #include "cocos2d.h" #include "Ex ...

  2. GitHub使用详解

    1.GitHub是什么? GitHub这个名词既可以是那个流行的代码分享和协作网站 https://github.com/,也可以是指Git客户端工具(与其他的Git客户端工具如GitEye类似,只不 ...

  3. 05.pathinfo的两种模式与模版和控制器之间的关系

    <?php function dump($data){ echo '<pre>'; var_dump($data); echo '</pre>'; } dump($_SE ...

  4. [Ramda] Compose and Curry

    Curry: The idea of Curry is to spreate the data from the function. Using Curry to define the functio ...

  5. MySQL server version for the right syntax to use near ‘USING BTREE

    转自:http://www.douban.com/note/157818842/ 有时导入mysql会提示如下错误: C:\Users\liqiang>mysql -uroot -paaaaaa ...

  6. C 高级编程3 静态库与动态库

    http://blog.csdn.net/Lux_Veritas/article/details/11934083http://www.cnblogs.com/catch/p/3857964.html ...

  7. 最简单的Java调用C/C++代码的步骤

    1)首先在Java类中声明一个native的方法 (2)使用javah命令生成包含native方法声明的C/C++头文件 (3)按照生成的C/C++头文件来写C/C++源文件 (4)将C/C++源文件 ...

  8. dl-ssl.google.com

    转载:http://jingyan.baidu.com/article/64d05a02752300de55f73b99.html 搭建Android就会用到Android SDK,而安装SDK有个恶 ...

  9. SCI&EI 英文PAPER投稿经验【转】

    英文投稿的一点经验[转载] From: http://chl033.woku.com/article/2893317.html 1. 首先一定要注意杂志的发表范围, 超出范围的千万别投,要不就是浪费时 ...

  10. Android进阶笔记13:RoboBinding(实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架)

    1.RoboBinding RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架.从简单的角度看,他移除了如addXXListen ...