java String对象转Base64

本文介绍了一个项目需求,涉及将.doc文件通过流读取为String对象,然后将其转换为Base64格式,以便在XML页面上展示。核心代码包括ServiceResponse类和AttachmentFeign类。
摘要由CSDN通过智能技术生成

项目需求是:需要把world文档,即.doc后缀结尾的文件,通过流的方式读取文件转成String对象,再把String对象转成Base64格式数据展示在xml页面上。具体核心代码如下:

String docImport = fcsFormLayout.getTemplateFileId();
                 //if判空操作,当docImport不为空后执行
                if (StringUtilExt.isNotEmpty(docImport)) {

                    ServiceResponse<Attachment> serviceResponses = new ServiceResponse<>();
                    serviceResponses = attachmentFeign.get(docImport);
                    Attachment attachmentFeighVo = serviceResponses.getData();
                    String filePath = attachmentFeighVo.getFilePath();
                    InputStream inputStream = new FileInputStream(filePath);
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuffer buffer = new StringBuffer();
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        buffer.append(line);
                    }
                    String docString = buffer.toString();
                    String base64Str = (new sun.misc.BASE64Encoder()).encode(docString.getBytes());
                    fcsWidgetForm.setTemplateFile(base64Str);
                }

 功能相关代码:

package com.esgov.smartform.controller.form;

import com.esgov.smartform.contants.FcsFormConstants;
import com.esgov.smartform.core.dto.ServiceResponse;
import com.esgov.smartform.core.util.FastJsonUtils;
import com.esgov.smartform.core.util.StringUtilExt;
import com.esgov.smartform.core.util.UUIDUtil;
import com.esgov.smartform.core.util.XmlUtil;
import com.esgov.smartform.fe.common.AttachmentFeign;
import com.esgov.smartform.fe.common.entity.Attachment;
import com.esgov.smartform.fe.common.entity.FileType;
import com.esgov.smartform.fe.form.FcsFormGroupFeign;
import com.esgov.smartform.fe.form.FcsFormLayoutFeign;
import com.esgov.smartform.fe.form.entity.*;
import com.esgov.smartform.fe.form.entity.edit.FcsWidgetForm;
import com.esgov.smartform.fe.form.entity.edit.FcsWidgetPage;
import com.esgov.smartform.utils.FastDfsUtils;
import com.esgov.smartform.utils.LayuiPageUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import or
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值