java web development large amount of data export Excel timeout of 504 problem solved

  • 2020-06-23 00:13:23
  • OfStack


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 {
  /**
   *  Target output stream 
   */
  private OutputStream stream;
  /**
   *  header 
   */
  private Map<String,String> fields;

  /**
   *  The data source model All of the fields 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);
    // Write the header, generate the file with the specified name, and return to the client 
    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();
  }
  /**
   *  Insert records into the table 
   */
  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);
    }
  }
}

The preparation of Excel data that is common in web development requires querying data from a database or querying data across the system call interface, which is time-consuming and therefore does not return data to the browser in time, resulting in a 504 timeout.

This tool USES ServletOutputStream segmentation to the browser flush data. Call method: first, new CSV (), pass in specified parameters, call wirte() method to write data to the browser, and finally call close method to close the flow.

The exported file format of this tool is.csv file. The default encoding of windows office tool is ASCI, and wps can match various codes.

This tool only handles transcoding of ", "in CSV and does not handle double quotes.

I hope this article can be helpful to those who encounter this problem


Related articles: