java导出TXT

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");
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值