java web開發(fā)中大量數(shù)據(jù)導(dǎo)出Excel超時(504)問題解決
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import com.travelzen.framework.net.http.TZHttpClient;
import com.travelzen.tops.front.ota.member.item.CustomerItem;
public class CSV {
/**
* 目標(biāo)輸出流
*/
private OutputStream stream;
/**
* 表頭
*/
private Map<String,String> fields;
/**
* 數(shù)據(jù)源model所有字段map
*/
private static Map<String, Field> fieldMap = new HashMap<>();
public CSV(HttpServletResponse response,Map<String,String> fields,String fileName,Class<?> clz) throws IOException{
if(response == null || fields == null || fileName == null || clz == null)
throw new IllegalArgumentException();
getFieldMap(clz,fieldMap);
this.stream = response.getOutputStream();
this.fields = fields;
response.setContentType("application/octet-stream;charset=GBK");
response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);
//寫表頭,生成指定名字的文件,返回客戶端
StringBuilder hb = new StringBuilder();
for(Entry<String, String> e : fields.entrySet())
hb.append(e.getValue()+",");
stream.write(hb.substring(0, hb.length() - 1).getBytes("GBK"));
stream.flush();
}
/**
* 往表格中插入記錄
*/
public void write(List<Object> data) throws IllegalArgumentException, IllegalAccessException, IOException{
for(Object o : data){
StringBuilder sb = new StringBuilder();
sb.append("\n");
for(String field : fields.keySet()){
Field f = fieldMap.get(field);
f.setAccessible(true);
Object value = f.get(o);
if(value == null || StringUtils.isBlank(value.toString())){
sb.append(" ,");
} else if (f.getType() == Date.class) {
sb.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value) + ",");
} else if (f.getType() == DateTime.class) {
sb.append(((DateTime)value).toString("yyyy-MM-dd HH:mm:ss") + ",");
} else {
String tmp = value.toString();
if(tmp.contains(","))
tmp = tmp.replace(",", "\",\"");
sb.append(value.toString() + ",");
}
}
stream.write(sb.substring(0, sb.length() - 1).getBytes("GBK"));
stream.flush();
}
}
public void close() throws IOException{
stream.close();
}
private static <T extends Object> void getFieldMap(Class<T> clz, Map<String, Field> result) {
for (Field field : clz.getDeclaredFields()) {
result.put(field.getName(), field);
}
if (clz.getSuperclass() != null) {
getFieldMap(clz.getSuperclass(), result);
}
}
}
web開發(fā)中常見的準(zhǔn)備Excel數(shù)據(jù)需要從數(shù)據(jù)庫查詢數(shù)據(jù),或者跨系統(tǒng)調(diào)用接口查詢數(shù)據(jù),耗費(fèi)大量時間,因此未及時向?yàn)g覽器返回數(shù)據(jù),導(dǎo)致504超時。
本工具使用ServletOutputStream分段的往瀏覽器flush數(shù)據(jù)。調(diào)用方式:先new CSV(),傳入指定參數(shù),不斷的調(diào)用wirte()方法往瀏覽器寫入數(shù)據(jù),最后調(diào)用close方法關(guān)閉流。
本工具導(dǎo)出的文件格式為.csv文件,windows office工具默認(rèn)編碼為ASCI,wps會匹配各種編碼,libreOffice calc可以指定編碼,故此設(shè)置編碼為GBK,兼容三種Excel軟件,也可根據(jù)自身需求設(shè)置編碼。
本工具只處理了CSV中”,”的轉(zhuǎn)碼,對于雙引號并未處理。
希望本文能夠?qū)τ龅酱藛栴}的朋友能有所幫助
相關(guān)文章
Mybatis分頁插件Pagehelper的PageInfo字段屬性使用及解釋
這篇文章主要介紹了Mybatis分頁插件Pagehelper的PageInfo字段屬性使用及解釋,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA快速部署Spring?Boot?項(xiàng)目到Docker的實(shí)現(xiàn)方法
本文主要介紹了IDEA快速部署Spring?Boot?項(xiàng)目到Docker的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Spring中ApplicationContext的拓展功能詳解
這篇文章主要介紹了Spring中ApplicationContext的拓展功能詳解,相對于BeanFactory來說,ApplicationContext除了提供BeanFactory的所有功能外,還有一些其他的功能,主要包括國際化支持、資源訪問、事件傳遞,需要的朋友可以參考下2024-01-01
Java獲取Prometheus監(jiān)控數(shù)據(jù)的方法實(shí)現(xiàn)
本文主要介紹了Java獲取Prometheus監(jiān)控數(shù)據(jù)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解
這篇文章主要介紹了Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Spring Security自定義異常 AccessDeniedHandler不生效解決方法
本文主要介紹了Spring Security自定義異常 AccessDeniedHandler不生效解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
新的Java訪問mysql數(shù)據(jù)庫工具類的操作代碼
本文通過實(shí)例代碼給大家介紹新的Java訪問mysql數(shù)據(jù)庫工具類的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-12-12

