Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzdisk
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzdisk
提交
fc79f7c3
提交
fc79f7c3
编写于
7月 10, 2022
作者:
zhouweidong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
同步cloud批量下载文件逻辑
上级
c6bf0424
变更
3
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
202 行增加
和
4 行删除
+202
-4
DiskCoreService.java
...ibizlab/core/disk/extensions/service/DiskCoreService.java
+50
-4
DiskCoreResource.java
...java/cn/ibizlab/api/rest/extensions/DiskCoreResource.java
+9
-0
ZipUtils.java
...k-util/src/main/java/cn/ibizlab/util/helper/ZipUtils.java
+143
-0
未找到文件。
ibzdisk-core/src/main/java/cn/ibizlab/core/disk/extensions/service/DiskCoreService.java
浏览文件 @
fc79f7c3
...
@@ -5,8 +5,11 @@ import cn.ibizlab.core.disk.service.ISDFileService;
...
@@ -5,8 +5,11 @@ import cn.ibizlab.core.disk.service.ISDFileService;
import
cn.ibizlab.core.disk.extensions.vo.FileItem
;
import
cn.ibizlab.core.disk.extensions.vo.FileItem
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
cn.ibizlab.util.helper.ZipUtils
;
import
com.baomidou.mybatisplus.core.toolkit.IdWorker
;
import
com.baomidou.mybatisplus.core.toolkit.IdWorker
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
...
@@ -15,13 +18,11 @@ import org.springframework.context.annotation.Primary;
...
@@ -15,13 +18,11 @@ import org.springframework.context.annotation.Primary;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.DigestUtils
;
import
org.springframework.util.DigestUtils
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.*
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Files
;
import
java.security.MessageDigest
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.NoSuchAlgorithmException
;
...
@@ -31,6 +32,7 @@ import java.util.ArrayList;
...
@@ -31,6 +32,7 @@ import java.util.ArrayList;
import
java.util.Base64
;
import
java.util.Base64
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
import
java.util.zip.ZipOutputStream
;
@Primary
@Primary
@Slf4j
@Slf4j
...
@@ -158,6 +160,50 @@ public class DiskCoreService {
...
@@ -158,6 +160,50 @@ public class DiskCoreService {
throw
new
InternalServerErrorException
(
"文件未找到"
);
throw
new
InternalServerErrorException
(
"文件未找到"
);
}
}
/**
* 批量下载文件 [{ "id":"fileid1"},{"id":"fileid2"}]
* @param strCat
* @param list
* @return
*/
public
File
getFile
(
String
strCat
,
List
<
JsonNode
>
list
)
{
if
(
ObjectUtils
.
isEmpty
(
list
))
{
throw
new
InternalServerErrorException
(
"未传入文件清单"
);
}
List
<
File
>
fileList
=
new
ArrayList
<
File
>();
for
(
JsonNode
item
:
list
)
{
if
(
item
instanceof
ObjectNode
)
{
ObjectNode
map
=
(
ObjectNode
)
item
;
item
=
map
.
get
(
"id"
);
}
fileList
.
add
(
this
.
getFile
(
strCat
,
item
.
asText
()));
}
try
{
File
tempFile
=
File
.
createTempFile
(
"oss"
,
".zip"
);
try
(
ZipOutputStream
zipOutputStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
tempFile
)))
{
for
(
File
file
:
fileList
)
{
if
(
file
.
getParentFile
()
==
null
||
file
.
getParentFile
().
getParentFile
()
==
null
)
{
throw
new
Exception
(
"文件路径不正确"
);
}
int
nFolderLength
=
file
.
getParentFile
().
getParentFile
().
getAbsolutePath
().
length
()
+
1
;
long
nTime
=
System
.
currentTimeMillis
();
ZipUtils
.
zip
(
file
,
zipOutputStream
,
nFolderLength
);
log
.
debug
(
String
.
format
(
"压缩文件[%1$s]耗时[%2$s]ms"
,
file
.
getAbsolutePath
(),
System
.
currentTimeMillis
()
-
nTime
));
}
zipOutputStream
.
flush
();
zipOutputStream
.
close
();
}
return
tempFile
;
}
catch
(
Throwable
ex
)
{
throw
new
InternalServerErrorException
(
String
.
format
(
"生成压缩文件发生异常,%1$s"
,
ex
.
getMessage
()));
}
}
public
File
getFileById
(
String
fileId
)
{
public
File
getFileById
(
String
fileId
)
{
SDFile
sdFile
=
sdFileService
.
getById
(
fileId
);
SDFile
sdFile
=
sdFileService
.
getById
(
fileId
);
if
(
sdFile
!=
null
&&(!
StringUtils
.
isEmpty
(
sdFile
.
getFilePath
()))){
if
(
sdFile
!=
null
&&(!
StringUtils
.
isEmpty
(
sdFile
.
getFilePath
()))){
...
...
ibzdisk-provider/ibzdisk-provider-api/src/main/java/cn/ibizlab/api/rest/extensions/DiskCoreResource.java
浏览文件 @
fc79f7c3
...
@@ -8,6 +8,7 @@ import cn.ibizlab.core.disk.extensions.vo.FileItem;
...
@@ -8,6 +8,7 @@ import cn.ibizlab.core.disk.extensions.vo.FileItem;
import
cn.ibizlab.core.disk.service.ISDFileService
;
import
cn.ibizlab.core.disk.service.ISDFileService
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -101,6 +102,14 @@ public class DiskCoreResource
...
@@ -101,6 +102,14 @@ public class DiskCoreResource
this
.
sendRespose
(
response
,
file
);
this
.
sendRespose
(
response
,
file
);
}
}
@PostMapping
(
value
=
"net-disk/download/"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
void
download
(
@RequestBody
List
<
JsonNode
>
list
,
HttpServletRequest
request
,
HttpServletResponse
response
){
File
file
=
diskCoreService
.
getFile
(
null
,
list
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
getFileName
(
request
.
getHeader
(
"User-Agent"
),
file
.
getName
()));
this
.
sendRespose
(
response
,
file
);
}
@GetMapping
(
value
=
{
"net-disk/openview/{folder}/{id}/{name}.{ext}"
,
"net-disk/files/{folder}/{id}/{name}.{ext}"
})
@GetMapping
(
value
=
{
"net-disk/openview/{folder}/{id}/{name}.{ext}"
,
"net-disk/files/{folder}/{id}/{name}.{ext}"
})
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
void
open
(
@PathVariable
(
"folder"
)
String
folder
,
@PathVariable
(
"id"
)
String
id
,
public
void
open
(
@PathVariable
(
"folder"
)
String
folder
,
@PathVariable
(
"id"
)
String
id
,
...
...
ibzdisk-util/src/main/java/cn/ibizlab/util/helper/ZipUtils.java
0 → 100644
浏览文件 @
fc79f7c3
package
cn
.
ibizlab
.
util
.
helper
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.FileChannel
;
import
java.util.List
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipInputStream
;
import
java.util.zip.ZipOutputStream
;
public
class
ZipUtils
{
private
static
final
org
.
apache
.
commons
.
logging
.
Log
log
=
org
.
apache
.
commons
.
logging
.
LogFactory
.
getLog
(
ZipUtils
.
class
);
public
static
void
zip
(
File
file
,
File
zipFile
)
throws
IOException
{
long
nBeginTime
=
System
.
currentTimeMillis
();
try
(
ZipOutputStream
zipOutputStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
zipFile
)))
{
long
nTime
=
System
.
currentTimeMillis
();
zip
(
file
,
zipOutputStream
);
log
.
debug
(
String
.
format
(
"压缩文件[%1$s]耗时[%2$s]ms"
,
file
.
getAbsolutePath
(),
System
.
currentTimeMillis
()
-
nTime
));
zipOutputStream
.
flush
();
zipOutputStream
.
close
();
}
log
.
debug
(
String
.
format
(
"生成压缩文件[%1$s]耗时[%2$s]ms"
,
zipFile
.
getAbsolutePath
(),
System
.
currentTimeMillis
()
-
nBeginTime
));
}
public
static
void
zip
(
List
<
File
>
fileList
,
File
zipFile
)
throws
IOException
{
long
nBeginTime
=
System
.
currentTimeMillis
();
try
(
ZipOutputStream
zipOutputStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
zipFile
)))
{
for
(
File
file
:
fileList
)
{
long
nTime
=
System
.
currentTimeMillis
();
zip
(
file
,
zipOutputStream
);
log
.
debug
(
String
.
format
(
"压缩文件[%1$s]耗时[%2$s]ms"
,
file
.
getAbsolutePath
(),
System
.
currentTimeMillis
()
-
nTime
));
}
zipOutputStream
.
flush
();
zipOutputStream
.
close
();
}
log
.
debug
(
String
.
format
(
"生成压缩文件[%1$s]耗时[%2$s]ms"
,
zipFile
.
getAbsolutePath
(),
System
.
currentTimeMillis
()
-
nBeginTime
));
}
public
static
void
zip
(
File
file
,
ZipOutputStream
zipOutputStream
)
throws
IOException
{
int
nFolderLength
=
0
;
if
(
file
.
isFile
())
{
nFolderLength
=
file
.
getParentFile
().
getAbsolutePath
().
length
();
}
else
{
nFolderLength
=
file
.
getAbsolutePath
().
length
();
}
zip
(
file
,
zipOutputStream
,
nFolderLength
+
1
);
}
public
static
void
zip
(
File
file
,
ZipOutputStream
zipOutputStream
,
int
nFolderLength
)
throws
IOException
{
if
(
file
.
isFile
())
{
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocate
(
1024
);
ZipEntry
zipEntry
=
new
ZipEntry
(
file
.
getAbsolutePath
().
substring
(
nFolderLength
).
replace
(
"\\"
,
"/"
));
zipEntry
.
setTime
(
file
.
lastModified
());
zipOutputStream
.
putNextEntry
(
zipEntry
);
try
(
FileInputStream
fis
=
new
FileInputStream
(
file
);
FileChannel
channel
=
fis
.
getChannel
();)
{
while
(
true
)
{
byteBuffer
.
clear
();
int
read
=
channel
.
read
(
byteBuffer
);
if
(
read
==
-
1
)
break
;
zipOutputStream
.
write
(
byteBuffer
.
array
(),
0
,
read
);
}
}
zipOutputStream
.
closeEntry
();
}
else
{
File
[]
files
=
file
.
listFiles
();
if
(
files
==
null
||
files
.
length
==
0
)
{
ZipEntry
zipEntry
=
new
ZipEntry
(
file
.
getAbsolutePath
().
substring
(
nFolderLength
).
replace
(
"\\"
,
"/"
)
+
"/"
);
zipEntry
.
setTime
(
file
.
lastModified
());
zipOutputStream
.
putNextEntry
(
zipEntry
);
zipOutputStream
.
closeEntry
();
}
else
{
for
(
File
item
:
files
)
{
zip
(
item
,
zipOutputStream
,
nFolderLength
);
}
}
}
}
public
static
void
unzip
(
File
zipFile
,
File
dstFolder
)
throws
IOException
{
if
(!
dstFolder
.
exists
())
{
dstFolder
.
mkdirs
();
}
String
destDir
=
dstFolder
.
getAbsolutePath
();
// buffer to read and write data in the file
byte
[]
buffer
=
new
byte
[
1024
];
try
(
FileInputStream
fis
=
new
FileInputStream
(
zipFile
);
ZipInputStream
zis
=
new
ZipInputStream
(
fis
);
)
{
ZipEntry
ZE
=
zis
.
getNextEntry
();
while
(
ZE
!=
null
)
{
String
fileName
=
ZE
.
getName
();
File
newFile
=
new
File
(
destDir
+
File
.
separator
+
fileName
);
new
File
(
newFile
.
getParent
()).
mkdirs
();
FileOutputStream
fos
=
new
FileOutputStream
(
newFile
);
int
len
;
while
((
len
=
zis
.
read
(
buffer
))
>
0
)
{
fos
.
write
(
buffer
,
0
,
len
);
}
fos
.
close
();
// close this ZipEntry
zis
.
closeEntry
();
ZE
=
zis
.
getNextEntry
();
}
}
}
public
static
void
main
(
String
[]
args
)
{
try
{
long
nTime
=
System
.
currentTimeMillis
();
ZipOutputStream
zipOutputStream
;
zipOutputStream
=
new
ZipOutputStream
(
new
FileOutputStream
(
new
File
(
"d:/aa.zip"
)));
zip
(
new
File
(
"I:\\code\\DEMO\\CBD466CC-A18A-47EC-A018-A6E7E5AB191F\\trunk\\TEST11\\model"
),
zipOutputStream
);
zipOutputStream
.
flush
();
zipOutputStream
.
close
();
nTime
=
System
.
currentTimeMillis
()
-
nTime
;
System
.
out
.
print
(
String
.
format
(
"压缩文件耗时[%1$s]ms"
,
nTime
));
nTime
=
System
.
currentTimeMillis
();
unzip
(
new
File
(
"d:/aa.zip"
),
new
File
(
"d:/aaaa"
));
nTime
=
System
.
currentTimeMillis
()
-
nTime
;
System
.
out
.
print
(
String
.
format
(
"解压文件耗时[%1$s]ms"
,
nTime
));
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录