Excel下载模板配合的工具类

来源:互联网 发布:中国纺织品出口数据图 编辑:程序博客网 时间:2024/05/01 18:55
Excel下载模板配合的工具类
public class ExportUtil {


/**
* 导出文件

* @param response
* @param file
*            导出文件
* @param name
* @param contentType
* @throws IOException
*/
public static void download(HttpServletResponse response, File file,
String name, String contentType) throws IOException {
String fileName = StringUtils.isBlank(name) ? file.getName() : name;
download(response, new FileInputStream(file), fileName, contentType);
}


/**
* 下载数据/文件

* @param response
*            HTTP输出
* @param inputStream
*            文件流
* @param fileName
*            文件名
* @param contentType
*            ContentType in HTTP Header
* @throws IOException
*             IO异常
*/
public static void download(HttpServletResponse response,
InputStream inputStream, String fileName, String contentType)
throws IOException {


response.setContentType(StringUtils.isEmpty(contentType) ? "application/octet-stream"
: contentType);
/*这里的StringUtil是一个工具类用来判断空字符的
* 字符串非空验证
* @param str 验证字符串
* @return 验证结果
public static boolean isEmpty(String str){
if(null==str || str.equals("")){
return true;
}
return false;
}

*/

response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gbk"), "ISO-8859-1"));
response.setStatus(HttpServletResponse.SC_OK);


BufferedInputStream reader = null;
try {
reader = new BufferedInputStream(inputStream);
IOUtils.copy(reader, response.getOutputStream());
} finally {
if (reader != null) {
reader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
}
}