流转base64的预览
package com.epoint.yy.auditproject.auditdoc.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.jboss.resteasy.spi.WriterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.epoint.ahxdocment.api.IAhxDocmentUtil;
import com.epoint.auditproject.auditproject.service.MatrixToImageWriter;
// import com.epoint.auditproject.auditproject.service.MatrixToImageWriter;
import com.epoint.basic.auditproject.auditproject.domain.AuditProject;
import com.epoint.basic.auditproject.auditproject.inter.IAuditProject;
import com.epoint.basic.auditproject.auditprojectdocsnap.domain.AuditProjectDocSnap;
import com.epoint.basic.auditproject.auditprojectdocsnap.inter.IAuditProjectDocSnap;
import com.epoint.basic.auditproject.auditprojectmaterial.domain.AuditProjectMaterial;
import com.epoint.basic.auditproject.auditprojectmaterial.inter.IAuditProjectMaterial;
import com.epoint.basic.auditresource.auditdoctemp.domain.AuditDocTemp;
import com.epoint.basic.auditresource.auditdoctemp.inter.IAuditDocTemp;
import com.epoint.basic.audittask.basic.domain.AuditTask;
import com.epoint.basic.audittask.basic.inter.IAuditTask;
import com.epoint.basic.audittask.material.domain.AuditTaskMaterial;
import com.epoint.basic.audittask.material.inter.IAuditTaskMaterial;
import com.epoint.basic.controller.BaseController;
import com.epoint.common.util.AttachUtil;
import com.epoint.common.util.SqlConditionUtil;
import com.epoint.common.zwfw.authentication.ZwfwUserSession;
import com.epoint.composite.auditsp.handlematerial.inter.IHandleMaterial;
import com.epoint.core.grammar.Record;
import com.epoint.core.utils.classpath.ClassPathUtil;
import com.epoint.core.utils.date.EpointDateUtil;
import com.epoint.core.utils.file.FileManagerUtil;
import com.epoint.core.utils.string.StringUtil;
import com.epoint.frame.service.attach.api.IAttachService;
import com.epoint.frame.service.attach.entity.FrameAttachInfo;
import com.epoint.frame.service.attach.entity.FrameAttachStorage;
import com.epoint.frame.service.metadata.code.api.ICodeItemsService;
import com.epoint.wenshu.api.IProjectWenshuService;
import com.epoint.yy.auditproject.inter.IReceiveArtileList;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import sun.misc.BASE64Encoder;
/**
* 办件预览
*
* @作者 刘荣
*/
@RestController("attachonlineviewaction")
@Scope("request")
public class AttachOnlineViewAction extends BaseController
{
@Autowired
private IAttachService iAttachService;
private ByteArrayOutputStream baos;
private String pngbase64 = "data:image/png;base64,";
private String pdfbase64 = "data:application/pdf;base64,";
@Override
public void pageLoad() {
String attachGuid = getRequestParameter("attachGuid");
FrameAttachInfo frameAttachInfo = iAttachService.getAttachInfoDetail(attachGuid);
String pdfToBase64 = "";
InputStream inputStreamByInfo = iAttachService.getInputStreamByInfo(frameAttachInfo);
String contentType = frameAttachInfo.getContentType();
if(".docx".equalsIgnoreCase(contentType) || ".doc".equalsIgnoreCase(contentType) || ".txt".equalsIgnoreCase(contentType)) {
try {
Document document = new Document(inputStreamByInfo);
ByteArrayOutputStream pdfoutputStream = new ByteArrayOutputStream();
document.save(pdfoutputStream, SaveFormat.PDF);// 保存成pdf
pdfToBase64 = PDFToBase64(pdfoutputStream);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
addCallbackParam("url", pdfbase64+pdfToBase64);
}else {
try {
ByteArrayOutputStream reader = Reader(inputStreamByInfo);
pdfToBase64 = PDFToBase64(reader);
}
catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(".pdf".equalsIgnoreCase(contentType)) {
addCallbackParam("url", pdfbase64+pdfToBase64);
}
if(".png".equalsIgnoreCase(contentType) || ".jpg".equalsIgnoreCase(contentType) || ".gif".equalsIgnoreCase(contentType) || ".bmp".equalsIgnoreCase(contentType) || ".jpeg".equalsIgnoreCase(contentType)) {
addCallbackParam("url", pngbase64+pdfToBase64);
}
}
}
public ByteArrayOutputStream Reader(InputStream input) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
while ((len = input.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
} catch (IOException e) {
throw new Exception("Illegal flow.");
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return this.baos = baos;
}
public static String PDFToBase64(ByteArrayOutputStream pdfoutputStream) {
BASE64Encoder encoder = new BASE64Encoder();
//FileInputStream fin =null;
//BufferedInputStream bin =null;
ByteArrayOutputStream baos = null;
BufferedOutputStream bout =null;
try {
//fin = new FileInputStream(file);
//bin = new BufferedInputStream(fin);
baos = pdfoutputStream;
bout = new BufferedOutputStream(baos);
// byte[] buffer = new byte[1024];
// int len = bin.read(buffer);
// while(len != -1){
// bout.write(buffer, 0, len);
// len = bin.read(buffer);
// }
//刷新此输出流并强制写出所有缓冲的输出字节
bout.flush();
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
// fin.close();
// bin.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void pdfdown(String filePath,byte[] bytes){
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
bis = new BufferedInputStream(byteInputStream);
File file = new File(filePath);
File path = file.getParentFile();
if(!path.exists()){
path.mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int length = bis.read(buffer);
while(length != -1){
bos.write(buffer, 0, length);
length = bis.read(buffer);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (bis != null) {
bis.close();
}
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}