用struts2实现上传文件和转化文件为html

action

public class shangchuanAction extends ActionSupport{
	public File some;
	public String someFileName;
	public File getSome() {
		return some;
	}
	public void setSome(File some) {
		this.some = some;
	}
	public String getSomeFileName() {
		return someFileName;
	}
	public void setSomeFileName(String someFileName) {
		this.someFileName = someFileName;
	}
		public String excute() throws Exception {

		String path="/upload/"+ someFileName;
		String path1=ServletActionContext.getServletContext().getRealPath(path);
		File dic = new File(ServletActionContext.getServletContext().getRealPath("/upload"));
		if(!dic.exists()) {        //文件夹自动生成
			dic.mkdir();
		}
		System.out.println(path1); //输出上传到的地址
		File f = new File(dic,someFileName);
		try {
			//保存文件到项目的文件夹下
			FileUtils.copyFile(some,f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		WordReader.extractDoc(path1,path1.replace(".docx", ".html"));//转化word为html
		return "success";
	}

转换为html的类 只搞上传不用
下载jacob-1.19.zip解压后导入里面的jacob.jar包,复制相应的.dll文件到jdk和jre安装的bin目录下

package com.test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordReader {
	 public static void extractDoc(String inputFIle, String outputFile) {
	        boolean flag = false;
	        // 打开Word应用程序
	        ActiveXComponent app = new ActiveXComponent("Word.Application");
	        try {
	            // 设置word不可见
	            app.setProperty("Visible", new Variant(false));
	            // 打开word文件
	            Dispatch doc1 = app.getProperty("Documents").toDispatch();
	            Dispatch doc2 = Dispatch
	                .invoke(doc1, "Open", Dispatch.Method, new Object[] {inputFIle, new Variant(false), new Variant(true)},
	                        new int[1]).toDispatch();
	            // 作为html格式保存到临时文件::参数 new Variant(8)其中8表示word转html;7表示word转txt;44表示Excel转html。。。
	            Dispatch.invoke(doc2, "SaveAs", Dispatch.Method, new Object[] {outputFile, new Variant(8)}, new int[1]);
	            // 关闭word
	            Variant f = new Variant(false);
	            Dispatch.call(doc2, "Close", f);
	            flag = true;
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            app.invoke("Quit", new Variant[] {});
	        }
	        if (flag == true) {
	            System.out.println("Transformed Successfully");
	        } else {
	            System.out.println("Transform Failed");
	        }
	    }
}

上传jsp代码

<form action="shangchuanAction" method="post" enctype="multipart/form-data">
    选择一个文件:
    <input type="file" name="some"/>
    <br/><br/>
    <input type="submit" value="上传"/>
</form>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值