java在线文档编写_JAVA写的一个类似豆丁网的在线文档阅读

1 packagecom.onlinelook.util;2 3 importjava.io.File;4 importjava.io.InputStream;5 6 importooo.connector.BootstrapSocketConnector;7 8 importcom.sun.star.beans.PropertyValue;9 importcom.sun.star.frame.XComponentLoader;10 importcom.sun.star.frame.XStorable;11 importcom.sun.star.lang.XComponent;12 importcom.sun.star.lang.XMultiComponentFactory;13 importcom.sun.star.uno.UnoRuntime;14 importcom.sun.star.uno.XComponentContext;15 importcom.sun.star.util.XCloseable;16 17 publicclassConvertDocument {18 /**19 * 实例20 *@paramoooExeFolder21 *@return22 *@throwsException23 */24 privateXComponentContext createContext(String oooExeFolder)25 throwsException {26 //get the remote office component context27 //return Bootstrap.bootstrap();28 returnBootstrapSocketConnector.bootstrap(oooExeFolder);29 }30 31 /**32 * 加载器33 *@paramcontext34 *@return35 *@throwsException36 */37 privateXComponentLoader createLoader(XComponentContext context)38 throwsException {39 //get the remote office service manager40 XMultiComponentFactory mcf=context.getServiceManager();41 Object desktop=mcf.createInstanceWithContext(42 "com.sun.star.frame.Desktop", context);43 returnUnoRuntime.queryInterface(XComponentLoader.class, desktop);44 }45 46 /**47 * 加载文档48 *@paramloader49 *@paraminputFilePath50 *@return51 *@throwsException52 */53 privateObject loadDocument(XComponentLoader loader, String inputFilePath)54 throwsException {55 //Preparing properties for loading the document56 PropertyValue[] propertyValues=newPropertyValue[1];57 propertyValues[0]=newPropertyValue();58 propertyValues[0].Name="Hidden";59 propertyValues[0].Value=newBoolean(true);60 61 //Composing the URL by replacing all backslashs62 File inputFile=newFile(inputFilePath);63 String inputUrl="file:///"64 +inputFile.getAbsolutePath().replace('\\','/');65 66 returnloader.loadComponentFromURL(inputUrl,"_blank",0,67 propertyValues);68 }69 70 /**71 * 转换文档72 *@paramdoc73 *@paramoutputFilePath74 *@paramconvertType75 *@throwsException76 */77 privatevoidconvertDocument(Object doc, String outputFilePath,78 String convertType)throwsException {79 //Preparing properties for converting the document80 PropertyValue[] propertyValues=newPropertyValue[2];81 //Setting the flag for overwriting82 propertyValues[0]=newPropertyValue();83 propertyValues[0].Name="Overwrite";84 propertyValues[0].Value=newBoolean(true);85 //Setting the filter name86 propertyValues[1]=newPropertyValue();87 propertyValues[1].Name="FilterName";88 propertyValues[1].Value=convertType;89 90 //Composing the URL by replacing all backslashs91 File outputFile=newFile(outputFilePath);92 String outputUrl="file:///"93 +outputFile.getAbsolutePath().replace('\\','/');94 95 //Getting an object that will offer a simple way to store96 //a document to a URL.97 XStorable storable=UnoRuntime.queryInterface(XStorable.class, doc);98 //Storing and converting the document99 //storable.storeAsURL(outputUrl, propertyValues);100 storable.storeToURL(outputUrl, propertyValues);101 }102 103 /**104 * 关闭文档105 *@paramdoc106 *@throwsException107 */108 privatevoidcloseDocument(Object doc)throwsException {109 //Closing the converted document. Use XCloseable.clsoe if the110 //interface is supported, otherwise use XComponent.dispose111 XCloseable closeable=UnoRuntime.queryInterface(XCloseable.class, doc);112 113 if(closeable!=null) {114 closeable.close(false);115 }else{116 XComponent component=UnoRuntime.queryInterface(XComponent.class,117 doc);118 component.dispose();119 }120 }121 122 String pdfPath=null;123 124 /**125 *@paraminputFilePath126 * 将要转换的文档路径 re:D:\Program Files\Apache Software127 * Foundation\Tomcat 6.0\webapps\onlinelook\doc\12345.doc128 *@paramswfOutPath129 * 为输出文件夹 服务器路径 flash/swf130 *@paramoooSdkPath131 * OpenOffice的路径 D:/Program Files/OpenOfficePortable/132 *@paramrealPath133 * 服务器根目录(项目) D:\Program Files\Apache Software Foundation\Tomcat134 * 6.0\webapps\onlinelook135 *@returnSWF路径 (web)136 *@throwsException137 */138 publicString convert(String inputFilePath, String swfOutPath,139 String oooSdkPath, String realPath) {140 141 //一大堆的字符串处理142 //目的:把\\和/统一和谐掉,统一格式143 inputFilePath=subpath(inputFilePath);144 realPath=subpath(realPath);145 swfOutPath=subpath(swfOutPath);146 String outFilePath=swfOutPath;147 swfOutPath=realPath+"/"+swfOutPath;148 oooSdkPath=subpath(oooSdkPath);149 150 booleanisPdf=false;151 152 String path=this.getClass().getResource("/").getPath();153 path=path.substring(1);154 intlast=inputFilePath.lastIndexOf('.');155 156 if(inputFilePath.substring(last+1).equalsIgnoreCase("PDF"))157 isPdf=true;158 159 String fileName=inputFilePath.substring(inputFilePath160 .lastIndexOf('/')+1, last);161 pdfPath=inputFilePath.substring(0, last+1)+"pdf";162 swfOutPath+="/"+fileName+".swf";163 outFilePath+="/"+fileName+".swf";164 oooSdkPath=oooSdkPath+"/App/openoffice/program";165 //the given type to convert to166 StringBuffer buffer=newStringBuffer();167 buffer.append("\"").append(path.replaceAll("%20","")).append(168 "pdf2swf.exe\"\"").append(pdfPath).append(169 "\"-s zoom=110-o \"").append(swfOutPath).append(170 "\"-t-T9-f");171 172 finalString convertType="writer_pdf_Export";173 finalString sdkPath=oooSdkPath;174 finalString input=inputFilePath;175 finalString execute=buffer.toString();176 finalbooleanfileIsPdf=isPdf;177 Runnable run1=newRunnable() {178 @Override179 publicvoidrun() {180 Process process=null;181 try{182 process=Runtime.getRuntime().exec(execute);183 while(true) {184 try{185 process.exitValue();//这个不报异常。就是完成命令了 - -186 break;187 }catch(IllegalThreadStateException e) {188 //把输出流给处理掉,防止线程堵塞189 InputStream stream=process.getInputStream();190 if(stream.available()>0) {191 byte[] bs=newbyte[stream.available()];192 stream.read(bs);193 }194 }195 Thread.sleep(500);196 }197 }catch(Exception e) {198 //199 }200 File file=newFile(pdfPath);201 if(!fileIsPdf&&file.exists())202 file.delete();//删除临时文件,如果输入文件是PDF则没有临时文件203 }204 205 };206 finalThread thread1=newThread(run1);//SWF处理线程207 208 209 Runnable run=newRunnable() {210 @Override211 publicvoidrun() {212 XComponentContext context;213 try{214 context=createContext(sdkPath);//ooo的SDK位置215 XComponentLoader compLoader=createLoader(context);//创建一个加载器216 Object doc=loadDocument(compLoader, input);//加载文件217 convertDocument(doc, pdfPath, convertType);//转换文件218 closeDocument(doc);//close219 thread1.start();//开启SWF处理线程220 //process.waitFor();//线程等待221 }catch(Exception e) {222 e.printStackTrace();223 }224 }225 };226 Thread thread=newThread(run);//文档转换的处理线程227 thread.setPriority(Thread.MIN_PRIORITY);//设置级别228 229 if(fileIsPdf)//如果输入的文件是PDF 不进行转换处理230 thread1.start();231 else232 thread.start();233 234 try{235 Thread.sleep(10);//线程休息 哈哈 - - 原线程回家报告236 }catch(InterruptedException e) {237 238 }239 returnoutFilePath;240 }241 242 /**243 * 和谐的字符串处理 - -244 *@paramstring245 *@return246 */247 privateString subpath(String string) {248 string=string.replace("\\","/");249 if(string.charAt((string.length()-1))=='/')250 string=string.substring(0, string.length()-1);251 returnstring;252 }253 }254 255 256

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值