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
9cc02676
Commit
9cc02676
authored
May 14, 2020
by
陈精华
Committed by
kl
May 18, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化:允许上传多个同一类型文件
parent
a21f35c2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
23 deletions
+24
-23
FileController.java
...rc/main/java/cn/keking/web/controller/FileController.java
+19
-21
index.ftl
jodconverter-web/src/main/resources/web/index.ftl
+5
-2
No files found.
jodconverter-web/src/main/java/cn/keking/web/controller/FileController.java
View file @
9cc02676
...
...
@@ -7,6 +7,8 @@ import com.google.common.collect.ImmutableMap;
import
com.google.common.collect.Lists
;
import
cn.keking.model.ReturnResponse
;
import
cn.keking.utils.FileUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
...
...
@@ -28,15 +30,17 @@ import java.util.UUID;
*/
@RestController
public
class
FileController
{
String
fileDir
=
ConfigConstants
.
getFileDir
();
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FileController
.
class
);
private
String
fileDir
=
ConfigConstants
.
getFileDir
();
@Autowired
FileUtils
fileUtils
;
String
demoDir
=
"demo"
;
String
demoPath
=
demoDir
+
File
.
separator
;
private
FileUtils
fileUtils
;
private
String
demoDir
=
"demo"
;
private
String
demoPath
=
demoDir
+
File
.
separator
;
@RequestMapping
(
value
=
"fileUpload"
,
method
=
RequestMethod
.
POST
)
public
String
fileUpload
(
@RequestParam
(
"file"
)
MultipartFile
file
,
HttpServletRequest
request
)
throws
JsonProcessingException
{
public
String
fileUpload
(
@RequestParam
(
"file"
)
MultipartFile
file
)
throws
JsonProcessingException
{
// 获取文件名
String
fileName
=
file
.
getOriginalFilename
();
//判断是否为IE浏览器的文件名,IE浏览器下文件名会带有盘符信息
...
...
@@ -49,15 +53,15 @@ public class FileController {
if
(
pos
!=
-
1
)
{
fileName
=
fileName
.
substring
(
pos
+
1
);
}
// 判断该文件类型是否有上传过,如果上传过则提示不允许再次上传
if
(
existsTypeFile
(
fileName
))
{
return
new
ObjectMapper
().
writeValueAsString
(
new
ReturnResponse
<
String
>(
1
,
"每一种类型只可以上传一个文件,请先删除原有文件再次上传"
,
null
));
// 判断是否存在同名文件
if
(
existsFile
(
fileName
))
{
return
new
ObjectMapper
().
writeValueAsString
(
new
ReturnResponse
<
String
>(
1
,
"存在同名文件,请先删除原有文件再次上传"
,
null
));
}
File
outFile
=
new
File
(
fileDir
+
demoPath
);
if
(!
outFile
.
exists
())
{
outFile
.
mkdirs
();
}
logger
.
info
(
"上传文件:{}"
,
outFile
.
getAbsolutePath
());
try
(
InputStream
in
=
file
.
getInputStream
();
OutputStream
ot
=
new
FileOutputStream
(
fileDir
+
demoPath
+
fileName
)){
byte
[]
buffer
=
new
byte
[
1024
];
...
...
@@ -67,7 +71,7 @@ public class FileController {
}
return
new
ObjectMapper
().
writeValueAsString
(
new
ReturnResponse
<
String
>(
0
,
"SUCCESS"
,
null
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
(
);
logger
.
error
(
"文件上传失败"
,
e
);
return
new
ObjectMapper
().
writeValueAsString
(
new
ReturnResponse
<
String
>(
1
,
"FAILURE"
,
null
));
}
}
...
...
@@ -78,6 +82,7 @@ public class FileController {
fileName
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"/"
)
+
1
);
}
File
file
=
new
File
(
fileDir
+
demoPath
+
fileName
);
logger
.
info
(
"删除文件:{}"
,
file
.
getAbsolutePath
());
if
(
file
.
exists
())
{
file
.
delete
();
}
...
...
@@ -106,18 +111,11 @@ public class FileController {
* @return
* @param fileName
*/
private
boolean
exists
Type
File
(
String
fileName
)
{
private
boolean
existsFile
(
String
fileName
)
{
boolean
result
=
false
;
String
suffix
=
fileUtils
.
getSuffixFromFileName
(
fileName
);
File
file
=
new
File
(
fileDir
+
demoPath
);
File
file
=
new
File
(
fileDir
+
demoPath
+
fileName
);
if
(
file
.
exists
())
{
for
(
File
file1
:
file
.
listFiles
()){
String
existsFileSuffix
=
fileUtils
.
getSuffixFromFileName
(
file1
.
getName
());
if
(
suffix
.
equals
(
existsFileSuffix
))
{
result
=
true
;
break
;
}
}
return
true
;
}
return
result
;
}
...
...
jodconverter-web/src/main/resources/web/index.ftl
View file @
9cc02676
...
...
@@ -212,12 +212,15 @@ window.open('http://127.0.0.1:8012/picturesPreview?urls='+encodeURIComponent(fil
// 上传完成,刷新table
if
(
1
==
data
.
code
)
{
alert
(
data
.
msg
);
}
else
{
}
else
{
$
(
'#table'
).
bootstrapTable
(
'refresh'
,
{});
}
$
(
".loading_container"
).
hide
();
},
error
:
function
(
error
)
{
alert
(
error
);
$
(
".loading_container"
).
hide
();},
error
:
function
()
{
alert
(
'上传失败,请联系管理员'
);
$
(
".loading_container"
).
hide
();
},
url
:
'fileUpload'
,
/*设置post提交到的页面*/
type
:
"post"
,
/*设置表单以post方法提交*/
dataType
:
"json"
/*设置返回值类型为文本*/
...
...
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