@RequestMapping("/downloadxls.action")
public void downloadxls(HttpServletRequest request, HttpServletResponse response) {
//获取请求参数
Map<String, Object> params = ParamsUtil.getParams(request);
String contextPath = request.getSession().getServletContext().getRealPath(File.separator + "report");
String excelName = xxxxx;
String excelFullName = contextPath + File.separator + excelName + ".xls";
InputStream inStream = null, fileInStream = null;
ServletOutputStream outStream = null;
int byteRead;
try {
fileInStream = new FileInputStream(excelFullName);
inStream = new BufferedInputStream(fileInStream);
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-disposition", "attachment; filename=" + excelName + ".xls");
outStream = response.getOutputStream();
byte[] buffer = new byte[1024];
while ((byteRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, byteRead);
}
response.flushBuffer();
outStream.close();
inStream.close();
fileInStream.close();
} catch (Exception e) {
LOGGER.error( e);
}finally{
try {
if(outStream!=null){
outStream.close();
}
} catch (IOException e2) {
LOGGER.error(e2);
}
try {
if(inStream!=null){
inStream.close();
}
} catch (IOException e2) {
LOGGER.error(e2);
}
try {
if(fileInStream!=null){
fileInStream.close();
}
} catch (IOException e2) {
LOGGER.error(e2);
}
}
}

[功能集锦] 001 - java下载文件的更多相关文章

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

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

  2. java下载文件工具类

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

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

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

  4. Java 下载文件

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

  5. java下载文件demo

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

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

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

  7. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

  8. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  9. Java下载文件方法

    public static void download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径. F ...

随机推荐

  1. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  2. hdu 4565

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  3. poj 2229 拆数问题 dp算法

    题意:一个n可以拆成 2的幂的和有多少种 思路:先看实例 1   1 2    1+1     2 3     1+1+1  1+2 4      1+1+1+1  1+1+2  2+2  4 5  ...

  4. BZOJ 4425: [Nwerc2015]Assigning Workstations分配工作站

    难度在于读题 #include<cstdio> #include<algorithm> #include<queue> using namespace std; p ...

  5. 添加字段的SQL语句的写法:

    alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参 --删除字段   -- alter table  [SolidDB].[dbo].tP ...

  6. bat 文件读取乱码问题

    使用 for 循环 type file1.txt > file2.txt 文件读取后可能会出现乱码,需要在 bat 文件中设置 chcp 表示将批处理设置为 utf-8 编码,这样在生成文件和读 ...

  7. 缓存淘汰算法之LFU

    1. LFU类 1.1. LFU 1.1.1. 原理 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频 ...

  8. Leetcode 457.环形数组循环

    环形数组循环 给定一组含有正整数和负整数的数组.如果某个索引中的 n 是正数的,则向前移动 n 个索引.相反,如果是负数(-n),则向后移动 n 个索引. 假设数组首尾相接.判断数组中是否有环.环中至 ...

  9. 2018省赛赛第一次训练题解和ac代码

    第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title     A CodeForces 607A Chain Reaction     B CodeForces ...

  10. Welcome-to-Swift-08枚举 (Enumerations)

    枚举为一系相关联的值定义了一个公共的组类型.同时能够让你在编程的时候在类型安全的情况下去使用这些值. 如果你对C语言很熟悉,你肯定知道在C语言中枚举类型就是一系列具有被指定有关联名称的的整数值.但在S ...