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;
}
}