Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xzksxtfw
Overview
Overview
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
常超
xzksxtfw
Commits
1ec91884
Commit
1ec91884
authored
Jan 16, 2024
by
yangyang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
视频上传提交
parent
9ef185fb
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
290 additions
and
7 deletions
+290
-7
pom.xml
+10
-0
src/main/java/com/exam/mapper/VideoMapper.java
+2
-2
src/main/java/com/exam/serviceimpl/VideoServiceImpl.java
+22
-5
src/main/java/com/exam/util/FtpUtil.java
+151
-0
src/main/java/com/exam/util/KeyUtil.java
+73
-0
src/main/resources/application-dev.properties
+14
-0
src/main/resources/application-prod.properties
+13
-0
src/main/resources/application.properties
+5
-0
No files found.
pom.xml
View file @
1ec91884
...
...
@@ -82,6 +82,16 @@
<artifactId>
poi-ooxml
</artifactId>
<version>
3.17
</version>
</dependency>
<dependency>
<groupId>
com.jcraft
</groupId>
<artifactId>
jsch
</artifactId>
<version>
0.1.55
</version>
</dependency>
<dependency>
<groupId>
commons-net
</groupId>
<artifactId>
commons-net
</artifactId>
<version>
3.8.0
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/exam/mapper/VideoMapper.java
View file @
1ec91884
...
...
@@ -19,7 +19,7 @@ public interface VideoMapper {
List
<
Video
>
findAll
();
@Update
(
"update video set videoName = #{name} where videoId = #{id}"
)
public
int
compileVideo
(
String
id
,
String
name
);
public
int
compileVideo
(
@Param
(
"id"
)
String
id
,
@Param
(
"name"
)
String
name
);
@Delete
(
"delete from video where videoId = #{id}"
)
public
int
deleteVideo
(
String
id
);
...
...
@@ -40,5 +40,5 @@ public interface VideoMapper {
"</if>"
+
"</script>"
)
List
<
Video
>
selectVideoByNameOrTime
(
String
name
,
String
startDate
,
String
endDate
);
List
<
Video
>
selectVideoByNameOrTime
(
@Param
(
"name"
)
String
name
,
@Param
(
"startDate"
)
String
startDate
,
@Param
(
"endDate"
)
String
endDate
);
}
src/main/java/com/exam/serviceimpl/VideoServiceImpl.java
View file @
1ec91884
package
com
.
exam
.
serviceimpl
;
import
c
om.exam.entity.PaperPhoto
;
import
c
h.qos.logback.core.util.FileUtil
;
import
com.exam.entity.Video
;
import
com.exam.mapper.VideoMapper
;
import
com.exam.service.VideoService
;
import
com.exam.util.Base64Util
;
import
com.exam.util.FtpUtil
;
import
com.exam.util.KeyUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
...
...
@@ -17,6 +21,8 @@ import java.util.List;
@Service
public
class
VideoServiceImpl
implements
VideoService
{
@Value
(
"${imagesUrl}"
)
public
String
imagesUrl
;
@Autowired
private
VideoMapper
videoMapper
;
...
...
@@ -24,16 +30,23 @@ public class VideoServiceImpl implements VideoService {
@Override
public
int
add
(
MultipartFile
videoFile
,
String
videoName
,
Date
uploadTime
)
{
int
i
=
0
;
if
(
videoFile
!=
null
)
{
String
base64
=
""
;
try
{
base64
=
"data:video/mp4;base64,"
+
Base64Util
.
getBase64
(
videoFile
);
Video
video
=
new
Video
();
video
.
setVideo
(
base64
);
// 上传视频后缀
String
type
=
videoFile
.
getOriginalFilename
().
substring
(
videoFile
.
getOriginalFilename
().
lastIndexOf
(
"."
));
String
videoId
=
KeyUtil
.
getVedioKey
();
String
name
=
videoId
+
type
;
// 上传视频到服务器
if
(
FtpUtil
.
uploadFile
(
name
,
videoFile
.
getInputStream
())){
video
.
setVideoId
(
name
);
video
.
setVideo
(
imagesUrl
+
name
);
video
.
setVideoName
(
videoName
);
video
.
setUploadTime
(
uploadTime
);
i
=
videoMapper
.
add
(
video
);
}
else
{
return
0
;
};
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
0
;
...
...
@@ -55,7 +68,11 @@ public class VideoServiceImpl implements VideoService {
@Override
public
int
deleteVideo
(
String
id
)
{
//删除服务器上的文件
if
(
FtpUtil
.
delFile
(
id
)){
return
videoMapper
.
deleteVideo
(
id
);
};
return
0
;
}
@Override
...
...
src/main/java/com/exam/util/FtpUtil.java
0 → 100644
View file @
1ec91884
package
com
.
exam
.
util
;
import
com.jcraft.jsch.Channel
;
import
com.jcraft.jsch.ChannelSftp
;
import
com.jcraft.jsch.JSch
;
import
com.jcraft.jsch.Session
;
import
lombok.extern.java.Log
;
import
org.apache.commons.net.ftp.*
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Properties
;
/**
* @Author: admin
* @Description: 创建服务器连接
* @Date:Created in 2024/01/16
* @Modified:
*/
@Log
@Component
public
class
FtpUtil
{
private
static
ChannelSftp
sftp
=
null
;
@Value
(
"${host}"
)
public
String
host
;
@Value
(
"${port}"
)
private
String
port
;
@Value
(
"${usernameFtp}"
)
private
String
username
;
@Value
(
"${password}"
)
private
String
password
;
@Value
(
"${basePath}"
)
private
String
basePath
;
public
static
String
hosts
;
private
static
String
ports
;
private
static
String
usernames
;
private
static
String
passwords
;
private
static
String
basePaths
;
//利用@PostConstruct将配置文件中配置的值赋给本地的变量
@PostConstruct
public
void
getHost
(){
hosts
=
this
.
host
;
}
@PostConstruct
public
void
getPort
()
{
ports
=
this
.
port
;
}
@PostConstruct
public
void
getUsername
()
{
usernames
=
this
.
username
;
}
@PostConstruct
public
void
getPassword
()
{
passwords
=
this
.
password
;
}
@PostConstruct
public
void
getBasePath
()
{
basePaths
=
this
.
basePath
;
}
/**
* Description: 向FTP服务器上传文件
* host FTP服务器ip
* port FTP服务器端口
* username FTP登录账号
* password FTP登录密码
* basePath FTP服务器基础目录,/data2/ftpuser/images
* filePath FTP服务器文件存放路径。例如分日期存放:/2022/05/28。文件的路径为basePath
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public
static
boolean
uploadFile
(
String
filename
,
InputStream
input
)
{
boolean
result
=
false
;
FTPClient
ftp
=
new
FTPClient
();
try
{
JSch
jsch
=
new
JSch
();
//获取sshSession 账号-ip-端口
Session
sshSession
=
jsch
.
getSession
(
usernames
,
hosts
,
Integer
.
valueOf
(
ports
));
//添加密码
sshSession
.
setPassword
(
passwords
);
Properties
sshConfig
=
new
Properties
();
sshConfig
.
put
(
"userauth.gssapi-with-mic"
,
"no"
);
//严格主机密钥检查
sshConfig
.
put
(
"StrictHostKeyChecking"
,
"no"
);
sshSession
.
setConfig
(
sshConfig
);
//开启sshSession链接
sshSession
.
connect
();
//获取sftp通道
Channel
channel
=
sshSession
.
openChannel
(
"sftp"
);
//开启
channel
.
connect
();
sftp
=
(
ChannelSftp
)
channel
;
//设置为被动模式
ftp
.
enterLocalPassiveMode
();
//设置上传文件的类型为二进制类型
//进入到要上传的目录 然后上传文件
sftp
.
cd
(
basePaths
);
sftp
.
put
(
input
,
filename
);
input
.
close
();
result
=
true
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
ftp
.
isConnected
())
{
try
{
ftp
.
disconnect
();
}
catch
(
IOException
ioe
)
{
}
}
}
return
result
;
}
/**
* 删除ftp服务器上的文件
*
* @param fileName ftp服务器保存目录
* @return 成功返回true,否则返回false
*/
public
static
boolean
delFile
(
String
fileName
){
boolean
flag
=
false
;
try
{
JSch
jsch
=
new
JSch
();
Session
sshSession
=
jsch
.
getSession
(
usernames
,
hosts
,
Integer
.
valueOf
(
ports
));
sshSession
.
setPassword
(
passwords
);
sshSession
.
setConfig
(
"StrictHostKeyChecking"
,
"no"
);
sshSession
.
connect
();
Channel
channel
=
sshSession
.
openChannel
(
"sftp"
);
channel
.
connect
();
ChannelSftp
sftpChannel
=
(
ChannelSftp
)
channel
;
sftpChannel
.
rm
(
basePaths
+
"/"
+
fileName
);
sftpChannel
.
disconnect
();
sshSession
.
disconnect
();
System
.
out
.
println
(
"文件删除成功!"
);
flag
=
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
flag
;
}
}
src/main/java/com/exam/util/KeyUtil.java
0 → 100644
View file @
1ec91884
package
com
.
exam
.
util
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.UUID
;
public
class
KeyUtil
{
/**
*
* @Description: 生成唯一的主键 格式: 19位
* @Param:
* @return: java.lang.String
* @Author: admin
* @date: 2018/8/12 0012-23:14
*/
public
static
synchronized
String
getUUIDKey
(
String
str
)
{
String
uuid
=
System
.
currentTimeMillis
()+
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
).
substring
(
0
,
6
);
return
str
+
uuid
;
}
/**
* 生成申请表编号 19位
* @return
*/
public
static
synchronized
String
getVedioKey
()
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
Date
date
=
new
Date
();
String
dateNowStr
=
sdf
.
format
(
date
);
int
num
=
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
).
hashCode
();
num
=
num
<
0
?
-
num:
num
;
return
"VIDEO"
+
dateNowStr
+
String
.
valueOf
(
num
).
substring
(
0
,
7
);
}
/**
*
* @Description: 生成唯一的主键 格式: 19位
* @Param:
* @return: java.lang.String
* @Author: admin
* @date: 2018/8/12 0012-23:14
*/
public
static
synchronized
String
getJzKey
(
String
str
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMdd"
);
Date
date
=
new
Date
();
String
dateNowStr
=
sdf
.
format
(
date
);
int
num
=
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
).
hashCode
();
num
=
num
<
0
?
-
num:
num
;
String
asd
=
str
+
dateNowStr
+
String
.
valueOf
(
num
).
substring
(
0
,
9
);
return
asd
;
}
/**
*
* @Description: 生成唯一的主键 格式:
*/
public
static
synchronized
String
getJzKey1
(
String
str
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMdd"
);
Date
date
=
new
Date
();
String
dateNowStr
=
sdf
.
format
(
date
);
int
num
=
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
).
hashCode
();
num
=
num
<
0
?
-
num:
num
;
String
substring
=
String
.
valueOf
(
num
).
substring
(
0
,
6
);
String
asd
=
str
+
substring
;
return
asd
;
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
getVedioKey
());
}
}
src/main/resources/application-dev.properties
View file @
1ec91884
...
...
@@ -4,3 +4,17 @@ spring.datasource.url=jdbc:mysql://47.92.48.137:3900/gkpt?useUnicode=true&charac
spring.datasource.driver-class-name
=
com.mysql.cj.jdbc.Driver
spring.datasource.type
=
com.alibaba.druid.pool.DruidDataSource
mybatis.configuration.mapUnderscoreToCamelCase
=
true
#FTP服务器ip
host
=
47.92.48.137
#FTP服务器端口
port
=
54322
#FTP登录账号
usernameFtp
=
root
#FTP登录密码
password
=
CcYunzheng900118.
#FTP路径
basePath
=
/data/images
#video服务器路径
imagesUrl
=
http://47.92.48.137:1008/images/
src/main/resources/application-prod.properties
View file @
1ec91884
...
...
@@ -4,3 +4,16 @@ spring.datasource.url=jdbc:mysql://77.1.24.51:3306/test?useUnicode=true&characte
spring.datasource.driver-class-name
=
com.mysql.cj.jdbc.Driver
spring.datasource.type
=
com.alibaba.druid.pool.DruidDataSource
mybatis.configuration.mapUnderscoreToCamelCase
=
true
#FTP服务器ip
host
=
77.1.24.19
#FTP服务器端口
port
=
62222
#FTP登录账号
usernameFtp
=
root
#FTP登录密码
password
=
founder123
#FTP路径
basePath
=
/data/images
#video服务器路径
imagesUrl
=
http://77.1.24.19:1008/images/
src/main/resources/application.properties
View file @
1ec91884
...
...
@@ -3,6 +3,11 @@ server.port=8080
logging.level.com.exam.mapper
=
debug
#设置单个文件大小
spring.servlet.multipart.max-file-size
=
1024MB
#设置单次请求文件的总大小
spring.servlet.multipart.max-request-size
=
1024MB
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