在java程序里如何换图片_java程序替换word2007中的图片

1、新建word2007文档h2do.docx;

2、QQ截图粘贴到文档,添加些文字;

3、用winrar打开(修改扩展名docx为rar或右键->打开方式->选择winrar程序),目录结构如下:

.

│  [Content_Types].xml

├─docProps

│      app.xml

│      core.xml

├─word

│  │  comments.xml

│  │  document.xml

│  │  endnotes.xml

│  │  fontTable.xml

│  │  footnotes.xml

│  │  numbering.xml

│  │  settings.xml

│  │  styles.xml

│  │  webSettings.xml

│  │

│  ├─media

│  │      image1.png

│  │      image2.png

│  │

│  ├─theme

│  │      theme1.xml

│  │

│  └─_rels

│          document.xml.rels

└─_rels

.rels

以下代码通过java程序替换word文件中的图片image1.png

public static void main(String[] args) throws Throwable

{

ZipInputStream  zis = new ZipInputStream (new FileInputStream ("h2do.docx") );

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("e2say.docx"));

ZipEntry ze = null;

byte[] b = new byte[1024];

while((ze = zis.getNextEntry()) != null)

{

System.out.println(ze.getName() + ":" + ze.getMethod());

ZipEntry entry = new ZipEntry(ze.getName());

zos.putNextEntry(entry);

/*将源压缩文件中每个文件写至新压缩文件*/

InputStream is = zis;

if(ze.getName().endsWith(".xml"))

{

String line = null;

BufferedReader br = new BufferedReader(new InputStreamReader(is));

while((line = br.readLine()) != null)

{

//TODO:进行查证替换

zos.write((line+"\r\n").getBytes());

}

}else{//非xml文件,二进制流数据

//替换图片

if(ze.getName().endsWith("image1.png")){

is = new FileInputStream("temp.png");

}

int r = -1;

while((r = is.read(b)) != -1){

zos.write(b, 0, r);

}

}

if(is != zis){

is.close();

}

}

zos.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值