用java将word文件上传到服务器,把word里面的内容保存到数据库
使用java中的io进行读取BufferedReader bufferedReader = null;File file = new File("文档地址+文档名.docx");if(!file.exists()){System.out.println("文件不存在");} else {bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"));String lineText = null;while((lineText = bufferedReader.readLine()) != null){if (linText != null && !lineText.eq("")){System.out.println("一次读取一行,一行内容为:" + lineText);}}}
Java程序把Word文档直接换成Html文件方法是什么呢?
acob是Java和Windows下的Com桥,通过它我们可以在Java程序中调用COM组件。
如果你的JDK是1.4,那你需要下载Jacob 1.9的jni库才能正常运行,早期版本在JDK 1.4下有些问题。
以下是引用片段:package com;/*** 〈p〉Title:Word文档转html类〈/p〉* 〈p〉Description: 〈/p〉* 〈p〉Copyright:() 2002〈/p〉* @author 舵手* @version 1.0*/import com.jacob.com.*;import com.jacob.activeX.*;public class WordtoHtml {/***文档转换函数*@param docfile word文档的绝对路径加文件名(包含扩展名)*@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)*/public static void change(String docfile, String htmlfile) {ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动wordtry {app.setProperty("Visible", new Variant(false));//设置word不可见Object docs = app.getProperty("Documents").toDispatch();Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[]{ docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();// 打开word文件Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile,new Variant (8) }, new int[1]);// 作为html格式保存到临时文件Variant f = new Variant(false);Dispatch.call(doc, "Close", f);} catch (Exception e) {e.printStackTrace();} finally {app.invoke("Quit", new Variant[]{});}}public static void main(String[] strs){WordtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t");}}
java上传的文档如何在页面上显示出来
置了网页上的word文件在浏览器中打开(工具->选项->常规->web选项->浏览器)里面设置。
或者服务器端写代码:2,public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {res.setCharacterEncoding("GBK");res.setContentType("application/msword");File f = new File(xxxx);BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));BufferedOutputStream bos = new BufferedOutputStream(res.getOutputStream());byte buff[] = new byte[4096];int bytesRead;while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))bos.write(buff, 0, bytesRead);if (bis != null)bis.close();if (bos != null) {bos.flush();bos.close();}}
Java怎么操作OpenOffice创建word文档并向其设置内容
创建Openoffice连接OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);try {//:\Program Files (x86)\OpenOffice,其实也可以是MS Office.out,等待eclipse打开;program>soffice -headless -accept="socket;);*** 将Word文档转换成html字符串的工具类** @author MZULE**/,等待下载。
4、安装OpenOffice..");e.printStackTrace();}//public class Doc2Html {public static void main(String[] args) {System,安装结束后;"//,说明一下,本文的测试基于 MS Win7 Ultimate X64 系统。
下面就是规规矩矩的实现。
1、下载OpenOffice,启动OpenOffice的一项服务。
* commons-io* jodconverter* juh* jurt* ridl* slf4j-api* slf4j-jdk14* unoil* xstream 8;获取OpenOffice连接失败..artofsolving.jodconverter将Word转Html的原理是这样的:1、客户上传Word文档到服务器2、服务器调用OpenOffice程序打开上传的Word文档3、OpenOffice将Word文档另存为Html格式4、Over至此可见.Pattern.getTime()+ ".util;import java.io.FileInputStream.artofsolving.jodconverter,调用cmd.io.InputStreamReader;import java.net.ConnectException;import com.artofsolving,port=8100,你懂的; 连接con.connect();} catch (ConnectException e) {System..查看代码package com.mzule.doc2html.println(toHtmlString(new File(".SocketOpenOfficeConnection;import com; + new Date();}/、下载Jodconverter 这是一个开启OpenOffice进行格式转化的第三方jar包。
3、泡杯热茶。
** @param docFile* 需要转换的文档* @param filepath* 文档中图片的保存位置* @return 转换成功的html代码*/.util.Date;);//"*** 将word转换成html文件,并且获取html文件代码, "C:/test"return htmlFile.openoffice.connection。
恩.io.FileNotFoundException;import java.io.artofsolving.jodconverter、Coding..out;import java;import java,2.openoffice.converter.OpenOfficeDocumentConverter;/C、打开eclipse6、喝杯热茶;/ 转换文档问htmlconverter.convert(docFile, htmlFile):C.regex.org 3\.println(" 关闭openoffice连接con.disconnect().BufferedReader;import java.io.openoffice.connection;import java; 创建转换器DocumentConverter converter = new OpenOfficeDocumentConverter(con);/urp;5;));}/*** 将word文档转换成html文档** @param docFile* 需要转换的word文档* @param filepath* 转换之后html的存放路径* @return 转换之后的html文件*/, String filepath) {/、新建eclipse项目;import java。
7,导入Jodconverter/lib 下得jar包.html".File;import java.io;/ 创建保存html的文件File htmlFile = new File(filepath + "/.IOException:/test/public static File convert(File docFile.util.regex.Matcher;import java.util.jodconverter.DocumentConverter;import com.OpenOfficeConnection;import com;test.doc"public static String toHtmlString(File docFile, String filepath) {// 转换word文档File htmlFile = convert(docFile, filepath);// 获取html文件流StringBuffer htmlSb = new StringBuffer();try {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlFile)));while (br.ready()) {htmlSb.append(br.readLine());}br.close();// 删除临时文件htmlFile.delete();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}// HTML文件字符串String htmlStr = htmlSb.toString();// 返回经过清洁的html文本return clearFormat(htmlStr, filepath);}/*** 清除一些不需要的html标记** @param htmlStr* 带有复杂html标记的html语句* @return 去除了不需要html标记的语句*/protected static String clearFormat(String htmlStr, String docImgPath) {// 获取body内容的正则String bodyReg = "";Pattern bodyPattern = Pattern.compile(bodyReg);Matcher bodyMatcher = bodyPattern.matcher(htmlStr);if (bodyMatcher.find()) {// 获取BODY内容,并转化BODY标签为DIVhtmlStr = bodyMatcher.group().replaceFirst("", "");}// 调整图片地址htmlStr = htmlStr.replaceAll("转换成保留样式// content = content.replaceAll("(]*>.*?)()",// "");// 把转换成并删除样式htmlStr = htmlStr.replaceAll("(]*)(>.*?)()", "");// 删除不需要的标签htmlStr = htmlStr.replaceAll("]*?>","");// 删除不需要的属性htmlStr = htmlStr.replaceAll("]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>","");return htmlStr;}},这要求服务器端安装OpenOffice软件,不过OpenOffice的优势是跨平台 展开
java实现word文档自动转成pdf文档我现在在做一个网站,客
没自动的..你可以把软件安装好了点文件-导入\导出 来转换成PDF文件 也可以用WPS, 这是我自己下载了在用的,很好用的版本: Office.2003.SP2.龙卷风大企业版 V2.2 2.2 658M ISO 此ISO整合SP2升级包,免激活,免序列号,支持在线更新 包含下列组件: Microsoft Office Word 2003 Microsoft Office Excel 2003 Microsoft Office PowerPoint 2003 Microsoft Office FrontPage 2003 Microsoft Office Access 2003 Microsoft Office Outlook 2003 Microsoft Office OneNote 2003 Microsoft Office Visio 2003 Microsoft Office InfoPath 2003 Microsoft Office Publisher 2003 Microsoft Office Project 2003 安装说明:先卸载你原来的版本,然后全新安装此版即可
java中关于上传文件,修改上传文件信息时怎么让之前上传的文件在上...
我说一下一般的步骤吧:1. 上传文件。
2. 将文件路径、文件名称等信息存入数据库或者session缓存中3. 再次上传的时候读取上传记录,将已上传的文件信息列出,并增加删除和修改选项。
4. 如果进行删除操作,根据保存的记录获取需要删除的文件路径,删除之。
...
JAVA中如何把WORD文档直接转换成html?
jaco是java和windows下的com桥,通过它我们可以在java程序中调用COM组件。
如果你的JDK是1.4,那你需要下载jaco1.9的jni库才能正常运行,早期版本在JDK1.4下有些问题。
package com;** * Title:Wod文档转html类 * Desciption: * Copyight:() 2002 * @autho 舵手 * @vesion 1.0 *impot com.jaco.com.*;impot com.jaco.activeX.*; pulic class WodtoHtml { ** *文档转换函数 *@paam docfile wod文档的绝对路径加文件名(包含扩展名) *@paam htmlfile 转换后的html文件绝对路径和文件名(不含扩展名) * pulic static void change(Sting docfile, Sting htmlfile) { ActiveXComponent app = new ActiveXComponent("Wod.Application"); 启动wod ty { app.setPopety("Visile", new Vaiant(false)); 设置wod不可见 Oject docs = app.getPopety("Documents").toDispatch(); Oject doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Oject[] { docfile, new Vaiant(false),new Vaiant(tue) }, new int[1]).toDispatch(); 打开wod文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Oject[] {htmlfile, new Vaiant(8) }, new int[1]); 作为html格式保存到临时文件 Vaiant f = new Vaiant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.pintStackTace(); } finally { app.invoke("Quit", new Vaiant[]{}); } } pulic static void main(Sting[] sts){ WodtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t"); }}
java怎么上传大文件
Java Excel API 文档 http://www.andykhan.com/jexcelapi/ 1、一个jacob操作Word的例子,其他操作excel,pdf的sample里都有 import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class WordTest { public static void main(String[] args) { WordBean word=new WordBean(); word.openWord(true); word.createNewDocument(); word.insertText("Hello word."); } } import com.jacob.activeX.*; import com.jacob.com.*; public class WordBean extends java.awt.Panel { private ActiveXComponent MsWordApp = null; private Dispatch document = null; public WordBean() { super(); } public void openWord(boolean makeVisible) { //Open Word if we've not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word.Application"); } //Set the visible property as required. Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible)); } public void createNewDocument() { //Find the Documents collection object maintained by Word Dispatch documents = Dispatch.get(MsWordApp,"Documents").toDispatch(); //Call the Add method of the Documents collection to create //a new document to edit document = Dispatch.call(documents,"Add").toDispatch(); } public void insertText(String textToInsert) { // Get the current selection within Word at the moment. If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); //Put the specified text at the insertion point Dispatch.put(selection,"Text",textToInsert); } public void saveFileAs(String filename) { Dispatch.call(document,"SaveAs",filename); } public void printFile() { //Just print the current document to the default printer Dispatch.call(document,"PrintOut"); } public void closeDocument() { // Close the document without saving changes // 0 = wdDoNotSaveChanges // -1 = wdSaveChanges // -2 = wdPromptToSaveChanges Dispatch.call(document, "Close", new Variant(0)); document = null; } public void closeWord() { Dispatch.call(MsWordApp,"Quit"); MsWordApp = null; document = null; } }
转载请注明出处51数据库 » java 上传word文档