项目需求是:需要把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