package cn.com.countrygarden.bi.main.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.util.ResourceUtils;

public class DownloadExeclTemplate {
	
	public static void downloadTemplate(HttpServletResponse response,String key) throws IOException {
		File file = ResourceUtils.getFile("classpath:"+key);
		String fileName = new String(file.getName().getBytes("UTF-8"),"iso-8859-1");
		response.reset();
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("UTF-8");
		response.setContentLength((int) file.length());
		response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}

}
