java web開發中大量數據導出Excel超時(504)問題解決。本站提示廣大學習愛好者:(java web開發中大量數據導出Excel超時(504)問題解決)文章只能為提供參考,不一定能成為您想要的結果。以下是java web開發中大量數據導出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開發中常見的准備Excel數據需要從數據庫查詢數據,或者跨系統調用接口查詢數據,耗費大量時間,因此未及時向浏覽器返回數據,導致504超時。
本工具使用ServletOutputStream分段的往浏覽器flush數據。調用方式:先new CSV(),傳入指定參數,不斷的調用wirte()方法往浏覽器寫入數據,最後調用close方法關閉流。
本工具導出的文件格式為.csv文件,windows office工具默認編碼為ASCI,wps會匹配各種編碼,libreOffice calc可以指定編碼,故此設置編碼為GBK,兼容三種Excel軟件,也可根據自身需求設置編碼。
本工具只處理了CSV中”,”的轉碼,對於雙引號並未處理。
希望本文能夠對遇到此問題的朋友能有所幫助