package com.xinhua.xpm.execute.util;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xinhua.xpm.execute.model.PrjDesignBomData;
/**
* @author minqiang
* @version 创建时间 2018年8月9日 下午3:06:29
*/
public class ExportTxtUtil {
private static final Logger logger = LoggerFactory.getLogger(ExportTxtUtil.class);
public static void exportTxt(List<PrjDesignBomData> prjDesignBomList, HttpServletResponse response) {
String text = formatData(prjDesignBomList);
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain");
BufferedOutputStream bufferedOutputStream = null;
ServletOutputStream servletOutputStream = null;
try {
response.addHeader("Content-Disposition", "attachment;filename=" + genAttachmentFileName("sap") + ".txt");
servletOutputStream = response.getOutputStream();
bufferedOutputStream = new BufferedOutputStream(servletOutputStream);
bufferedOutputStream.write(text.getBytes("UTF-8"));
bufferedOutputStream.flush();
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage());
} catch (IOException e) {
logger.error(e.getMessage());
} finally {
try {
bufferedOutputStream.close();
servletOutputStream.close();
} catch (Exception e) {
logger.error(e.getMessage());
}
}
}
public static String formatData(List<PrjDesignBomData> prjDesignBomList) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
StringBuffer text = new StringBuffer();
for (PrjDesignBomData prjDesignBom : prjDesignBomList) {
text.append(prjDesignBom.getParentId());
text.append(" ");
text.append("X001");
text.append(" ");
text.append("1");
text.append(" ");
text.append("1");
text.append(" ");
text.append("1");
text.append(" ");
text.append("99999999");
text.append(" ");
text.append("1");
text.append(" ");
text.append(simpleDateFormat.format(new Date()));
text.append(" ");
text.append(prjDesignBom.getBomNum());
text.append(" ");
text.append("L");
text.append(" ");
text.append(prjDesignBom.getAmount());
text.append("\r\n");// 换行字符
}
return text.toString();
}
public static String genAttachmentFileName(String fileName) throws UnsupportedEncodingException {
return fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
}
}