java web開發(fā)中大量數據導出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 {
/**
* 目標輸出流
*/
private OutputStream stream;
/**
* 表頭
*/
private Map<String,String> fields;
/**
* 數據源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ā)中常見的準備Excel數據需要從數據庫查詢數據,或者跨系統(tǒng)調用接口查詢數據,耗費大量時間,因此未及時向瀏覽器返回數據,導致504超時。
本工具使用ServletOutputStream分段的往瀏覽器flush數據。調用方式:先new CSV(),傳入指定參數,不斷的調用wirte()方法往瀏覽器寫入數據,最后調用close方法關閉流。
本工具導出的文件格式為.csv文件,windows office工具默認編碼為ASCI,wps會匹配各種編碼,libreOffice calc可以指定編碼,故此設置編碼為GBK,兼容三種Excel軟件,也可根據自身需求設置編碼。
本工具只處理了CSV中”,”的轉碼,對于雙引號并未處理。
希望本文能夠對遇到此問題的朋友能有所幫助
相關文章
Java?web訪問http://localhost:8080/xx/xx.jsp報404錯誤問題的解決方法
這篇文章主要給大家介紹了關于Java?web訪問http://localhost:8080/xx/xx.jsp報404錯誤問題的解決方法,很多小伙伴在剛開始用Springboot整合jsp開發(fā)時都會遇到這個問題, 按照別人的教程一步一步搭建,但就是會報404,文中介紹的非常詳細,需要的朋友可以參考下2023-04-04
基于springboot實現整合shiro實現登錄認證以及授權過程解析
這篇文章主要介紹了基于springboot實現整合shiro實現登錄認證以及授權過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12
Apache Commons fileUpload文件上傳多個示例分享
這篇文章主要為大家分享了Apache Commons fileUpload文件上傳4個示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
java中快速創(chuàng)建帶初始值的List和Map實例
下面小編就為大家?guī)硪黄猨ava中快速創(chuàng)建帶初始值的List和Map實例。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

