文件导出_单文件导出.sql/.txt文件

文件导出_单文件导出.sql/.txt文件

将一个对象的内容字段导出为一个文件。

思路
通过前台data对象获取被选中对象的id值,
后台拿取到id后操作这个对象,取值(文件名 内容),写出即可。

···
html 部分

(layu框架)设置一个event / 你喜欢的
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="export">sql导出</a>

js 部分

	// 绑定事件
	if (layEvent === 'export') {
            Ts.onExportTs(data);
        }
    /**
     * 点击导出content内容为一个.sql文件
     */
    Ts.onExportTs = function (data) {
        var tsId = data.tsId;
        window.location = Feng.ctxPath + "/ts/exportTxt?tsId="+tsId;
    };
    

@Controller

 	/**
     * 提数.sql文件导出
     *
     * @Author liujiaxing
     * @Date 12:24 2019/12/13
     * @Param [tsId, response]
     * @return void
     **/
    @RequestMapping(value = "/exportTxt")
    @ResponseBody
    @BussinessLog(value = "导出sql", key = "tsId", dict = TsMap.class)
    public void exportTxt(@RequestParam Long tsId, HttpServletResponse response){
        this.tsService.exportTxt(tsId,response);
    }

@Service


	// 添加日志报错对象
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
	/**
     * 读取字符串导出为.sql文件
     *
     * @return void
     * @Author liujiaxing
     * @Date 12:25 2019/12/13
     * @Param [id, response]
     **/
    public void exportTxt(@RequestParam Long id, HttpServletResponse response) {
        // 编码设置
        response.setCharacterEncoding("utf-8");
        response.setContentType("test/plain");
        // 
        Object obj = this.getById(id);
        // 获取“文件名”&“文件内容” (html反转义文本格式2次检测)
        String fileName = HtmlUtils.htmlUnescape(obj.getFileName().replaceAll("& #", "&#"));
        String fileContent = HtmlUtils.htmlUnescape(obj.getContent().replaceAll("& #", "& "));
        // 只取对象中存储的文件名的名字部分,后缀自己定义
        fileName = fileName.substring(0, fileName.lastIndexOf("."));

        // 添加相应头 genAttachementFileName()处理文件名编码 // Content-Disposition“内容处理” attachment"附件"
        response.addHeader("Content-Disposition", "attachment;filename=" + genAttachmentFileName(fileName, "(默认名称)") + ".sql");
        // 声明 输出流对象
        BufferedOutputStream buff = null;
        ServletOutputStream os = null;
        try {
            os = response.getOutputStream();
            buff = new BufferedOutputStream(os);
            buff.write(fileContent.getBytes());
            buff.flush();
            buff.close();
//            System.out.println("导出成功");
        } catch (Exception e) {
            logger.error("sql文件导出catch错误,ljx=>ObjectService.exportTxt()", e);
        } finally {
            try {
                buff.close();
                os.close();
            } catch (Exception e) {
                logger.error("sql文件导出流对象close出错 ljx=>ObjectService.exportTxt() e:{}", e);
            }
        }// 此处不做返回了,浏览器下载框够明显了,你也可以自己做返回
    }
	/**
     * 防止文件名中文乱码
     * (sql文件导出sql时addHarder调用的,若使用setHeader请忽略)
     * 自带的一个String编码替换,我也没搞明白为什么是这2个编码
     * @Param ‘当前字符串’ ‘默认字符串’
     **/
	public String genAttachmentFileName(String cnName, String defaultName) {

        try {
            cnName = new String(cnName.getBytes("gb2312"), "ISO8859-1");
        } catch (Exception e) {
            cnName = defaultName;
        }
        return cnName;
    }

就此完成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值