java world 转富文本_JAVA导出下载word文档(导出带富文本图片word)

该博客介绍了如何使用Java将图片转换为Base64编码,并将其嵌入到Word文档中,以实现导出带有富文本和图片的Word文档。提供了相关方法如`imageToBase64`用于图片转Base64,以及`generateImageBase64Block`生成图片块。
摘要由CSDN通过智能技术生成

importjava.awt.image.BufferedImage;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.math.BigDecimal;importjava.util.UUID;importjavax.imageio.ImageIO;importorg.apache.commons.codec.binary.Base64;importsun.misc.BASE64Encoder;/*** @Description:WORD 文档图片转换器

**/

public classWordImageConvertor {//private static Const WORD_IMAGE_SHAPE_TYPE_ID="";

/*** @Description: 将图片转换成base64编码的字符串

*@param @paramimageSrc 文件路径

*@param @return*@returnString

*@throwsIOException

*@throws

*/

public static String imageToBase64(String imageSrc) throwsIOException{//判断文件是否存在

File file=newFile(imageSrc);if(!file.exists()){throw new FileNotFoundException("文件不存在!");

}

StringBuilder pictureBuffer= newStringBuilder();

FileInputStream input=newFileInputStream(file);

ByteArrayOutputStream out= newByteArrayOutputStream();//读取文件//BufferedInputStream bi=new BufferedInputStream(in);

Base64 base64=newBase64();

BASE64Encoder encoder=newBASE64Encoder();byte[] temp = new byte[1024];for(int len = input.read(temp); len != -1;len =input.read(temp)){

out.write(temp,0, len);//out(pictureBuffer.toString());//out.reset();

}

pictureBuffer.append(newString( base64.encodeBase64Chunked(out.toByteArray())));//pictureBuffer.append(encoder.encodeBuffer(out.toByteArray()));

/*byte[] data=new byte[input.available()];

input.read(data);

pictureBuffer.append(base64.encodeBase64String (data));*/input.close();/*BASE64Decoder decoder=new BASE64Decoder();

FileOutputStream write = new FileOutputStream(new File("c:\\test2.jpg"));

//byte[] decoderBytes = decoder.decodeBuffer (pictureBuffer.toString());

byte[] decoderBytes = base64.decodeBase64(pictureBuffer.toString());

write.write(decoderBytes);

write.close();*/

returnpictureBuffer.toString();

}public staticString toDocBodyBlock(

String imageFilePath,

String imageFielShortName,intimageHeight,intimageWidth,

String imageStyle,

String srcLocationShortName,

String shapeidPrex,String spidPrex,String typeid){//shapeid//mht文件中针对shapeid的生成好像规律,其内置的生成函数没法得知,但是只要保证其唯一就行//这里用前置加32位的uuid来保证其唯一性。

String shapeid=shapeidPrex;

shapeid+=UUID.randomUUID().toString();//spid ,同shapeid处理

String spid=spidPrex;

spid+=UUID.randomUUID().toString();/*

src=3D"file9462.files/image002.jpg" alt=3D725017921264249223.jpg v:shapes=

=3D"_x56fe__x7247__x0020_0">*/StringBuilder sb1=newStringBuilder();

sb1.append(" ");//以下是为了兼容游览器显示时的效果,但是如果是纯word阅读的话没必要这么做。

/*StringBuilder sb2=new StringBuilder();

sb2.append(" ");

sb2.append("

" src=3D\"" + srcLocationShortName +"\" alt=" +imageFielShortName+

" v:shapes=3D\"" + shapeid +"\">");

sb2.append("");*/

//return sb1.toString()+sb2.toString();

returnsb1.toString();

}/*** @Description: 生成图片的base4块

*@param @paramnextPartId

*@param @paramcontextLoacation

*@param @paramContentType

*@param @parambase64Content

*@param @return*@returnString

*@throws

*/

public staticString generateImageBase64Block(String nextPartId,String contextLoacation,

String fileTypeName,String base64Content){/*--=_NextPart_01D188DB.E436D870

Content-Location: file:///C:/70ED9946/file9462.files/image001.jpg

Content-Transfer-Encoding: base64

Content-Type: image/jpeg

base64Content*/StringBuilder sb=newStringBuilder();

sb.append("\n");

sb.append("\n");

sb.append("------=_NextPart_"+nextPartId);

sb.append("\n");

sb.append("Content-Location: "+contextLoacation);

sb.append("\n");

sb.append("Content-Transfer-Encoding: base64");

sb.append("\n");

sb.append("Content-Type: " +getImageContentType(fileTypeName));

sb.append("\n");

sb.append("\n");

sb.append(base64Content);returnsb.toString();

}private static String generateImageBodyBlockStyleAttr(String imageFilePath, int height,intwidth){

StringBuilder sb=newStringBuilder();

BufferedImage sourceImg;try{

sourceImg= ImageIO.read(newFileInputStream(imageFilePath));if(height==0){

height=sourceImg.getHeight();

}if(width==0){

width=sourceImg.getWidth();

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}//将像素转化成pt

BigDecimal heightValue=new BigDecimal(height*12/16);

heightValue= heightValue.setScale(2, BigDecimal.ROUND_HALF_UP);

BigDecimal widthValue=new BigDecimal(width*12/16);

widthValue= widthValue.setScale(2, BigDecimal.ROUND_HALF_UP);

sb.append("height:"+heightValue +"pt;");

sb.append("width:"+widthValue +"pt;");

sb.append("visibility:visible;");

sb.append("mso-wrap-style:square; ");returnsb.toString();

}private staticString getImageContentType(String fileTypeName){

String result="image/jpeg";//http://tools.jb51.net/table/http_content_type

if(fileTypeName.equals("tif") || fileTypeName.equals("tiff")){

result="image/tiff";

}else if(fileTypeName.equals("fax")){

result="image/fax";

}else if(fileTypeName.equals("gif")){

result="image/gif";

}else if(fileTypeName.equals("ico")){

result="image/x-icon";

}else if(fileTypeName.equals("jfif") || fileTypeName.equals("jpe")||fileTypeName.equals("jpeg") ||fileTypeName.equals("jpg")){

result="image/jpeg";

}else if(fileTypeName.equals("net")){

result="image/pnetvue";

}else if(fileTypeName.equals("png") || fileTypeName.equals("bmp") ){

result="image/png";

}else if(fileTypeName.equals("rp")){

result="image/vnd.rn-realpix";

}else if(fileTypeName.equals("rp")){

result="image/vnd.rn-realpix";

}returnresult;

}public staticString getFileSuffix(String srcRealPath){int lastIndex = srcRealPath.lastIndexOf(".");

String suffix= srcRealPath.substring(lastIndex + 1);//String suffix = srcRealPath.substring(srcRealPath.indexOf(".")+1);

returnsuffix;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值