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>