Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzdisk
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzdisk
提交
507fc9a3
提交
507fc9a3
编写于
8月 14, 2020
作者:
sq3536
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
网盘
上级
456fdd2c
变更
3
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
231 行增加
和
67 行删除
+231
-67
DiskCoreService.java
...ibizlab/core/disk/extensions/service/DiskCoreService.java
+137
-0
DiskCoreResource.java
...java/cn/ibizlab/api/rest/extensions/DiskCoreResource.java
+93
-0
SimpleFileService.java
.../main/java/cn/ibizlab/util/service/SimpleFileService.java
+1
-67
未找到文件。
ibzdisk-core/src/main/java/cn/ibizlab/core/disk/extensions/service/DiskCoreService.java
0 → 100644
浏览文件 @
507fc9a3
package
cn
.
ibizlab
.
core
.
disk
.
extensions
.
service
;
import
cn.ibizlab.core.disk.domain.SDFile
;
import
cn.ibizlab.core.disk.service.ISDFileService
;
import
cn.ibizlab.util.domain.FileItem
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.DigestUtils
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
@Primary
@Slf4j
@Service
public
class
DiskCoreService
{
@Value
(
"${ibiz.filePath:/app/file/}"
)
private
String
fileRoot
;
private
SimpleDateFormat
simpleDateFormat
=
new
SimpleDateFormat
(
"yyMMdd"
);
@Autowired
private
ISDFileService
sdFileService
;
public
FileItem
saveFile
(
MultipartFile
multipartFile
)
{
return
saveFile
(
""
,
multipartFile
);
}
public
FileItem
saveFile
(
String
folder
,
MultipartFile
multipartFile
)
{
return
saveFile
(
folder
,
""
,
multipartFile
);
}
public
FileItem
saveFile
(
String
folder
,
String
fileId
,
MultipartFile
multipartFile
)
{
SDFile
sdFile
=
new
SDFile
();
if
(!
StringUtils
.
isEmpty
(
fileId
))
sdFile
.
setId
(
fileId
);
if
(!
StringUtils
.
isEmpty
(
folder
))
sdFile
.
setId
(
folder
);
return
saveFile
(
sdFile
,
multipartFile
);
}
public
FileItem
saveFile
(
SDFile
sdFile
,
MultipartFile
multipartFile
)
{
FileItem
item
=
null
;
try
{
String
fileName
=
multipartFile
.
getOriginalFilename
();
sdFile
.
setName
(
fileName
);
String
extension
=
"."
+
getExtensionName
(
fileName
);
sdFile
.
setExtension
(
extension
);
String
digestCode
=
DigestUtils
.
md5DigestAsHex
(
multipartFile
.
getInputStream
());
sdFile
.
setDigestCode
(
digestCode
);
String
fileId
=
sdFile
.
getId
();
if
(
StringUtils
.
isEmpty
(
fileId
))
{
fileId
=
simpleDateFormat
.
format
(
new
Date
()).
concat
(
"-"
).
concat
(
digestCode
);
sdFile
.
setId
(
fileId
);
}
String
folder
=
sdFile
.
getFolder
();
if
(
StringUtils
.
isEmpty
(
folder
))
{
folder
=
"ibizutil"
;
sdFile
.
setFolder
(
folder
);
}
String
filePath
=
sdFile
.
getFilePath
();
if
(
StringUtils
.
isEmpty
(
filePath
))
{
filePath
=
folder
.
concat
(
File
.
separator
).
concat
(
fileId
).
concat
(
File
.
separator
).
concat
(
fileName
);
sdFile
.
setFilePath
(
filePath
);
}
int
size
=
(
int
)
multipartFile
.
getSize
();
sdFile
.
setFileSize
(
size
);
String
fileFullPath
=
this
.
fileRoot
.
concat
(
filePath
);
fileFullPath
=
fileFullPath
.
replace
(
"\\"
,
File
.
separator
).
replace
(
"/"
,
File
.
separator
);
File
file
=
new
File
(
fileFullPath
);
File
parent
=
new
File
(
file
.
getParent
());
if
(!
parent
.
exists
())
parent
.
mkdirs
();
FileCopyUtils
.
copy
(
multipartFile
.
getInputStream
(),
Files
.
newOutputStream
(
file
.
toPath
()));
item
=
new
FileItem
(
fileId
,
fileName
,
fileId
,
fileName
,
size
,
extension
);
sdFileService
.
save
(
sdFile
);
}
catch
(
IOException
e
)
{
throw
new
InternalServerErrorException
(
"文件上传失败"
);
}
return
item
;
}
public
File
getFile
(
String
fileId
)
{
return
getFile
(
""
,
fileId
);
}
public
File
getFile
(
String
folder
,
String
fileId
)
{
if
(
StringUtils
.
isEmpty
(
folder
))
folder
=
"ibizutil"
;
String
dirpath
=
this
.
fileRoot
.
concat
(
folder
).
concat
(
File
.
separator
).
concat
(
fileId
);
File
parent
=
new
File
(
dirpath
);
if
(
parent
.
exists
()
&&
parent
.
isDirectory
()
&&
parent
.
listFiles
().
length
>
0
)
{
return
parent
.
listFiles
()[
0
];
}
return
getFile
(
sdFileService
.
get
(
fileId
));
}
public
File
getFile
(
SDFile
sdFile
)
{
if
(!
StringUtils
.
isEmpty
(
sdFile
.
getFilePath
())){
String
fileFullPath
=
this
.
fileRoot
.
concat
(
sdFile
.
getFilePath
());
fileFullPath
=
fileFullPath
.
replace
(
"\\"
,
File
.
separator
).
replace
(
"/"
,
File
.
separator
);
File
file
=
new
File
(
fileFullPath
);
if
(
file
.
exists
())
return
file
;
}
throw
new
InternalServerErrorException
(
"文件未找到"
);
}
/**
* 获取文件扩展名
* @param fileName
* @return
*/
public
static
String
getExtensionName
(
String
fileName
)
{
if
((
fileName
!=
null
)
&&
(
fileName
.
length
()
>
0
))
{
int
dot
=
fileName
.
lastIndexOf
(
'.'
);
if
((
dot
>-
1
)
&&
(
dot
<
(
fileName
.
length
()
-
1
)))
{
return
fileName
.
substring
(
dot
+
1
);
}
}
return
fileName
;
}
}
\ No newline at end of file
ibzdisk-provider/ibzdisk-provider-api/src/main/java/cn/ibizlab/api/rest/extensions/DiskCoreResource.java
0 → 100644
浏览文件 @
507fc9a3
package
cn
.
ibizlab
.
api
.
rest
.
extensions
;
import
cn.ibizlab.core.disk.extensions.service.DiskCoreService
;
import
cn.ibizlab.util.domain.FileItem
;
import
cn.ibizlab.util.service.FileService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
@Slf4j
@RestController
@RequestMapping
(
"/"
)
public
class
DiskCoreResource
{
@Autowired
private
DiskCoreService
diskCoreService
;
@PostMapping
(
value
=
"{folder}/upload"
)
public
ResponseEntity
<
FileItem
>
upload
(
@PathVariable
(
"folder"
)
String
folder
,
@RequestParam
(
"file"
)
MultipartFile
multipartFile
){
return
ResponseEntity
.
ok
().
body
(
diskCoreService
.
saveFile
(
folder
,
multipartFile
));
}
@GetMapping
(
value
=
"{folder}/download/{id}"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
void
download
(
@PathVariable
(
"folder"
)
String
folder
,
@PathVariable
(
"id"
)
String
id
,
HttpServletResponse
response
){
File
file
=
diskCoreService
.
getFile
(
folder
,
id
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
getFileName
(
file
.
getName
()));
this
.
sendRespose
(
response
,
file
);
}
@GetMapping
(
value
=
"net-disk/{folder}/{id}/{name}.{ext}"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
void
open
(
@PathVariable
(
"folder"
)
String
folder
,
@PathVariable
(
"id"
)
String
id
,
@PathVariable
(
"name"
)
String
name
,
@PathVariable
(
"ext"
)
String
ext
,
HttpServletResponse
response
){
File
file
=
diskCoreService
.
getFile
(
folder
,
id
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
getFileName
(
file
.
getName
()));
this
.
sendRespose
(
response
,
file
);
}
protected
void
sendRespose
(
HttpServletResponse
response
,
File
file
){
BufferedInputStream
bis
=
null
;
BufferedOutputStream
bos
=
null
;
try
{
bis
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
bos
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
byte
[]
buff
=
new
byte
[
2048
];
int
bytesRead
;
while
(-
1
!=
(
bytesRead
=
bis
.
read
(
buff
,
0
,
buff
.
length
)))
{
bos
.
write
(
buff
,
0
,
bytesRead
);
}
}
catch
(
Exception
e
)
{
//throw e;
}
finally
{
if
(
bis
!=
null
)
{
try
{
bis
.
close
();
}
catch
(
IOException
e
)
{
}
}
if
(
bos
!=
null
)
{
try
{
bos
.
close
();
}
catch
(
IOException
e
)
{
}
}
}
}
protected
String
getFileName
(
String
fileName
){
try
{
return
new
String
(
fileName
.
getBytes
(
"utf-8"
),
"iso8859-1"
);
//防止中文乱码
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
return
fileName
;
}
}
\ No newline at end of file
ibzdisk-util/src/main/java/cn/ibizlab/util/service/SimpleFileService.java
浏览文件 @
507fc9a3
package
cn
.
ibizlab
.
util
.
service
;
package
cn
.
ibizlab
.
util
.
service
;
import
cn.ibizlab.util.domain.FileItem
;
public
class
SimpleFileService
{
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.util.DigestUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.IOException
;
import
java.nio.file.Files
;
@Primary
@Slf4j
@Service
public
class
SimpleFileService
implements
FileService
{
@Value
(
"${ibiz.filePath:/app/file/}"
)
private
String
fileRoot
;
@Override
public
FileItem
saveFile
(
MultipartFile
multipartFile
)
{
FileItem
item
=
null
;
// 获取文件名
String
fileName
=
multipartFile
.
getOriginalFilename
();
// 获取文件后缀
String
extname
=
"."
+
getExtensionName
(
fileName
);
try
{
String
fileid
=
DigestUtils
.
md5DigestAsHex
(
multipartFile
.
getInputStream
());
String
fileFullPath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileid
+
File
.
separator
+
fileName
;
File
file
=
new
File
(
fileFullPath
);
File
parent
=
new
File
(
file
.
getParent
());
if
(!
parent
.
exists
())
parent
.
mkdirs
();
FileCopyUtils
.
copy
(
multipartFile
.
getInputStream
(),
Files
.
newOutputStream
(
file
.
toPath
()));
item
=
new
FileItem
(
fileid
,
fileName
,
fileid
,
fileName
,(
int
)
multipartFile
.
getSize
(),
extname
);
}
catch
(
IOException
e
)
{
throw
new
InternalServerErrorException
(
"文件上传失败"
);
}
return
item
;
}
@Override
public
File
getFile
(
String
fileid
)
{
String
dirpath
=
this
.
fileRoot
+
"ibizutil"
+
File
.
separator
+
fileid
;
File
parent
=
new
File
(
dirpath
);
if
(
parent
.
exists
()
&&
parent
.
isDirectory
()
&&
parent
.
listFiles
().
length
>
0
)
{
return
parent
.
listFiles
()[
0
];
}
throw
new
InternalServerErrorException
(
"文件未找到"
);
}
/**
* 获取文件扩展名
* @param filename
* @return
*/
public
static
String
getExtensionName
(
String
filename
)
{
if
((
filename
!=
null
)
&&
(
filename
.
length
()
>
0
))
{
int
dot
=
filename
.
lastIndexOf
(
'.'
);
if
((
dot
>-
1
)
&&
(
dot
<
(
filename
.
length
()
-
1
)))
{
return
filename
.
substring
(
dot
+
1
);
}
}
return
filename
;
}
}
}
\ No newline at end of file
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录