Java中Excel文件在多浏览器中下载

package com.foriseland.ec.product.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;


/**
 *
 * @author sx
 * @date 2018年1月31日下午8:52:38
 */
@Slf4j
@Controller
@RequestMapping("/fileOperation")
@Api(tags="文件操作")
public class FileOperationController {
    
    @RequestMapping("/downLoadTemplate")
    public void downLoadTemplate(HttpServletRequest request,HttpServletResponse response) {
        log.info("FileOperationController ..downLoadTemplate() start");
        OutputStream out=null;
        FileInputStream inputStream=null;
        try {
            //获取模板名称
            String templateName=request.getParameter("templateName");
            if(templateName!=null) {
                if(!(templateName.endsWith(".xls")||templateName.endsWith(".xlsx"))) {
                    templateName=templateName+".xlsx";
                }
            }
            //下载后本地文件名
            String fileName=request.getParameter("fileName");
            if(fileName==null||fileName.trim().equals("")) {
                fileName=new SimpleDateFormat("yyyyMMddhhmmmss").format(new Date()).toString();
            }
            if(!(fileName.endsWith(".xls")||fileName.endsWith(".xlsx"))) {
                fileName+=".xls";
            }
              //文件名的两种处理方式,防中文乱码
            fileName=new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
            //fileName=URLEncoder.encode(fileName, "UTF-8");
            //获取模板位置
            String templatePath="D:\\upload";
            //获取模板实际位置
            String path=templatePath+File.separator+templateName;
            //设置响应类型
            response.setContentType("multipart/form-date");
            //设置文件头
            //response.setHeader("Content-Disposition", "attachment:fileName="+fileName);
            /*判断用户浏览器,设置Content-Disposition.

            if(request.getHeader("User-Agent").toLowerCase().indexOf("firefox")>-1) {
                response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''"+fileName);
                System.out.println("firefox");
            }else {
                response.setHeader("Content-Disposition", "attachment;filename="+fileName);
            }*/

            //不用判断用户浏览器,设置Content-Disposition.
            response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''"+fileName);
            response.addHeader("Content-Type", "application/vnd.ms-excel");
            //获取文件对象
            File file=new File(path);
            inputStream=new FileInputStream(file);
            //通过response获取outputstream对象
            out=response.getOutputStream();
            byte[] buffer=new byte[512];
            int b=inputStream.read(buffer);
            while(b!=-1) {
                //将指定长度字节写出到输出流中
                out.write(buffer, 0, b);
                b=inputStream.read(buffer);
            }
            inputStream.close();
            out.close();
            out.flush();
        }catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    }

 

转载于:https://my.oschina.net/u/3748249/blog/1619296

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值