java web開(kāi)發(fā)中大量數(shù)據(jù)導(dǎo)出Excel超時(shí)(504)問(wèn)題解決
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);
//寫(xiě)表頭,生成指定名字的文件,返回客戶(hù)端
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開(kāi)發(fā)中常見(jiàn)的準(zhǔn)備Excel數(shù)據(jù)需要從數(shù)據(jù)庫(kù)查詢(xún)數(shù)據(jù),或者跨系統(tǒng)調(diào)用接口查詢(xún)數(shù)據(jù),耗費(fèi)大量時(shí)間,因此未及時(shí)向?yàn)g覽器返回?cái)?shù)據(jù),導(dǎo)致504超時(shí)。
本工具使用ServletOutputStream分段的往瀏覽器flush數(shù)據(jù)。調(diào)用方式:先new CSV(),傳入指定參數(shù),不斷的調(diào)用wirte()方法往瀏覽器寫(xiě)入數(shù)據(jù),最后調(diào)用close方法關(guān)閉流。
本工具導(dǎo)出的文件格式為.csv文件,windows office工具默認(rèn)編碼為ASCI,wps會(huì)匹配各種編碼,libreOffice calc可以指定編碼,故此設(shè)置編碼為GBK,兼容三種Excel軟件,也可根據(jù)自身需求設(shè)置編碼。
本工具只處理了CSV中”,”的轉(zhuǎn)碼,對(duì)于雙引號(hào)并未處理。
希望本文能夠?qū)τ龅酱藛?wèn)題的朋友能有所幫助
- 基于java web獲取網(wǎng)頁(yè)訪(fǎng)問(wèn)次數(shù)代碼實(shí)例
- Javaweb實(shí)現(xiàn)在線(xiàn)人數(shù)統(tǒng)計(jì)代碼實(shí)例
- Javaweb監(jiān)聽(tīng)器實(shí)例之統(tǒng)計(jì)在線(xiàn)人數(shù)
- 詳解javaweb中jstl如何循環(huán)List中的Map數(shù)據(jù)
- Javaweb使用cors完成跨域ajax數(shù)據(jù)交互
- javaweb中mysql數(shù)據(jù)庫(kù)連接步驟方法及其實(shí)例
- Java web數(shù)據(jù)可視化實(shí)現(xiàn)原理解析
相關(guān)文章
Mybatis分頁(yè)插件Pagehelper的PageInfo字段屬性使用及解釋
這篇文章主要介紹了Mybatis分頁(yè)插件Pagehelper的PageInfo字段屬性使用及解釋,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA快速部署Spring?Boot?項(xiàng)目到Docker的實(shí)現(xiàn)方法
本文主要介紹了IDEA快速部署Spring?Boot?項(xiàng)目到Docker的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Spring中ApplicationContext的拓展功能詳解
這篇文章主要介紹了Spring中ApplicationContext的拓展功能詳解,相對(duì)于BeanFactory來(lái)說(shuō),ApplicationContext除了提供BeanFactory的所有功能外,還有一些其他的功能,主要包括國(guó)際化支持、資源訪(fǎng)問(wèn)、事件傳遞,需要的朋友可以參考下2024-01-01
Java獲取Prometheus監(jiān)控?cái)?shù)據(jù)的方法實(shí)現(xiàn)
本文主要介紹了Java獲取Prometheus監(jiān)控?cái)?shù)據(jù)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Mybatis注解方式操作Oracle數(shù)據(jù)庫(kù)詳解
這篇文章主要介紹了Mybatis注解方式操作Oracle數(shù)據(jù)庫(kù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Spring Security自定義異常 AccessDeniedHandler不生效解決方法
本文主要介紹了Spring Security自定義異常 AccessDeniedHandler不生效解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
新的Java訪(fǎng)問(wèn)mysql數(shù)據(jù)庫(kù)工具類(lèi)的操作代碼
本文通過(guò)實(shí)例代碼給大家介紹新的Java訪(fǎng)問(wèn)mysql數(shù)據(jù)庫(kù)工具類(lèi)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-12-12

