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
梁杰芳
slm-fileview
Commits
fb7cdfbe
Commit
fb7cdfbe
authored
Jun 17, 2019
by
陈精华
Committed by
kl
Jun 19, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持http/https文件流作为预览源url
parent
cf1ee9c6
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
115 additions
and
45 deletions
+115
-45
.gitignore
.gitignore
+4
-2
FileConverQueueTask.java
.../src/main/java/cn/keking/service/FileConverQueueTask.java
+6
-6
FilePreview.java
...rter-web/src/main/java/cn/keking/service/FilePreview.java
+2
-1
FilePreviewFactory.java
...b/src/main/java/cn/keking/service/FilePreviewFactory.java
+1
-3
CompressFilePreviewImpl.java
.../java/cn/keking/service/impl/CompressFilePreviewImpl.java
+1
-2
MediaFilePreviewImpl.java
...ain/java/cn/keking/service/impl/MediaFilePreviewImpl.java
+1
-2
OfficeFilePreviewImpl.java
...in/java/cn/keking/service/impl/OfficeFilePreviewImpl.java
+1
-4
OtherFilePreviewImpl.java
...ain/java/cn/keking/service/impl/OtherFilePreviewImpl.java
+1
-3
PdfFilePreviewImpl.java
.../main/java/cn/keking/service/impl/PdfFilePreviewImpl.java
+1
-2
PictureFilePreviewImpl.java
...n/java/cn/keking/service/impl/PictureFilePreviewImpl.java
+2
-1
SimTextFilePreviewImpl.java
...n/java/cn/keking/service/impl/SimTextFilePreviewImpl.java
+1
-2
DownloadUtils.java
...rter-web/src/main/java/cn/keking/utils/DownloadUtils.java
+2
-2
FileUtils.java
...onverter-web/src/main/java/cn/keking/utils/FileUtils.java
+78
-11
OnlinePreviewController.java
...ava/cn/keking/web/controller/OnlinePreviewController.java
+14
-4
No files found.
.gitignore
View file @
fb7cdfbe
...
...
@@ -37,4 +37,6 @@ nbdist/
/producer/tmp
/.temfile
.temfile
convertedFile/
\ No newline at end of file
convertedFile/
jodconverter-web/src/main/file
jodconverter-web/src/main/cache
\ No newline at end of file
jodconverter-web/src/main/java/cn/keking/service/FileConverQueueTask.java
View file @
fb7cdfbe
...
...
@@ -35,7 +35,7 @@ public class FileConverQueueTask {
@PostConstruct
public
void
startTask
(){
ExecutorService
executorService
=
Executors
.
newFixedThreadPool
(
3
);
executorService
.
submit
(
new
ConverTask
(
previewFactory
,
cacheService
,
fileUtils
));
executorService
.
submit
(
new
ConverTask
(
previewFactory
,
cacheService
,
fileUtils
));
logger
.
info
(
"队列处理文件转换任务启动完成 "
);
}
...
...
@@ -47,7 +47,7 @@ public class FileConverQueueTask {
FileUtils
fileUtils
;
public
ConverTask
(
FilePreviewFactory
previewFactory
,
CacheService
cacheService
,
FileUtils
fileUtils
)
{
public
ConverTask
(
FilePreviewFactory
previewFactory
,
CacheService
cacheService
,
FileUtils
fileUtils
)
{
this
.
previewFactory
=
previewFactory
;
this
.
cacheService
=
cacheService
;
this
.
fileUtils
=
fileUtils
;
...
...
@@ -58,13 +58,13 @@ public class FileConverQueueTask {
while
(
true
)
{
try
{
String
url
=
cacheService
.
takeQueueTask
();
if
(
url
!=
null
){
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
if
(
url
!=
null
){
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
logger
.
info
(
"正在处理转换任务,文件名称【{}】"
,
fileAttribute
.
getName
());
FileType
fileType
=
fileAttribute
.
getType
();
if
(
fileType
.
equals
(
FileType
.
compress
)
||
fileType
.
equals
(
FileType
.
office
)){
FilePreview
filePreview
=
previewFactory
.
get
(
url
);
filePreview
.
filePreviewHandle
(
url
,
new
ExtendedModelMap
()
);
FilePreview
filePreview
=
previewFactory
.
get
(
fileAttribute
);
filePreview
.
filePreviewHandle
(
url
,
new
ExtendedModelMap
(),
fileAttribute
);
}
}
}
catch
(
Exception
e
)
{
...
...
jodconverter-web/src/main/java/cn/keking/service/FilePreview.java
View file @
fb7cdfbe
package
cn
.
keking
.
service
;
import
cn.keking.model.FileAttribute
;
import
org.springframework.ui.Model
;
/**
...
...
@@ -7,5 +8,5 @@ import org.springframework.ui.Model;
* Content :
*/
public
interface
FilePreview
{
String
filePreviewHandle
(
String
url
,
Model
model
);
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
);
}
jodconverter-web/src/main/java/cn/keking/service/FilePreviewFactory.java
View file @
fb7cdfbe
...
...
@@ -5,7 +5,6 @@ import cn.keking.utils.FileUtils;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.stereotype.Service
;
import
org.springframework.ui.Model
;
import
java.util.Map
;
...
...
@@ -22,9 +21,8 @@ public class FilePreviewFactory {
@Autowired
ApplicationContext
context
;
public
FilePreview
get
(
String
url
)
{
public
FilePreview
get
(
FileAttribute
fileAttribute
)
{
Map
<
String
,
FilePreview
>
filePreviewMap
=
context
.
getBeansOfType
(
FilePreview
.
class
);
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
return
filePreviewMap
.
get
(
fileAttribute
.
getType
().
getInstanceName
());
}
}
jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java
View file @
fb7cdfbe
...
...
@@ -28,8 +28,7 @@ public class CompressFilePreviewImpl implements FilePreview{
ZipReader
zipReader
;
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
)
{
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
)
{
String
fileName
=
fileAttribute
.
getName
();
String
decodedUrl
=
fileAttribute
.
getDecodedUrl
();
String
suffix
=
fileAttribute
.
getSuffix
();
...
...
jodconverter-web/src/main/java/cn/keking/service/impl/MediaFilePreviewImpl.java
View file @
fb7cdfbe
...
...
@@ -19,9 +19,8 @@ public class MediaFilePreviewImpl implements FilePreview {
FileUtils
fileUtils
;
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
)
{
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
)
{
model
.
addAttribute
(
"mediaUrl"
,
url
);
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
String
suffix
=
fileAttribute
.
getSuffix
();
if
(
"flv"
.
equalsIgnoreCase
(
suffix
))
{
return
"flv"
;
...
...
jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java
View file @
fb7cdfbe
...
...
@@ -9,9 +9,7 @@ import cn.keking.utils.FileUtils;
import
cn.keking.utils.OfficeToPdf
;
import
cn.keking.utils.PdfUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.ui.ExtendedModelMap
;
import
org.springframework.ui.Model
;
import
org.springframework.util.StringUtils
;
...
...
@@ -44,11 +42,10 @@ public class OfficeFilePreviewImpl implements FilePreview {
public
static
final
String
OFFICE_PREVIEW_TYPE_ALLIMAGES
=
"allImages"
;
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
)
{
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
)
{
// 预览Type,参数传了就取参数的,没传取系统默认
String
officePreviewType
=
model
.
asMap
().
get
(
"officePreviewType"
)
==
null
?
ConfigConstants
.
getOfficePreviewType
()
:
model
.
asMap
().
get
(
"officePreviewType"
).
toString
();
String
originUrl
=
(
String
)
model
.
asMap
().
get
(
"originUrl"
);
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
String
suffix
=
fileAttribute
.
getSuffix
();
String
fileName
=
fileAttribute
.
getName
();
String
decodedUrl
=
fileAttribute
.
getDecodedUrl
();
...
...
jodconverter-web/src/main/java/cn/keking/service/impl/OtherFilePreviewImpl.java
View file @
fb7cdfbe
...
...
@@ -17,9 +17,7 @@ public class OtherFilePreviewImpl implements FilePreview {
FileUtils
fileUtils
;
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
)
{
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
)
{
model
.
addAttribute
(
"fileType"
,
fileAttribute
.
getSuffix
());
model
.
addAttribute
(
"msg"
,
"系统还不支持该格式文件的在线预览,"
+
"如有需要请按下方显示的邮箱地址联系系统维护人员"
);
...
...
jodconverter-web/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java
View file @
fb7cdfbe
...
...
@@ -34,8 +34,7 @@ public class PdfFilePreviewImpl implements FilePreview{
String
fileDir
=
ConfigConstants
.
getFileDir
();
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
)
{
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
)
{
String
decodedUrl
=
fileAttribute
.
getDecodedUrl
();
String
suffix
=
fileAttribute
.
getSuffix
();
String
fileName
=
fileAttribute
.
getName
();
...
...
jodconverter-web/src/main/java/cn/keking/service/impl/PictureFilePreviewImpl.java
View file @
fb7cdfbe
package
cn
.
keking
.
service
.
impl
;
import
cn.keking.model.FileAttribute
;
import
cn.keking.service.FilePreview
;
import
cn.keking.utils.FileUtils
;
import
com.google.common.collect.Lists
;
...
...
@@ -21,7 +22,7 @@ public class PictureFilePreviewImpl implements FilePreview {
FileUtils
fileUtils
;
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
)
{
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
)
{
String
fileKey
=(
String
)
RequestContextHolder
.
currentRequestAttributes
().
getAttribute
(
"fileKey"
,
0
);
List
imgUrls
=
Lists
.
newArrayList
(
url
);
try
{
...
...
jodconverter-web/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java
View file @
fb7cdfbe
...
...
@@ -23,8 +23,7 @@ public class SimTextFilePreviewImpl implements FilePreview{
FileUtils
fileUtils
;
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
){
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
){
String
decodedUrl
=
fileAttribute
.
getDecodedUrl
();
String
fileName
=
fileAttribute
.
getName
();
ReturnResponse
<
String
>
response
=
simTextUtil
.
readSimText
(
decodedUrl
,
fileName
);
...
...
jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java
View file @
fb7cdfbe
...
...
@@ -25,7 +25,7 @@ public class DownloadUtils {
* @param type
* @return
*/
public
ReturnResponse
<
String
>
downLoad
(
String
urlAddress
,
String
type
,
String
fileName
){
public
ReturnResponse
<
String
>
downLoad
(
String
urlAddress
,
String
type
,
String
fileName
)
{
ReturnResponse
<
String
>
response
=
new
ReturnResponse
<>(
0
,
"下载成功!!!"
,
""
);
URL
url
=
null
;
try
{
...
...
@@ -40,7 +40,7 @@ public class DownloadUtils {
UUID
uuid
=
UUID
.
randomUUID
();
if
(
null
==
fileName
)
{
fileName
=
uuid
+
"."
+
type
;
}
else
{
// 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
}
else
{
// 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
fileName
=
fileName
.
replace
(
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
),
type
);
}
String
realPath
=
fileDir
+
fileName
;
...
...
jodconverter-web/src/main/java/cn/keking/utils/FileUtils.java
View file @
fb7cdfbe
...
...
@@ -9,11 +9,13 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
java.io.*
;
import
java.net.URLDecoder
;
import
java.nio.charset.Charset
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -63,13 +65,17 @@ public class FileUtils {
* @return
*/
public
FileType
typeFromUrl
(
String
url
)
{
String
[]
simText
=
ConfigConstants
.
getSimText
();
String
[]
media
=
ConfigConstants
.
getMedia
();
String
nonPramStr
=
url
.
substring
(
0
,
url
.
indexOf
(
"?"
)
!=
-
1
?
url
.
indexOf
(
"?"
)
:
url
.
length
());
String
fileName
=
nonPramStr
.
substring
(
nonPramStr
.
lastIndexOf
(
"/"
)
+
1
);
return
typeFromFileName
(
fileName
);
}
private
FileType
typeFromFileName
(
String
fileName
)
{
String
[]
simText
=
ConfigConstants
.
getSimText
();
String
[]
media
=
ConfigConstants
.
getMedia
();
String
fileType
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
);
if
(
listPictureTypes
().
contains
(
fileType
.
toLowerCase
()))
{
return
FileType
.
picture
;
return
FileType
.
picture
;
}
if
(
listArchiveTypes
().
contains
(
fileType
.
toLowerCase
()))
{
return
FileType
.
compress
;
...
...
@@ -265,22 +271,83 @@ public class FileUtils {
private
String
suffixFromUrl
(
String
url
)
{
String
nonPramStr
=
url
.
substring
(
0
,
url
.
indexOf
(
"?"
)
!=
-
1
?
url
.
indexOf
(
"?"
)
:
url
.
length
());
String
fileName
=
nonPramStr
.
substring
(
nonPramStr
.
lastIndexOf
(
"/"
)
+
1
);
return
suffixFromFileName
(
fileName
);
}
private
String
suffixFromFileName
(
String
fileName
)
{
String
fileType
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
);
return
fileType
;
}
/**
* 获取url中的参数
* @param url
* @param name
* @return
*/
private
String
getUrlParameterReg
(
String
url
,
String
name
)
{
Map
<
String
,
String
>
mapRequest
=
new
HashMap
();
String
strUrlParam
=
TruncateUrlPage
(
url
);
if
(
strUrlParam
==
null
)
{
return
""
;
}
//每个键值为一组
String
[]
arrSplit
=
strUrlParam
.
split
(
"[&]"
);
for
(
String
strSplit:
arrSplit
)
{
String
[]
arrSplitEqual
=
strSplit
.
split
(
"[=]"
);
//解析出键值
if
(
arrSplitEqual
.
length
>
1
)
{
//正确解析
mapRequest
.
put
(
arrSplitEqual
[
0
],
arrSplitEqual
[
1
]);
}
else
if
(!
arrSplitEqual
[
0
].
equals
(
""
))
{
//只有参数没有值,不加入
mapRequest
.
put
(
arrSplitEqual
[
0
],
""
);
}
}
return
mapRequest
.
get
(
name
);
}
/**
* 去掉url中的路径,留下请求参数部分
* @param strURL url地址
* @return url请求参数部分
*/
private
static
String
TruncateUrlPage
(
String
strURL
)
{
String
strAllParam
=
null
;
strURL
=
strURL
.
trim
();
String
[]
arrSplit
=
strURL
.
split
(
"[?]"
);
if
(
strURL
.
length
()
>
1
)
{
if
(
arrSplit
.
length
>
1
)
{
if
(
arrSplit
[
1
]
!=
null
)
{
strAllParam
=
arrSplit
[
1
];
}
}
}
return
strAllParam
;
}
public
FileAttribute
getFileAttribute
(
String
url
)
{
String
decodedUrl
=
null
;
String
decodedUrl
=
null
;
try
{
decodedUrl
=
URLDecoder
.
decode
(
url
,
"utf-8"
);
}
catch
(
UnsupportedEncodingException
e
){
log
.
debug
(
"url解码失败"
);
}
catch
(
UnsupportedEncodingException
e
){
log
.
error
(
"url解码失败"
);
}
String
fileName
;
FileType
type
;
String
suffix
;
String
fullFileName
=
getUrlParameterReg
(
decodedUrl
,
"fullfilename"
);
if
(!
StringUtils
.
isEmpty
(
fullFileName
))
{
fileName
=
fullFileName
;
type
=
typeFromFileName
(
fileName
);
suffix
=
suffixFromFileName
(
fileName
);
}
else
{
fileName
=
getFileNameFromURL
(
decodedUrl
);
type
=
typeFromUrl
(
url
);
suffix
=
suffixFromUrl
(
url
);
}
// 路径转码
FileType
type
=
typeFromUrl
(
url
);
String
suffix
=
suffixFromUrl
(
url
);
// 抽取文件并返回文件列表
String
fileName
=
getFileNameFromURL
(
decodedUrl
);
return
new
FileAttribute
(
type
,
suffix
,
fileName
,
url
,
decodedUrl
);
}
}
jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java
View file @
fb7cdfbe
package
cn
.
keking
.
web
.
controller
;
import
cn.keking.model.FileAttribute
;
import
cn.keking.service.FilePreview
;
import
cn.keking.service.FilePreviewFactory
;
import
cn.keking.service.cache.CacheService
;
import
cn.keking.utils.FileUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
...
...
@@ -26,12 +30,17 @@ import java.util.List;
@Controller
public
class
OnlinePreviewController
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
OnlinePreviewController
.
class
);
@Autowired
FilePreviewFactory
previewFactory
;
@Autowired
CacheService
cacheService
;
@Autowired
private
FileUtils
fileUtils
;
/**
* @param url
* @param model
...
...
@@ -39,11 +48,12 @@ public class OnlinePreviewController {
*/
@RequestMapping
(
value
=
"onlinePreview"
,
method
=
RequestMethod
.
GET
)
public
String
onlinePreview
(
String
url
,
Model
model
,
HttpServletRequest
req
)
{
FileAttribute
fileAttribute
=
fileUtils
.
getFileAttribute
(
url
);
req
.
setAttribute
(
"fileKey"
,
req
.
getParameter
(
"fileKey"
));
model
.
addAttribute
(
"officePreviewType"
,
req
.
getParameter
(
"officePreviewType"
));
model
.
addAttribute
(
"originUrl"
,
req
.
getRequestURL
().
toString
());
FilePreview
filePreview
=
previewFactory
.
get
(
url
);
return
filePreview
.
filePreviewHandle
(
url
,
model
);
model
.
addAttribute
(
"originUrl"
,
req
.
getRequestURL
().
toString
());
FilePreview
filePreview
=
previewFactory
.
get
(
fileAttribute
);
return
filePreview
.
filePreviewHandle
(
url
,
model
,
fileAttribute
);
}
/**
...
...
@@ -105,7 +115,7 @@ public class OnlinePreviewController {
resp
.
getOutputStream
().
write
(
bs
,
0
,
len
);
}
}
catch
(
IOException
|
URISyntaxException
e
)
{
e
.
printStackTrace
(
);
LOGGER
.
error
(
"下载pdf文件失败"
,
e
);
}
finally
{
if
(
inputStream
!=
null
)
{
IOUtils
.
closeQuietly
(
inputStream
);
...
...
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