Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
slm-fileview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jenkins
slm-fileview
Commits
5888c56f
Commit
5888c56f
authored
Nov 21, 2019
by
陈精华
Committed by
kl
Nov 21, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化:异常处理、异常提示
parent
0798b8d8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
12 deletions
+72
-12
ConverterUtils.java
...ter-web/src/main/java/cn/keking/utils/ConverterUtils.java
+71
-11
fileNotSupported.ftl
jodconverter-web/src/main/resources/web/fileNotSupported.ftl
+1
-1
No files found.
jodconverter-web/src/main/java/cn/keking/utils/ConverterUtils.java
View file @
5888c56f
...
...
@@ -6,14 +6,20 @@ import org.artofsolving.jodconverter.OfficeDocumentConverter;
import
org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration
;
import
org.artofsolving.jodconverter.office.OfficeManager
;
import
org.artofsolving.jodconverter.office.OfficeUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.PreDestroy
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.charset.Charset
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Properties
;
/**
* 创建文件转换器
...
...
@@ -24,6 +30,8 @@ import java.util.Map;
@Component
public
class
ConverterUtils
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ConverterUtils
.
class
);
// @Value("${office.home}")
// String officeHome;
// OpenOfficeConnection connection;
...
...
@@ -31,18 +39,28 @@ public class ConverterUtils {
@PostConstruct
public
void
initOfficeManager
()
{
//// connection = new SocketOpenOfficeConnection(host,8100);
//// connection.connect();
String
officeHome
=
OfficeUtils
.
getDefaultOfficeHome
().
getAbsolutePath
();
DefaultOfficeManagerConfiguration
configuration
=
new
DefaultOfficeManagerConfiguration
();
configuration
.
setOfficeHome
(
officeHome
);
configuration
.
setPortNumber
(
8100
);
officeManager
=
configuration
.
buildOfficeManager
();
officeManager
.
start
();
// 设置任务执行超时为5分钟
// configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//
// 设置任务队列超时为24小时
// configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//
if
(
officeHome
==
null
)
{
throw
new
RuntimeException
(
"找不到office组件,请确认'office.home'配置是否有误"
);
}
boolean
killOffice
=
killProcess
(
"soffice.bin"
);
if
(
killOffice
)
{
logger
.
warn
(
"检测到有正在运行的office进程,已自动结束该进程"
);
}
try
{
DefaultOfficeManagerConfiguration
configuration
=
new
DefaultOfficeManagerConfiguration
();
configuration
.
setOfficeHome
(
officeHome
);
configuration
.
setPortNumber
(
8100
);
// 设置任务执行超时为5分钟
configuration
.
setTaskExecutionTimeout
(
1000
*
60
*
5L
);
// 设置任务队列超时为24小时
//configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
officeManager
=
configuration
.
buildOfficeManager
();
officeManager
.
start
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"启动office组件失败,请检查office组件是否可用"
);
throw
e
;
}
}
public
OfficeDocumentConverter
getDocumentConverter
()
{
...
...
@@ -60,6 +78,48 @@ public class ConverterUtils {
return
loadProperties
;
}
private
boolean
killProcess
(
String
processName
)
{
boolean
flag
=
false
;
Properties
props
=
System
.
getProperties
();
try
{
if
(
props
.
getProperty
(
"os.name"
).
toLowerCase
().
contains
(
"windows"
))
{
Process
p
=
Runtime
.
getRuntime
().
exec
(
"cmd /c tasklist "
);
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
InputStream
os
=
p
.
getInputStream
();
byte
b
[]
=
new
byte
[
256
];
while
(
os
.
read
(
b
)
>
0
)
{
baos
.
write
(
b
);
}
String
s
=
baos
.
toString
();
if
(
s
.
indexOf
(
processName
)
>=
0
)
{
Runtime
.
getRuntime
().
exec
(
"taskkill /im "
+
processName
+
" /f"
);
flag
=
true
;
}
else
{
flag
=
false
;
}
}
else
{
Process
p
=
Runtime
.
getRuntime
().
exec
(
new
String
[]{
"sh"
,
"-c"
,
"ps -ef | grep "
+
processName
});
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
InputStream
os
=
p
.
getInputStream
();
byte
b
[]
=
new
byte
[
256
];
while
(
os
.
read
(
b
)
>
0
)
{
baos
.
write
(
b
);
}
String
s
=
baos
.
toString
();
if
(
s
.
indexOf
(
processName
)
>=
0
)
{
String
[]
cmd
={
"sh"
,
"-c"
,
"killall -9 "
+
processName
};
Runtime
.
getRuntime
().
exec
(
cmd
);
flag
=
true
;
}
else
{
flag
=
false
;
}
}
}
catch
(
IOException
e
)
{
logger
.
error
(
"检测office进程异常"
,
e
);
}
return
flag
;
}
@PreDestroy
public
void
destroyOfficeManager
(){
if
(
null
!=
officeManager
&&
officeManager
.
isRunning
())
{
...
...
jodconverter-web/src/main/resources/web/fileNotSupported.ftl
View file @
5888c56f
...
...
@@ -32,7 +32,7 @@
<img
src=
"images/sorry.jpg"
/>
<span>
该文件类型(${fileType})系统暂时不支持在线预览,
<b>
说明
</b>
:
<p>
${msg}
</p>
<p
style=
"color: red;"
>
${msg}
</p>
如有需要请联系
<a
href=
"mailto:1210893441@qq.com"
>
@HappyPig
</a>
</span>
</div>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment