java将获取到的file文件下载到任意位置

来源:互联网 发布:同花顺软件k线图 编辑:程序博客网 时间:2024/05/29 09:56

1、首先获取到需要下载的数据(list),

2、将其转成Excel格式(具体操作可参考将list转成Excel表格并下载):

ExcelFileUtil excel = new ExcelFileUtil();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
File file = excel.write("君康健康险承保数据报表           操作时间:"+date, performanceDetailList, EbizPerfoemanceDetailDto.class);        


3、将得到的file文件下载到任意位置:

response.setContentType("text/html,charset=utf-8");

        response.setCharacterEncoding("UTF-8");
try {
FileInputStream in = new FileInputStream(file);
OutputStream fos = response.getOutputStream();
response.reset();
response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment;filename="+file.getName());
byte[] b = new byte[2048];
int read;
while ((read = in.read(b)) != -1) {
fos.write(b,0,read);
}
fos.flush();
in.close();
fos.close();
} catch (FileNotFoundException e) {
logger.error(e.getMessage());
    }

4、效果如图:

1 0
原创粉丝点击