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. npm package 装包匹配原则

    经常看到package.json 里面有这样的devDependencies: "devDependencies": { "@angular/common": ...

  2. SpringMVC经典系列-14自己定义SpringMVC的拦截器---【LinusZhu】

    注意:此文章是个人原创.希望有转载须要的朋友们标明文章出处.假设各位朋友们认为写的还好,就给个赞哈.你的鼓舞是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linusz ...

  3. cocos2d-x如何优化内存的应用

    自身以前也写过cocos2d-x如何优化内存的应用,以及内存不够的情况下怎么样处置惩罚游戏.今天在微博中看到有友好简介了下内存,挺详细的.不晓得是谁写的,我纪录下. 一,IOS与图片内存 在IOS上, ...

  4. 浅谈Linux的内存管理机制

    转至:http://ixdba.blog.51cto.com/2895551/541355 一 物理内存和虚拟内存          我们知道,直接从物理内存读写数据要比从硬盘读写数据要快的多,因此, ...

  5. OSGi 学习(一)

    从基础开始,先来说说OSGi的基本理念. OSGi通过隔离底层classloader,强制应用在设计的时候就考虑模块化,并且基于白板模式来支持服务的注册与订阅. 在OSGi中,模块可以等价理解为bun ...

  6. apue.h

    [root@localhost unix_env_advance_prog]# cat apue.h #ifndef _APUE_H #define _APUE_H #define _XOPEN_SO ...

  7. 关于Android M RuntimePermission的问题

    关于shouldShowRequestPermissionRationale的理解, 在onRequestPermissionsResult里如果用户拒绝了权限, 可以调用这个api, 返回true, ...

  8. 关于File.getPath,File.getAbsolutePath,File.getCanonicalPath的区别

    这个问题, 不了解一下还是挺恍惚它们之间的区别的. 其实也挺简单的. getPath()-->>new File()时的路径 getAbsolutePath()-->>当前路径 ...

  9. css 之position用法详解

    css  之position用法详解: http://www.jb51.net/web/77495.html

  10. MySQL查询优化 (一)

    以下的文章主要讲述的是MySQL查询优化的5个十分好用方法,熟悉SQL语句的人都清楚,如果要对一个任务进行操作的话,SQL语句可以有很多种相关写法,但是不同的写法查询的性能可能会有天壤之别. 本文列举 ...