[功能集锦] 001 - java下载文件
@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下载文件的更多相关文章
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- java下载文件工具类
java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- java下载文件时文件名出现乱码的解决办法
转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249 java下载文件时文件名出现乱码的解决办法: String userAgent ...
- Java 下载文件
public @ResponseBody void exportExcel(HttpServletRequest request, HttpServletResponse response, Khxx ...
- java下载文件demo
java通过http方式下载文件 https://www.cnblogs.com/tiancai/p/7942201.html
- Java下载文件(流的形式)
@RequestMapping("download") @ResponseBody public void download(HttpServletResponse respons ...
- Java下载文件的几种方式
转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...
- java 下载文件的两种方式和java文件的上传
一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...
- Java下载文件方法
public static void download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径. F ...
随机推荐
- LeetCode(282) Peeking Iterator
题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...
- LeetCode(205)Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...
- HDU - 5884 Sort (二分答案+贪心)
有n个数字,你需要把这n个数字合成一个数字,每次只能把k个数字合并成一个,花费为这k个数字的和. 给一个最大花费,问不超过这个最大花费的情况下,k的最小值. Sample Input 1 5 25 1 ...
- 浅谈内核的Makefile、Kconfig和.config文件
Linux内核源码文件繁多,搞不清Makefile.Kconfig..config间的关系,不了解内核编译体系,编译修改内核有问题无从下手,自己写的驱动不知道怎么编进内核,不知道怎么配置内核,这些问题 ...
- csapp-15213错误修正18-10-28
1.p229 练习题3.15 b.答案错误,应为400419
- Post页面爬取失败__编码问题
python3爬取Post页面时, 报以下错误 "POST data should be bytes or an iterable of bytes. It cannot be of typ ...
- x200 xp 驱动下载
http://support.lenovo.com/en_US/downloads/detail.page?&LegacyDocID=MIGR-70602
- 解决子线程操作UI的方法
- selenium 自动化测试 Chrome 大于 63 版本 不能重定向问题解决办法
Chrome 一些信息: Chrome 63 以后,浏览器默认屏蔽了重定向 Chrome 63 版本,设置了禁止更新,有些情况还是会更新到最新版本 解决过程: 在博客上查到 selenium 里 给 ...
- ASP.Net 更新页面输出缓存的几种方法
ASP.Net 自带的缓存机制对于提高页面性能有至关重要的作用,另一方面,缓存的使用也会造成信息更新的延迟.如何快速更新缓存数据,有时成了困扰程序员的难题.根据我的使用经验,总结了下面几种方法,概括了 ...