RandyJie

心灵的慰藉 岁月的痕迹 秋天了...我毫无收获...

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

 

 

在没有工作的日子里,一切都是那么的不充实,写了一个在线文档阅读器,拿出来安慰下自己吧,呵呵 - -!

 

主要框架结构:struts2.18 + BlazeDS + Spring2.5 + Hibernate3,这是一个多门面的架构,主要是为了练练手,JSP+flex交替使用。

通过struts上传文件,中间通过一些文件处理和转换(这里主要是借助 OpenOffice3.2,不需要安装版,USB版也可以),然后可以用jsp或flex查看已经上传的文档列表并可以在线阅读。

java to flex通信主要是通过BlazeDS 实现的,而BlazeDS 是与spring整合了。

下面贴出主要代码吧~~~

 

ConvertDocument.java
1 package com.onlinelook.util;
2
3  import java.io.File;
4  import java.io.InputStream;
5
6  import ooo.connector.BootstrapSocketConnector;
7
8  import com.sun.star.beans.PropertyValue;
9  import com.sun.star.frame.XComponentLoader;
10 import com.sun.star.frame.XStorable;
11 import com.sun.star.lang.XComponent;
12 import com.sun.star.lang.XMultiComponentFactory;
13 import com.sun.star.uno.UnoRuntime;
14 import com.sun.star.uno.XComponentContext;
15 import com.sun.star.util.XCloseable;
16
17 public class ConvertDocument {
18 /**
19 * 实例
20 * @param oooExeFolder
21 * @return
22 * @throws Exception
23 */
24 private XComponentContext createContext(String oooExeFolder)
25 throws Exception {
26 // get the remote office component context
27 // return Bootstrap.bootstrap();
28 return BootstrapSocketConnector.bootstrap(oooExeFolder);
29 }
30
31 /**
32 * 加载器
33 * @param context
34 * @return
35 * @throws Exception
36 */
37 private XComponentLoader createLoader(XComponentContext context)
38 throws Exception {
39 // get the remote office service manager
40 XMultiComponentFactory mcf = context.getServiceManager();
41 Object desktop = mcf.createInstanceWithContext(
42 "com.sun.star.frame.Desktop", context);
43 return UnoRuntime.queryInterface(XComponentLoader.class, desktop);
44 }
45
46 /**
47 * 加载文档
48 * @param loader
49 * @param inputFilePath
50 * @return
51 * @throws Exception
52 */
53 private Object loadDocument(XComponentLoader loader, String inputFilePath)
54 throws Exception {
55 // Preparing properties for loading the document
56 PropertyValue[] propertyValues = new PropertyValue[1];
57 propertyValues[0] = new PropertyValue();
58 propertyValues[0].Name = "Hidden";
59 propertyValues[0].Value = new Boolean(true);
60
61 // Composing the URL by replacing all backslashs
62 File inputFile = new File(inputFilePath);
63 String inputUrl = "file:///"
64 + inputFile.getAbsolutePath().replace('\\', '/');
65
66 return loader.loadComponentFromURL(inputUrl, "_blank", 0,
67 propertyValues);
68 }
69
70 /**
71 * 转换文档
72 * @param doc
73 * @param outputFilePath
74 * @param convertType
75 * @throws Exception
76 */
77 private void convertDocument(Object doc, String outputFilePath,
78 String convertType) throws Exception {
79 // Preparing properties for converting the document
80 PropertyValue[] propertyValues = new PropertyValue[2];
81 // Setting the flag for overwriting
82 propertyValues[0] = new PropertyValue();
83 propertyValues[0].Name = "Overwrite";
84 propertyValues[0].Value = new Boolean(true);
85 // Setting the filter name
86 propertyValues[1] = new PropertyValue();
87 propertyValues[1].Name = "FilterName";
88 propertyValues[1].Value = convertType;
89
90 // Composing the URL by replacing all backslashs
91 File outputFile = new File(outputFilePath);
92 String outputUrl = "file:///"
93 + outputFile.getAbsolutePath().replace('\\', '/');
94
95 // Getting an object that will offer a simple way to store
96 // a document to a URL.
97 XStorable storable = UnoRuntime.queryInterface(XStorable.class, doc);
98 // Storing and converting the document
99 // storable.storeAsURL(outputUrl, propertyValues);
100 storable.storeToURL(outputUrl, propertyValues);
101 }
102
103 /**
104 * 关闭文档
105 * @param doc
106 * @throws Exception
107 */
108 private void closeDocument(Object doc) throws Exception {
109 // Closing the converted document. Use XCloseable.clsoe if the
110 // interface is supported, otherwise use XComponent.dispose
111 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 * @param inputFilePath
126 * 将要转换的文档路径 re:D:\Program Files\Apache Software
127 * Foundation\Tomcat 6.0\webapps\onlinelook\doc\12345.doc
128 * @param swfOutPath
129 * 为输出文件夹 服务器路径 flash/swf
130 * @param oooSdkPath
131 * OpenOffice的路径 D:/Program Files/OpenOfficePortable/
132 * @param realPath
133 * 服务器根目录(项目) D:\Program Files\Apache Software Foundation\Tomcat
134 * 6.0\webapps\onlinelook
135 * @return SWF路径 (web)
136 * @throws Exception
137 */
138 public String 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 boolean isPdf = false;
151
152 String path = this.getClass().getResource("/").getPath();
153 path = path.substring(1);
154 int last = inputFilePath.lastIndexOf('.');
155
156 if (inputFilePath.substring(last + 1).equalsIgnoreCase("PDF"))
157 isPdf = true;
158
159 String fileName = inputFilePath.substring(inputFilePath
160 .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 to
166 StringBuffer buffer = new StringBuffer();
167 buffer.append("\"").append(path.replaceAll("%20", " ")).append(
168 "pdf2swf.exe\" \"").append(pdfPath).append(
169 "\" -s zoom=110 -o \"").append(swfOutPath).append(
170 "\" -t -T 9 -f");
171
172 final String convertType = "writer_pdf_Export";
173 final String sdkPath = oooSdkPath;
174 final String input = inputFilePath;
175 final String execute = buffer.toString();
176 final boolean fileIsPdf = isPdf;
177 Runnable run1 = new Runnable() {
178 @Override
179 public void run() {
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 = new byte[stream.available()];
192 stream.read(bs);
193 }
194 }
195 Thread.sleep(500);
196 }
197 } catch (Exception e) {
198 //
199 }
200 File file = new File(pdfPath);
201 if (!fileIsPdf && file.exists())
202 file.delete();//删除临时文件,如果输入文件是PDF则没有临时文件
203 }
204
205 };
206 final Thread thread1 = new Thread(run1);//SWF处理线程
207
208
209 Runnable run = new Runnable() {
210 @Override
211 public void run() {
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);// close
219 thread1.start();//开启SWF处理线程
220 // process.waitFor();//线程等待
221 } catch (Exception e) {
222 e.printStackTrace();
223 }
224 }
225 };
226 Thread thread = new Thread(run);//文档转换的处理线程
227 thread.setPriority(Thread.MIN_PRIORITY);//设置级别
228
229 if (fileIsPdf)//如果输入的文件是PDF 不进行转换处理
230 thread1.start();
231 else
232 thread.start();
233
234 try {
235 Thread.sleep(10);//线程休息 哈哈 - - 原线程回家报告
236 } catch (InterruptedException e) {
237
238 }
239 return outFilePath;
240 }
241
242 /**
243 * 和谐的字符串处理 - -
244 * @param string
245 * @return
246 */
247 private String subpath(String string) {
248 string = string.replace("\\", "/");
249 if (string.charAt((string.length() - 1)) == '/')
250 string = string.substring(0, string.length() - 1);
251 return string;
252 }
253 }
254
255
256

 

posted on 2010-09-01 16:36  RandyJie  阅读(1510)  评论(2编辑  收藏  举报