Skip to content

Commit 3ca8542

Browse files
committed
..
1 parent 99ed91c commit 3ca8542

15 files changed

Lines changed: 629 additions & 197 deletions

File tree

.idea/artifacts/docviewer.xml

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 459 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docviewer/src/com/log4ic/servlet/DocViewerServlet.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ public void getDocInfo(HttpServletRequest request, HttpServletResponse response)
4242
return;
4343
}
4444

45-
response.setHeader("Cache-Control", "private");
46-
response.setHeader("Pragma", "no-cache");
47-
response.setHeader("Connection", "Keep-Alive");
48-
response.setHeader("Proxy-Connection", "Keep-Alive");
4945
response.setContentType("application/json");
46+
response.setStatus(202);
5047
response.flushBuffer();
5148
PrintWriter writer = response.getWriter();
5249

@@ -75,6 +72,8 @@ public void getDocInfo(HttpServletRequest request, HttpServletResponse response)
7572
} else {
7673
writer.write("{\"uri\":\"" + docUri + "\",\"permissions\":" + permissions.ordinal() + "}");
7774
}
75+
writer.flush();
76+
writer.close();
7877
} catch (Exception e) {
7978
e.printStackTrace();
8079
response.setStatus(404);
@@ -104,9 +103,6 @@ public void getDoc(HttpServletRequest request, HttpServletResponse response) {
104103
} else {
105104
docId = Integer.parseInt(doc);
106105
}
107-
if (DocViewer.isEncryption()) {
108-
secretKey = request.getSession().getAttribute("secretKey") + "";
109-
}
110106
} catch (Exception e) {
111107
response.setStatus(404);
112108
return;
@@ -117,6 +113,9 @@ public void getDoc(HttpServletRequest request, HttpServletResponse response) {
117113
try {
118114
LOGGER.info("获取文档!");
119115
if (DocViewer.getDoc(docId) != null) {
116+
if (DocViewer.isEncryption()) {
117+
secretKey = request.getSession().getAttribute("secretKey") + "";
118+
}
120119
outp = response.getOutputStream();
121120
if (DocViewer.isSplitPage()) {
122121
if (DocViewer.isEncryption()) {
@@ -169,6 +168,7 @@ public void getDoc(HttpServletRequest request, HttpServletResponse response) {
169168
}
170169
if (outp != null) {
171170
try {
171+
outp.flush();
172172
outp.close();
173173
} catch (IOException e) {
174174
e.printStackTrace();

docviewerapi/src/com/log4ic/DocViewer.java

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
import com.log4ic.utils.convert.*;
77
import com.log4ic.utils.convert.office.OfficeConverter;
88
import com.log4ic.utils.convert.pdf.PDFConverter;
9-
import com.log4ic.utils.filter.SwfFileFilter;
9+
import com.log4ic.utils.filter.SplitSwfFileFilter;
1010
import com.log4ic.utils.security.XXTEA;
1111
import org.apache.commons.lang.StringUtils;
1212
import org.apache.commons.logging.Log;
1313
import org.apache.commons.logging.LogFactory;
1414
import org.artofsolving.jodconverter.office.OfficeConnectionProtocol;
1515

16-
import java.io.File;
17-
import java.io.FileOutputStream;
18-
import java.io.InputStream;
19-
import java.io.OutputStream;
16+
import java.io.*;
2017
import java.util.LinkedList;
2118
import java.util.Properties;
2219
import java.util.Random;
@@ -281,8 +278,15 @@ public synchronized static int getNextId() {
281278
private static final ConvertQueue pdfQueue = new ConvertQueue(PDF_POOL_MAX_THREAD, "pdf_queue");
282279

283280

281+
/**
282+
* 获取转换后的文档目录 如果没有转换则进行转换
283+
*
284+
* @param id
285+
* @return 文档所在目录
286+
* @throws Exception
287+
*/
284288
public static File getDoc(int id) throws Exception {
285-
if (DocViewer.hasDocDir(id)) {
289+
if (DocViewer.isConverting(id) || hasDoc(id)) {
286290
while (DocViewer.isConverting(id)) {
287291
Thread.sleep(500);
288292
}
@@ -362,9 +366,52 @@ public static File getPDFDoc(int id) throws Exception {
362366
return DocViewerConverter.toPDF(in, OUTPUT_PATH);
363367
}
364368

365-
369+
/**
370+
* 获取文档页数 没有则创建
371+
*
372+
* @param id
373+
* @return
374+
* @throws Exception
375+
*/
366376
public static int getDocPageCount(int id) throws Exception {
367-
return getDoc(id).listFiles(new SwfFileFilter()).length;
377+
return getDocPageCount(id, true);
378+
}
379+
380+
/**
381+
* 获取文档页数
382+
*
383+
* @param id 文档id
384+
* @param isCreate 如果文档不存在,是否从数据源创建文档
385+
* @return
386+
* @throws Exception
387+
*/
388+
public static int getDocPageCount(int id, boolean isCreate) throws Exception {
389+
File dir = null;
390+
if (isCreate) {
391+
dir = getDoc(id);
392+
} else {
393+
dir = new File(OUTPUT_PATH + id);
394+
}
395+
if (dir != null && dir.exists() && dir.isDirectory()) {
396+
File info = new File(FileUtils.appendFileSeparator(dir.getPath()) + "info");
397+
398+
if (info.exists() && info.isFile()) {
399+
400+
FileInputStream in = new FileInputStream(info);
401+
402+
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
403+
404+
try {
405+
return Integer.parseInt(reader.readLine());
406+
} catch (Exception e) {
407+
} finally {
408+
reader.close();
409+
in.close();
410+
}
411+
}
412+
413+
}
414+
return 0;
368415
}
369416

370417
public static File getDocFileFromSource(int id) throws Exception {
@@ -408,30 +455,59 @@ public static File getDocFileFromSource(int id) throws Exception {
408455

409456
public static boolean hasDocDir(int id) {
410457

411-
File file = new File(OUTPUT_PATH + id);
458+
File dir = new File(OUTPUT_PATH + id);
412459

413-
if (!file.exists()) {
460+
if (!dir.exists()) {
414461
return false;
415462
}
416463

417-
if (file.isDirectory()) {
464+
if (dir.isDirectory()) {
418465
return true;
419466
}
420467

421468
return false;
422469
}
423470

424471

425-
public static boolean hasDoc(int id) {
472+
public static boolean hasPDF(int id) {
426473

427-
File file = new File(OUTPUT_PATH + id);
474+
if (!hasDocDir(id)) {
475+
return false;
476+
}
477+
478+
File file = new File(OUTPUT_PATH + id + File.separator + id + ".pdf");
428479

429480
if (!file.exists()) {
430481
return false;
431482
}
432483

433-
if (file.isDirectory() && file.list().length > 0) {
434-
return true;
484+
return true;
485+
}
486+
487+
/**
488+
* 是否有转换的文档
489+
*
490+
* @param id
491+
* @return
492+
* @throws Exception
493+
*/
494+
public static boolean hasDoc(int id) throws Exception {
495+
496+
if (hasPDF(id)) {
497+
498+
File dir = new File(OUTPUT_PATH + id + File.separator);
499+
if (isSplitPage()) {
500+
if (dir.exists()) {
501+
if (dir.listFiles(new SplitSwfFileFilter()).length == getDocPageCount(id, false)) {
502+
return true;
503+
}
504+
}
505+
} else {
506+
File swf = new File(dir.getPath() + "page.swf");
507+
if (swf.exists() && swf.isFile() && swf.length() > 0) {
508+
return true;
509+
}
510+
}
435511
}
436512

437513
return false;

docviewerapi/src/com/log4ic/utils/convert/DocViewerConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static File toPDF(File file, String outPath) throws Exception {
8282
File pdf = null;
8383

8484
File dir = deploy(file, outPath);
85-
pdf = new File(dir.getPath() + File.separator + FileUtils.getFilePrefix(file) + ".pdf");
85+
pdf = new File(FileUtils.appendFileSeparator(dir.getPath()) + FileUtils.getFilePrefix(file) + ".pdf");
8686
if (!pdf.exists()) {
8787
pdf = officeConverter.toPDF(file, dir.getPath());
8888
}

docviewerapi/src/com/log4ic/utils/convert/office/OfficeConverter.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import java.io.File;
1515
import java.io.IOException;
16-
import java.io.InputStream;
1716
import java.net.ConnectException;
1817
import java.util.Properties;
1918

@@ -32,10 +31,6 @@ public class OfficeConverter {
3231

3332
private static OfficeManager officeManager;
3433

35-
/**
36-
* 配置文件
37-
*/
38-
private static String CONFIG_FILE = "config" + File.separator + "office-convert.properties";
3934
/**
4035
* open office 目录
4136
*/
@@ -138,27 +133,6 @@ public File convert(File inputFile, File outputFile) throws IOException {
138133
return null;
139134
}
140135

141-
public static void setConfig(String cfgName, String cfgValue) throws Exception {
142-
Properties properties = getProperties();
143-
properties.setProperty(cfgName, cfgValue);
144-
}
145-
146-
public static Properties getProperties() throws Exception {
147-
if (properties == null) {
148-
properties = new Properties();
149-
//获取class文件夹
150-
ClassLoader loader = OfficeConverter.class.getClassLoader();
151-
//加载文件
152-
InputStream is = loader.getResourceAsStream(CONFIG_FILE);
153-
if (is == null) {
154-
throw new Exception("properties is not found");
155-
}
156-
//读取
157-
properties.load(is);
158-
}
159-
return properties;
160-
}
161-
162136
public static void startService() throws Exception {
163137
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
164138
try {

docviewerapi/src/com/log4ic/utils/convert/pdf/PDFConverter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ private PDFConverterDeploy deploy(File pdfFile, String outPath, boolean splitPag
148148

149149
reader.close();
150150

151+
if (pageCount != 0) {
152+
OutputStream out = null;
153+
try {
154+
File info = new File(FileUtils.appendFileSeparator(outputDirectory.getPath()) + "info");
155+
156+
info.createNewFile();
157+
158+
out = new FileOutputStream(info);
159+
160+
out.write((pageCount + "").getBytes("UTF-8"));
161+
} finally {
162+
if (out != null) {
163+
out.flush();
164+
out.close();
165+
}
166+
}
167+
168+
}
169+
151170
return new PDFConverterDeploy(outputDirectory, pageCount, COMMAND.replace("${in}", pdfPath)
152171
.replace("${out}", outPath + (splitPage ? "page%.swf" : "page.swf")) +
153172
//是否将图片转换成点阵形式
@@ -303,6 +322,7 @@ public File convert(File pdfFile, String outPath, boolean poly2bitmap) throws Ex
303322

304323
/**
305324
* pdf转换为swf
325+
*
306326
* @param pdfFile
307327
* @param outPath
308328
* @param splitPage
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.log4ic.utils.filter;
2+
3+
import java.io.File;
4+
import java.io.FileFilter;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* @author: 张立鑫
10+
* @version: 1
11+
* @date: 11-8-25 下午12:05
12+
*/
13+
public class SplitSwfFileFilter implements FileFilter {
14+
public boolean accept(File pathname) {
15+
Pattern pattern = Pattern.compile("^page\\d+\\.swf$");
16+
Matcher matcher = pattern.matcher(pathname.getName().toString());
17+
return matcher.matches();
18+
}
19+
}

docviewerapi/src/com/log4ic/utils/filter/SwfFileFilter.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)