Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
S
syyl
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
注册表
注册表
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
svn
syyl
Commits
346294ad
提交
346294ad
authored
4月 07, 2020
作者:
renjiancai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
--no commit message
上级
029bedd1
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
226 行增加
和
8 行删除
+226
-8
FileController.java
...rc/main/java/com/zrqx/file/controller/FileController.java
+123
-8
ZipUtil.java
com.zrqx.file/src/main/java/com/zrqx/file/util/ZipUtil.java
+103
-0
没有找到文件。
com.zrqx.file/src/main/java/com/zrqx/file/controller/FileController.java
浏览文件 @
346294ad
package
com
.
zrqx
.
file
.
controller
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.File
;
import
java.io.FileFilter
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.URLEncoder
;
import
java.nio.channels.FileChannel
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
...
...
@@ -19,6 +22,7 @@ import java.util.List;
import
java.util.Map
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipInputStream
;
import
java.util.zip.ZipOutputStream
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletRequest
;
...
...
@@ -95,7 +99,7 @@ public class FileController {
// 处理文件上传
@ApiOperation
(
value
=
"文件上传 视频"
)
@RequestMapping
(
value
=
"/upload/video"
,
method
=
RequestMethod
.
POST
)
public
FileInfo
uploadVideo
(
@RequestParam
(
"file"
)
MultipartFile
file
,
HttpServletRequest
request
)
throws
Exception
{
public
FileInfo
uploadVideo
(
@RequestParam
(
"file"
)
MultipartFile
file
,
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
Exception
{
FileInfo
entity
=
service
.
uploadFile
(
file
);
File
newfile
=
new
File
(
rootPath
+
entity
.
getPath
()
+
"/"
+
entity
.
getFileName
()
+
entity
.
getSuffixName
());
Encoder
encoder
=
new
Encoder
();
...
...
@@ -115,30 +119,56 @@ public class FileController {
entity
.
setVideoPath
(
webPath
);
return
entity
;
}
@ApiOperation
(
value
=
"文件下载"
)
@ApiOperation
(
value
=
"文件
批量
下载"
)
@RequestMapping
(
value
=
"/batch/download"
,
method
=
RequestMethod
.
POST
)
public
String
downLoad
(
HttpServletRequest
request
,
HttpServletResponse
response
,
@RequestBody
List
<
String
>
fileNames
)
{
public
HttpServletResponse
downLoad1
(
HttpServletRequest
request
,
HttpServletResponse
response
,
@RequestBody
List
<
String
>
fileNames
)
throws
Exception
{
if
(
fileNames
.
size
()>
0
)
{
List
<
File
>
files
=
new
ArrayList
<
File
>();
String
relaPath
=
"download.zip"
;
String
zipFileName
=
"/opt/upload/"
+
relaPath
;
ZipOutputStream
out
=
new
ZipOutputStream
(
new
BufferedOutputStream
(
new
FileOutputStream
(
zipFileName
)));
for
(
String
fileName
:
fileNames
)
{
FileInfo
entity
=
new
FileInfo
();
File
file
=
new
File
(
rootPath
+
fileName
);
logger
.
info
(
rootPath
+
fileName
);
FileInfo
entity
=
new
FileInfo
();
if
(!
file
.
exists
())
{
entity
.
setFileName
(
fileName
);
entity
=
service
.
selectOne
(
entity
);
fileName
=
rootPath
+
entity
.
getPath
()
+
"/"
+
entity
.
getFileName
()
+
entity
.
getSuffixName
();
fileName
=
"D:/opt/upload/syyl/"
+
entity
.
getPath
()
+
"/"
+
entity
.
getFileName
()
+
entity
.
getSuffixName
();
file
=
new
File
(
fileName
);
if
(
file
.
exists
())
{
files
.
add
(
file
);
}
}
if
(!
file
.
exists
())
{
throw
new
BaseException
(
"没有找到该文件!"
);
}
}
try
{
DownloadUtil
.
start
(
response
,
file
,
entity
.
getOriginalFileName
());
/**创建一个临时压缩文件,
* 我们会把文件流全部注入到这个文件中
* 这里的文件你可以自定义是.rar还是.zip*/
File
file
=
new
File
(
relaPath
);
if
(!
file
.
exists
()){
file
.
createNewFile
();
}
response
.
reset
();
//response.getWriter()
//创建文件输出流
FileOutputStream
fous
=
new
FileOutputStream
(
file
);
/**打包的方法我们会用到ZipOutputStream这样一个输出流,
* 所以这里我们把输出流转换一下*/
ZipOutputStream
zipOut
=
new
ZipOutputStream
(
fous
);
/**这个方法接受的就是一个所要打包文件的集合,
* 还有一个ZipOutputStream*/
zipFile
(
files
,
zipOut
);
zipOut
.
close
();
fous
.
close
();
return
downloadZip
(
file
,
response
);
}
catch
(
Exception
e
)
{
throw
new
BaseException
(
"文件下载失败!"
);
}
}
}
return
null
;
}
...
...
@@ -627,4 +657,89 @@ public class FileController {
}
return
service
.
queryAll
(
pageParam
);
}
/**
* 把接受的全部文件打成压缩包
* @param List<File>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public
static
void
zipFile
(
List
files
,
ZipOutputStream
outputStream
)
{
int
size
=
files
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
File
file
=
(
File
)
files
.
get
(
i
);
zipFile
(
file
,
outputStream
);
}
}
/**
* 根据输入的文件与输出流对文件进行打包
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public
static
void
zipFile
(
File
inputFile
,
ZipOutputStream
ouputStream
)
{
try
{
if
(
inputFile
.
exists
())
{
/**如果是目录的话这里是不采取操作的,
* 至于目录的打包正在研究中*/
if
(
inputFile
.
isFile
())
{
FileInputStream
IN
=
new
FileInputStream
(
inputFile
);
BufferedInputStream
bins
=
new
BufferedInputStream
(
IN
,
512
);
//org.apache.tools.zip.ZipEntry
ZipEntry
entry
=
new
ZipEntry
(
inputFile
.
getName
());
ouputStream
.
putNextEntry
(
entry
);
// 向压缩文件中输出数据
int
nNumber
;
byte
[]
buffer
=
new
byte
[
512
];
while
((
nNumber
=
bins
.
read
(
buffer
))
!=
-
1
)
{
ouputStream
.
write
(
buffer
,
0
,
nNumber
);
}
// 关闭创建的流对象
bins
.
close
();
IN
.
close
();
}
else
{
try
{
File
[]
files
=
inputFile
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
zipFile
(
files
[
i
],
ouputStream
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
static
HttpServletResponse
downloadZip
(
File
file
,
HttpServletResponse
response
)
{
try
{
// 以流的形式下载文件。
InputStream
fis
=
new
BufferedInputStream
(
new
FileInputStream
(
file
.
getPath
()));
byte
[]
buffer
=
new
byte
[
fis
.
available
()];
fis
.
read
(
buffer
);
fis
.
close
();
// 清空response
response
.
reset
();
OutputStream
toClient
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
response
.
setContentType
(
"application/octet-stream"
);
// 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
URLEncoder
.
encode
(
file
.
getName
(),
"UTF-8"
));
toClient
.
write
(
buffer
);
toClient
.
flush
();
toClient
.
close
();
}
catch
(
IOException
ex
)
{
ex
.
printStackTrace
();
}
finally
{
try
{
File
f
=
new
File
(
file
.
getPath
());
f
.
delete
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
response
;
}
}
com.zrqx.file/src/main/java/com/zrqx/file/util/ZipUtil.java
0 → 100644
浏览文件 @
346294ad
package
com
.
zrqx
.
file
.
util
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.URLEncoder
;
import
java.util.List
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
import
javax.servlet.http.HttpServletResponse
;
public
class
ZipUtil
{
/**
* 把接受的全部文件打成压缩包
* @param List<File>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public
static
void
zipFile
(
List
files
,
ZipOutputStream
outputStream
)
{
int
size
=
files
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
File
file
=
(
File
)
files
.
get
(
i
);
zipFile
(
file
,
outputStream
);
}
}
/**
* 根据输入的文件与输出流对文件进行打包
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public
static
void
zipFile
(
File
inputFile
,
ZipOutputStream
ouputStream
)
{
try
{
if
(
inputFile
.
exists
())
{
/**如果是目录的话这里是不采取操作的,
* 至于目录的打包正在研究中*/
if
(
inputFile
.
isFile
())
{
FileInputStream
IN
=
new
FileInputStream
(
inputFile
);
BufferedInputStream
bins
=
new
BufferedInputStream
(
IN
,
512
);
//org.apache.tools.zip.ZipEntry
ZipEntry
entry
=
new
ZipEntry
(
inputFile
.
getName
());
ouputStream
.
putNextEntry
(
entry
);
// 向压缩文件中输出数据
int
nNumber
;
byte
[]
buffer
=
new
byte
[
512
];
while
((
nNumber
=
bins
.
read
(
buffer
))
!=
-
1
)
{
ouputStream
.
write
(
buffer
,
0
,
nNumber
);
}
// 关闭创建的流对象
bins
.
close
();
IN
.
close
();
}
else
{
try
{
File
[]
files
=
inputFile
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
zipFile
(
files
[
i
],
ouputStream
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
static
HttpServletResponse
downloadZip
(
File
file
,
HttpServletResponse
response
)
{
try
{
// 以流的形式下载文件。
InputStream
fis
=
new
BufferedInputStream
(
new
FileInputStream
(
file
.
getPath
()));
byte
[]
buffer
=
new
byte
[
fis
.
available
()];
fis
.
read
(
buffer
);
fis
.
close
();
// 清空response
response
.
reset
();
OutputStream
toClient
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
response
.
setContentType
(
"application/octet-stream"
);
// 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
URLEncoder
.
encode
(
file
.
getName
(),
"UTF-8"
));
toClient
.
write
(
buffer
);
toClient
.
flush
();
toClient
.
close
();
}
catch
(
IOException
ex
)
{
ex
.
printStackTrace
();
}
finally
{
try
{
File
f
=
new
File
(
file
.
getPath
());
f
.
delete
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
response
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论