Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
E
EncryptedFileSystem
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
zhangshuang
EncryptedFileSystem
Commits
a3f41036
提交
a3f41036
authored
3月 23, 2020
作者:
zhangshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ftp文件服务器
上级
daacd14c
隐藏空白字符变更
内嵌
并排
正在显示
14 个修改的文件
包含
735 行增加
和
0 行删除
+735
-0
pom.xml
efs-ftp/pom.xml
+12
-0
FtpApplication.java
efs-ftp/src/main/java/com/zjty/efs/ftp/FtpApplication.java
+11
-0
ResponseCode.java
...ain/java/com/zjty/efs/ftp/base/response/ResponseCode.java
+110
-0
ServerResponse.java
...n/java/com/zjty/efs/ftp/base/response/ServerResponse.java
+168
-0
FileDownLoadController.java
...a/com/zjty/efs/ftp/controller/FileDownLoadController.java
+23
-0
FileUploadController.java
...ava/com/zjty/efs/ftp/controller/FileUploadController.java
+25
-0
FileReturn.java
...ftp/src/main/java/com/zjty/efs/ftp/entity/FileReturn.java
+13
-0
FileDownLoadService.java
...in/java/com/zjty/efs/ftp/service/FileDownLoadService.java
+16
-0
FileUploadService.java
...main/java/com/zjty/efs/ftp/service/FileUploadService.java
+17
-0
FileDownLoadServiceImpl.java
...om/zjty/efs/ftp/service/impl/FileDownLoadServiceImpl.java
+101
-0
FileUploadServiceImpl.java
.../com/zjty/efs/ftp/service/impl/FileUploadServiceImpl.java
+84
-0
TimeUtil.java
efs-ftp/src/main/java/com/zjty/efs/ftp/utils/TimeUtil.java
+10
-0
ZipUtils.java
efs-ftp/src/main/java/com/zjty/efs/ftp/utils/ZipUtils.java
+137
-0
application.properties
efs-ftp/src/main/resources/application.properties
+8
-0
没有找到文件。
efs-ftp/pom.xml
浏览文件 @
a3f41036
...
...
@@ -12,4 +12,15 @@
<artifactId>
efs-ftp
</artifactId>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.46
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
efs-ftp/src/main/java/com/zjty/efs/ftp/FtpApplication.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
FtpApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
FtpApplication
.
class
,
args
);
}
}
efs-ftp/src/main/java/com/zjty/efs/ftp/base/response/ResponseCode.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
base
.
response
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
/**
* <p>Description : 异常结果枚举类,用于统一异常状态码与相关描述信息
* @Date : 2017/12/13 0:51
* @author : M@tr!x [xhyrzldf@foxmail.com]
*/
@AllArgsConstructor
@Getter
public
enum
ResponseCode
{
/**
* 服务器成功返回用户请求的数据
*/
OK
(
200
,
"[GET]:服务器成功返回用户请求的数据,返回资源对象"
),
/**
* 用户新建或修改数据成功
*/
CREATED
(
201
,
"[POST/PUT/PATCH]:用户新建或修改数据成功,返回新生成或修改的资源对象"
),
/**
* 表示一个请求已经进入后台排队(异步任务)
*/
ACCEPTED
(
202
,
"[*]:表示一个请求已经进入后台排队(异步任务)"
),
/**
* 用户上传文件成功
*/
UPLOADED
(
203
,
"[POST]文件上传成功"
),
/**
* 用户删除数据成功
*/
NO_CONTENT
(
204
,
" [DELETE]:用户删除数据成功"
),
/**
* 用户发出的请求有错误,服务器没有进行新建或修改数据的操作
*/
INVALID_REQUEST
(
400
,
"[POST/PUT/PATCH]:用户发出的请求有错误,服务器没有进行新建或修改数据的操作"
),
/**
* 表示用户没有权限(令牌、用户名、密码错误)
*/
UNAUTHORIZED
(
401
,
" [*]:表示用户没有权限(令牌、用户名、密码错误)"
),
/**
* 表示用户登录超时
*/
LOGINOUTTIME
(
402
,
" [*]:表示用户登录超时"
),
/**
* 表示用户得到授权(与401错误相对),但是访问是被禁止的
*/
FORBIDDEN
(
403
,
" [*] 表示用户得到授权(与401错误相对),但是访问是被禁止的"
),
/**
* 用户发出的请求针对的是不存在的记录,服务器没有进行操作,该操作是幂等的
*/
NOT_FOUND
(
404
,
" [*]:用户发出的请求针对的是不存在的记录,服务器没有进行操作"
),
/**
* 非法参数,请求中附带的参数不符合要求规范
*/
ILLEGAL_PARAMETER
(
405
,
"[*]:非法参数,请求中附带的参数不符合要求规范"
),
/**
* 用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)
*/
NOT_ACCEPTABLE
(
406
,
" [GET]:用户请求的格式不可得(比如用户请求JSON格式,但是只有XML格式)"
),
/**
* 用户的参数不得为空
*/
NOT_NULL_PARAMETER
(
408
,
"传递的参数不能为空"
),
/**
* 用户请求的资源被永久删除,且不会再得到的
*/
GONE
(
413
,
"[GET]:用户请求的资源被永久删除,且不会再得到的"
),
/**
* [PUT,PATCH,POST,DELETE]:操作没有成功,并没有数据发生变化
*/
NO_CHANGED
(
414
,
"[PUT,PATCH,POST,DELETE]:操作没有成功,并没有数据发生变化"
),
/**
* 创建一个对象时,发生一个验证错误
*/
UNPROCESSABLE_ENTITY
(
422
,
"[POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误"
),
/**
* 服务器发生错误
*/
INTERNAL_SERVER_ERROR
(
500
,
"服务器发生错误"
);
/**
* 结果代码编号
*/
private
Integer
code
;
/**
* 结果信息
*/
private
String
msg
;
}
efs-ftp/src/main/java/com/zjty/efs/ftp/base/response/ServerResponse.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
base
.
response
;
import
com.alibaba.fastjson.annotation.JSONField
;
import
lombok.Getter
;
import
static
com
.
zjty
.
efs
.
ftp
.
base
.
response
.
ResponseCode
.*;
/**
* Description : 结果类,用于统一返回格式 对外提供关于响应结果的很多便利的静态方法
*
* @author : M@tr!x [xhyrzldf@foxmail.com]
* @Date : 2017/12/13 0:05
*/
@SuppressWarnings
({
"WeakerAccess"
,
"unused"
})
@Getter
public
class
ServerResponse
<
T
>
{
/**
* 错误码
*/
@JSONField
(
ordinal
=
1
)
private
Integer
code
;
/**
* 提示信息
*/
@JSONField
(
ordinal
=
2
)
private
String
msg
;
/**
* 具体的内容
*/
@JSONField
(
ordinal
=
3
)
private
T
data
;
/* 以下为返回成功响应结果的各类重载静态方法 */
public
static
<
T
>
ServerResponse
<
T
>
success
()
{
return
new
ServerResponse
<>(
OK
.
getCode
(),
OK
.
getMsg
());
}
public
static
<
T
>
ServerResponse
<
T
>
success
(
T
data
)
{
return
new
ServerResponse
<>(
OK
.
getCode
(),
OK
.
getMsg
(),
data
);
}
public
static
<
T
>
ServerResponse
<
T
>
success
(
String
msg
,
T
data
)
{
return
new
ServerResponse
<>(
OK
.
getCode
(),
msg
,
data
);
}
public
static
<
T
>
ServerResponse
<
T
>
saveSuccess
(
T
data
)
{
return
new
ServerResponse
<>(
CREATED
.
getCode
(),
CREATED
.
getMsg
(),
data
);
}
public
static
<
T
>
ServerResponse
<
T
>
deleteSuccess
()
{
return
new
ServerResponse
<>(
NO_CONTENT
.
getCode
(),
NO_CONTENT
.
getMsg
());
}
public
static
<
T
>
ServerResponse
<
T
>
deleteSuccessWithCount
(
Number
effectCount
)
{
return
new
ServerResponse
<>(
NO_CONTENT
.
getCode
(),
"删除成功,操作删除的数据条数为: "
+
effectCount
);
}
public
static
<
T
>
ServerResponse
<
T
>
deleteSuccessWithId
(
Number
id
)
{
return
new
ServerResponse
<>(
NO_CONTENT
.
getCode
(),
"删除成功,操作删除的数据id为: "
+
id
);
}
public
static
<
T
>
ServerResponse
<
T
>
uploadSuccess
(
T
data
)
{
return
new
ServerResponse
<>(
UPLOADED
.
getCode
(),
UPLOADED
.
getMsg
(),
data
);
}
public
static
<
T
>
ServerResponse
<
T
>
messageSuccess
(
String
msg
)
{
return
new
ServerResponse
<>(
OK
.
getCode
(),
msg
);
}
/* 以下为返回失败响应结果的各类重载静态方法 */
public
static
<
T
>
ServerResponse
<
T
>
error
()
{
return
new
ServerResponse
<>(
INTERNAL_SERVER_ERROR
.
getCode
(),
INTERNAL_SERVER_ERROR
.
getMsg
());
}
public
static
<
T
>
ServerResponse
<
T
>
error
(
String
errorMessage
)
{
return
new
ServerResponse
<>(
INTERNAL_SERVER_ERROR
.
getCode
(),
errorMessage
);
}
public
static
<
T
>
ServerResponse
<
T
>
error
(
ResponseCode
responseCode
)
{
return
new
ServerResponse
<>(
responseCode
.
getCode
(),
responseCode
.
getMsg
());
}
public
static
<
T
>
ServerResponse
<
T
>
error
(
ResponseCode
responseCode
,
String
errorMessage
)
{
return
new
ServerResponse
<>(
responseCode
.
getCode
(),
errorMessage
);
}
public
static
<
T
>
ServerResponse
<
T
>
error
(
ResponseCode
responseCode
,
String
errorMessage
,
T
data
)
{
return
new
ServerResponse
<>(
responseCode
.
getCode
(),
errorMessage
,
data
);
}
/* 以下为提供给CONTROL层向文件服务器操作文件获得相应结果的相关方法 */
// /**
// * <b>向文件服务器上传文件并且取得{@code {@link ServerResponse}}类型的响应结果</b>
// * <p>
// * <p>封装了control层的关于文件上传的代码
// * <p>
// * <p>例如file是a.txt,dirName=template, 在文件服务器上存放的位置就是{@code root(根目录)/template/a.txt }
// *
// * @param file {@link MultipartFile} 接受到的文件俺对象
// * @param dirName 在文件服务器上的文件夹名
// * @return {@link ServerResponse} (包含了成功与导致失败的结果)
// */
// public static ServerResponse uploadAndGet(
// @RequestParam("file") Optional<MultipartFile> file, String dirName) {
// // 空指针判断
// if (!file.isPresent()) return error(NOT_NULL_PARAMETER);
// // 上传文件并且获得相应结果
// MultipartFile uploadFile = file.get();
// FileServerResponse fsp =
// HttpClientUtil.uploadFileToServer(uploadFile, dirName, uploadFile.getOriginalFilename());
// // 根据返回结果的状态码确定相应的返回信息(200返回成功,否则error)
// return (fsp.getCode() == 200) ? uploadSuccess(fsp.getData()) : error(NO_CHANGED, fsp.getMsg());
// }
// /**
// * <b>向文件服务器删除文件并且取得{@code {@link ServerResponse}}类型的响应结果</b>
// * <p>
// * <p>封装了control层的关于文件上传的代码
// * <p>
// * <p>例如提供的参数如下
// * <li>fileName = reset-hv_20180115144834_wV9A9iVD.png
// * <li>dirName = templates
// * <li>那么实际删除的文件路径为: {@code root(根目录)/templates/reset-hv_20180115144834_wV9A9iVD.png}
// *
// * @param fileName 要删除的文件名<b>注意是文件服务器上的文件名,例如<b>reset-hv_20180115144834_wV9A9iVD.png</b> 格式为{@code
// * 原始文件名_timestamp_uuid8位}
// * @param dirName 要删除的文件所处的文件目录
// * @return {@link ServerResponse} (包含了成功与导致失败的结果)
// */
// public static ServerResponse deleteAndGet(
// @RequestParam(value = "fileName") Optional<String> fileName, String dirName) {
// // 空指针判断
// if (!fileName.isPresent()) return error(NOT_NULL_PARAMETER);
// // 发送删除文件的请求,附带文件所在的目录和在服务器中的文件名
// FileServerResponse fsp = HttpClientUtil.daleteFileToServer(dirName, fileName.get());
// // 根据返回结果的状态码确定相应的返回信息(200返回成功,否则error)
// return (fsp.getCode() == 200) ? deleteSuccess() : error(NO_CHANGED, fsp.getMsg());
// }
/* 将构造器私有,防止外部进行实例化 仅提供给内部静态方法调用 * */
private
ServerResponse
()
{
}
private
ServerResponse
(
Integer
code
)
{
this
.
code
=
code
;
}
private
ServerResponse
(
Integer
code
,
String
msg
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
}
private
ServerResponse
(
String
msg
,
T
data
)
{
this
.
msg
=
msg
;
this
.
data
=
data
;
}
private
ServerResponse
(
Integer
code
,
String
msg
,
T
data
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
this
.
data
=
data
;
}
}
efs-ftp/src/main/java/com/zjty/efs/ftp/controller/FileDownLoadController.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
controller
;
import
com.zjty.efs.ftp.service.FileDownLoadService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
@RestController
@RequestMapping
(
"/file"
)
public
class
FileDownLoadController
{
@Autowired
private
FileDownLoadService
fileDownLoadService
;
@GetMapping
(
"/downLoad/{fileName}"
)
public
void
fileDownLoad
(
@PathVariable
(
"fileName"
)
String
fileName
,
HttpServletResponse
response
,
HttpServletRequest
request
){
fileDownLoadService
.
fileDownLoad
(
fileName
,
response
,
request
);
}
}
efs-ftp/src/main/java/com/zjty/efs/ftp/controller/FileUploadController.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
controller
;
import
com.zjty.efs.ftp.base.response.ServerResponse
;
import
com.zjty.efs.ftp.service.FileUploadService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PutMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.servlet.http.HttpServletRequest
;
@RestController
@RequestMapping
(
"/file"
)
public
class
FileUploadController
{
@Autowired
private
FileUploadService
fileUploadService
;
//文件上传
@PutMapping
(
"/upload"
)
public
ServerResponse
fileUpload
(
HttpServletRequest
httpServletRequest
){
return
fileUploadService
.
fileUpload
(
httpServletRequest
);
}
}
efs-ftp/src/main/java/com/zjty/efs/ftp/entity/FileReturn.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
entity
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
@AllArgsConstructor
@NoArgsConstructor
@Data
public
class
FileReturn
{
private
String
name
;
//上传文件的名称
private
String
path
;
//上传文件的地址
}
efs-ftp/src/main/java/com/zjty/efs/ftp/service/FileDownLoadService.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
service
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
public
interface
FileDownLoadService
{
/**
* 文件下载
* @param fileName
* @param response
* @param httpServletRequest
*/
void
fileDownLoad
(
String
fileName
,
HttpServletResponse
response
,
HttpServletRequest
httpServletRequest
);
}
efs-ftp/src/main/java/com/zjty/efs/ftp/service/FileUploadService.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
service
;
import
com.zjty.efs.ftp.base.response.ServerResponse
;
import
javax.servlet.http.HttpServletRequest
;
public
interface
FileUploadService
{
/**
* 文件上传
* @param request
* @return
*/
ServerResponse
fileUpload
(
HttpServletRequest
request
);
}
efs-ftp/src/main/java/com/zjty/efs/ftp/service/impl/FileDownLoadServiceImpl.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
service
.
impl
;
import
com.zjty.efs.ftp.service.FileDownLoadService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
@Service
@Slf4j
public
class
FileDownLoadServiceImpl
implements
FileDownLoadService
{
@Value
(
"${file.address}"
)
private
String
fileAddress
;
@Override
public
void
fileDownLoad
(
String
fileName
,
HttpServletResponse
response
,
HttpServletRequest
httpServletRequest
)
{
if
(
fileName
!=
null
){
response
.
setHeader
(
"content-type"
,
"application/octet-stream"
);
response
.
setContentType
(
"application/octet-stream"
);
try
{
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
new
String
(
fileName
.
getBytes
(
"utf-8"
),
"utf-8"
));
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
File
sourceFile
=
new
File
(
fileAddress
);
boolean
fileExist
=
false
;
File
[]
files
=
sourceFile
.
listFiles
();
if
(
files
!=
null
&&
files
.
length
!=
0
){
for
(
File
f:
files
){
if
(
f
.
getName
().
startsWith
(
fileName
)){
fileName
=
f
.
getName
();
fileExist
=
true
;
break
;
}
}
}
if
(
fileExist
){
File
file
=
new
File
(
fileAddress
+
"/"
+
fileName
);
setFileDownloadHeader
(
httpServletRequest
,
response
,
fileName
);
OutputStream
os
=
null
;
InputStream
is
=
null
;
BufferedInputStream
bs
=
null
;
byte
[]
buffer
=
new
byte
[
1024
];
try
{
is
=
new
FileInputStream
(
file
);
bs
=
new
BufferedInputStream
(
is
);
os
=
response
.
getOutputStream
();
int
i
=
bs
.
read
(
buffer
);
while
(
i
!=
-
1
)
{
os
.
write
(
buffer
,
0
,
i
);
i
=
bs
.
read
(
buffer
);
}
os
.
flush
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
bs
.
close
();
is
.
close
();
os
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
else
{
log
.
info
(
"该文件不存在"
);
}
}
}
/**
* 解决中文名称
* @param request
* @param response
* @param fileName
*/
public
void
setFileDownloadHeader
(
HttpServletRequest
request
,
HttpServletResponse
response
,
String
fileName
)
{
try
{
//中文文件名支持
String
encodedfileName
;
String
agent
=
request
.
getHeader
(
"USER-AGENT"
);
if
(
null
!=
agent
&&
agent
.
contains
(
"MSIE"
))
{
//IE
encodedfileName
=
java
.
net
.
URLEncoder
.
encode
(
fileName
,
"UTF-8"
);
}
else
if
(
null
!=
agent
&&
agent
.
contains
(
"Mozilla"
))
{
encodedfileName
=
new
String
(
fileName
.
getBytes
(
"UTF-8"
),
"iso-8859-1"
);
}
else
{
encodedfileName
=
java
.
net
.
URLEncoder
.
encode
(
fileName
,
"UTF-8"
);
}
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename=\""
+
encodedfileName
+
"\""
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
}
}
efs-ftp/src/main/java/com/zjty/efs/ftp/service/impl/FileUploadServiceImpl.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
service
.
impl
;
import
com.zjty.efs.ftp.base.response.ServerResponse
;
import
com.zjty.efs.ftp.entity.FileReturn
;
import
com.zjty.efs.ftp.service.FileUploadService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.*
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
@Service
@Slf4j
public
class
FileUploadServiceImpl
implements
FileUploadService
{
@Value
(
"${file.address}"
)
private
String
fileAddress
;
/**
* 前台文件上传
* @param request
* @return
*/
@Override
public
ServerResponse
fileUpload
(
HttpServletRequest
request
)
{
List
<
MultipartFile
>
multipartFiles
=
((
MultipartHttpServletRequest
)
request
).
getFiles
(
"file"
);
List
<
FileReturn
>
fileReturns
=
new
ArrayList
<>();
if
(
multipartFiles
!=
null
&&
multipartFiles
.
size
()
!=
0
){
for
(
MultipartFile
multipartFile:
multipartFiles
){
InputStream
is
=
null
;
BufferedInputStream
bis
=
null
;
FileOutputStream
fos
=
null
;
BufferedOutputStream
bos
=
null
;
FileReturn
fileReturn
=
new
FileReturn
();
String
sourceName
=
multipartFile
.
getOriginalFilename
();
String
suffix
=
sourceName
.
substring
(
sourceName
.
lastIndexOf
(
"."
));
String
fileName
=
UUID
.
randomUUID
().
toString
()
+
suffix
;
//不存在该目录,创建目录
File
file
=
new
File
(
fileAddress
);
if
(!
file
.
exists
()){
file
.
mkdirs
();
}
String
savePath
=
file
.
getAbsolutePath
()
+
"/"
+
fileName
;
File
createFile
=
new
File
(
savePath
);
try
{
is
=
multipartFile
.
getInputStream
();
bis
=
new
BufferedInputStream
(
is
);
fos
=
new
FileOutputStream
(
createFile
);
bos
=
new
BufferedOutputStream
(
fos
);
int
len
=
-
1
;
byte
[]
b
=
new
byte
[
1024
];
while
((
len
=
bis
.
read
(
b
))
!=
-
1
){
bos
.
write
(
b
,
0
,
len
);
}
fileReturn
.
setName
(
sourceName
);
//fileReturn.setPath(savePath);
fileReturns
.
add
(
fileReturn
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
continue
;
}
finally
{
try
{
bis
.
close
();
is
.
close
();
bos
.
close
();
fos
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
else
{
log
.
info
(
"上传失败,请重新确定key值,重新上传"
);
return
ServerResponse
.
error
(
"上传失败,请重新确定key值,重新上传"
);
}
return
ServerResponse
.
success
(
fileReturns
);
}
}
efs-ftp/src/main/java/com/zjty/efs/ftp/utils/TimeUtil.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
utils
;
import
java.time.LocalDateTime
;
public
class
TimeUtil
{
public
static
String
getNowDate
(){
LocalDateTime
date
=
LocalDateTime
.
now
();
return
date
.
getHour
()
+
":"
+
date
.
getMinute
()
+
":"
+
date
.
getSecond
();
}
}
efs-ftp/src/main/java/com/zjty/efs/ftp/utils/ZipUtils.java
0 → 100644
浏览文件 @
a3f41036
package
com
.
zjty
.
efs
.
ftp
.
utils
;
import
java.io.*
;
import
java.util.List
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
public
class
ZipUtils
{
private
static
final
int
BUFFER_SIZE
=
2
*
1024
;
/**
* 压缩成ZIP 方法 * @param srcDir 压缩文件夹路径
* @param out 压缩文件输出流
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws RuntimeException 压缩失败会抛出运行时异常
*/
public
static
void
toZip
(
String
srcDir
,
OutputStream
out
,
boolean
KeepDirStructure
)
throws
RuntimeException
{
long
start
=
System
.
currentTimeMillis
();
ZipOutputStream
zos
=
null
;
try
{
zos
=
new
ZipOutputStream
(
out
);
File
sourceFile
=
new
File
(
srcDir
);
compress
(
sourceFile
,
zos
,
sourceFile
.
getName
(),
KeepDirStructure
);
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"压缩完成,耗时:"
+
(
end
-
start
)
+
" ms"
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"zip error from ZipUtils"
,
e
);
}
finally
{
if
(
zos
!=
null
){
try
{
zos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
/**
* 压缩成ZIP 方法 * @param srcFiles 需要压缩的文件列表
* @param out 压缩文件输出流
* @throws RuntimeException 压缩失败会抛出运行时异常
*/
public
static
void
toZip
(
List
<
File
>
srcFiles
,
OutputStream
out
)
throws
RuntimeException
{
long
start
=
System
.
currentTimeMillis
();
ZipOutputStream
zos
=
null
;
try
{
zos
=
new
ZipOutputStream
(
out
);
for
(
File
srcFile
:
srcFiles
)
{
byte
[]
buf
=
new
byte
[
BUFFER_SIZE
];
zos
.
putNextEntry
(
new
ZipEntry
(
srcFile
.
getName
()));
int
len
;
FileInputStream
in
=
new
FileInputStream
(
srcFile
);
while
((
len
=
in
.
read
(
buf
))
!=
-
1
){
zos
.
write
(
buf
,
0
,
len
);
}
zos
.
closeEntry
();
in
.
close
();
}
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"压缩完成,耗时:"
+
(
end
-
start
)
+
" ms"
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"zip error from ZipUtils"
,
e
);
}
finally
{
if
(
zos
!=
null
){
try
{
zos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
/**
* 递归压缩方法
* @param sourceFile 源文件
* @param zos zip输出流
* @param name 压缩后的名称
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
* @throws Exception
*/
private
static
void
compress
(
File
sourceFile
,
ZipOutputStream
zos
,
String
name
,
boolean
KeepDirStructure
)
throws
Exception
{
byte
[]
buf
=
new
byte
[
BUFFER_SIZE
];
if
(
sourceFile
.
isFile
()){
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos
.
putNextEntry
(
new
ZipEntry
(
name
));
// copy文件到zip输出流中
int
len
;
FileInputStream
in
=
new
FileInputStream
(
sourceFile
);
while
((
len
=
in
.
read
(
buf
))
!=
-
1
){
zos
.
write
(
buf
,
0
,
len
);
}
// Complete the entry
zos
.
closeEntry
();
in
.
close
();
}
else
{
//是文件夹
File
[]
listFiles
=
sourceFile
.
listFiles
();
if
(
listFiles
==
null
||
listFiles
.
length
==
0
){
// 需要保留原来的文件结构时,需要对空文件夹进行处理
if
(
KeepDirStructure
){
// 空文件夹的处理
zos
.
putNextEntry
(
new
ZipEntry
(
name
+
"/"
));
// 没有文件,不需要文件的copy
zos
.
closeEntry
();
}
}
else
{
for
(
File
file
:
listFiles
)
{
// 判断是否需要保留原来的文件结构
if
(
KeepDirStructure
)
{
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress
(
file
,
zos
,
name
+
"/"
+
file
.
getName
(),
KeepDirStructure
);
}
else
{
compress
(
file
,
zos
,
file
.
getName
(),
KeepDirStructure
);
}
}
}
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
/** 测试压缩方法 */
FileOutputStream
fos1
=
new
FileOutputStream
(
new
File
(
"C:\\Users\\xiaoge\\Desktop\\领导.zip"
));
ZipUtils
.
toZip
(
"C:\\Users\\xiaoge\\Desktop\\右侧\\领导"
,
fos1
,
true
);
}
}
efs-ftp/src/main/resources/application.properties
0 → 100644
浏览文件 @
a3f41036
server.port
=
8093
# spring-http-ļϴ
spring.servlet.multipart.max-file-size
=
100MB
spring.servlet.multipart.max-request-size
=
100MB
file.address
=
/file/uploads
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论