思路
(
1
)使用
Springmvc
上传组件,从页面表单接收图片
(
2
)使用
vsftpd
组件,将图片上传到
Linux
服务器。
(
a
)、服务端:在
Linux
上安装
ftp
服务端
vsftpd
软件,并开启服务。
(
b
)、客户端:在
java
代码中使用
FtpClient
客户端建立与服务器的连接
(
3
)返回值:
返回图片上传之后的访问路径。
为什么?
因为保存图片到数据库的时候,保存的就是图片的
访问路径。
前端
js
实现
前端使用
kindeditor
,初始化上传组件
后台
java
实现
3.4.3.3.1
代码结构
Controller
:从表单接收图片,返回图片的回调地址
Service
:创建
FtpClient
客户端,将图片直接上传到
Linux
服务器
3.4.3.3.2
请求响应格式
请求路径
/pic/upload
请求方式
Post
请求参数
uploadFile
返回值结构
参考
Kindeditor
官方文档
(
http://kindeditor.net/docs/upload.html
)
Kindeditor
官方文档要求的返回格式类型JSON
定义返回值类型
在
ego-base
工程中定义。
package
cn.gzsxt.base.pojo;
/**
* KindEditer
文件上传返回格式
*
*/
public class
UploadResult {
private int
error
;
//0
表示成功
1
表示失败
private
String
url
;
//
成功时,图片的访问地址
private
String
message
;
//
失败时,错误信息
public
PictureResult() {
super
();
}
//
补充
get
、
set
方法
}
在
ego-manager
工程中添加
Springmvc
上传组件及
Pom
依赖
(
1
)、修改
spring-mvc.xml
,添加上传组件
<
bean
name
=
"multipartResolver"
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolv
er"
>
<
property
name
=
"defaultEncoding"
value
=
"UTF-8"
>property>
<
property
name
=
"maxUploadSize"
value
=
"5242880"
>property>
bean>
(
2
)、修改
pom.xml
,添加上传依赖
common-fileupload.jar
<
dependency
>
<
groupId
>
commons-fileupload
groupId>
<
artifactId
>
commons-fileupload
artifactId>
dependency>
(
3
)将
vsftpd
服务端请求参数写到
properties
配置文件中
#
图片上传基本配置
FTP_HOST=
虚拟机IP
FTP_PORT=
21(默认)
FTP_USER=
你设置的账号
FTP_PASSWD=
密码
FTP_BASE_URL=
tngine和ftp的共享路径
PICTURE_BASE_URL=
http://虚拟机ip/images
Service
层代码实现
--
创建
UploadService
接口及其实现类
public class
UploadServiceImpl
implements
UploadService{
private
String
FTP_HOST
;
private
Integer
FTP_PORT
;
private
String
FTP_USERNAME
;
@Value
(
"${FTP_PASSWORD}"
)
private
String
FTP_PASSWORD
;
@Value
(
"${FTP_BASE_URL}"
)
private
String
FTP_BASE_URL
;
@Value
(
"${PICTURE_BASE_URL}"
)
private
String
PICTURE_BASE_URL
;
@Override
public
UploadResult upload(MultipartFile
file
) {
UploadResult
result
=
new
UploadResult();
//
需求:将上传的图片按日期来分类
/2019/02/25/1.jpg
Date
date
=
new
Date();
//
获取日期的目录格式
String
filePath
=
"/"
+
new
SimpleDateFormat(
"yyyy"
).format(
date
)+
"/"
+
new
SimpleDateFormat(
"MM"
).format(
date
)+
"/"
+
new
SimpleDateFormat(
"dd"
).format(
date
);
//
获取图片的类型
.jpg .png
String
originalFilename
=
file
.getOriginalFilename();
String
filtType
=
originalFilename
.substring(
originalFilename
.lastIndexOf(
"."
));
String
remoteFileName
= IDUtils.
genImageName
()+
filtType
;
try
{
boolean
upload
= FtpUtils.
upload
(
FTP_HOST
,
FTP_PORT
,
FTP_USERNAME
,
FTP_PASSWORD
,
FTP_BASE_URL
,
filePath
,
remoteFileName
,
file
.getInputStream());
if
(
upload
){
result
.setError(0);
// 192.168.4.253/images /2019/02/25 /
111111.jpg
result
.setUrl(
PICTURE_BASE_URL
+
filePath
+
"/"
+
remoteFileName
);
}
else
{
result
.setError(1);
result
.setMessage(
"
上传失败,请稍后再试!
"
);
}
}
catch
(IOException
e
) {
e
.printStackTrace();
result
.setError(1);
result
.setMessage(
"
上传失败,请稍后再试!
"
);
}
return
result
;
}
}
Controller
层代码实现
--
创建
UploadController
类
@Controller
public class
UploadController {
@Autowired
private
UploadService
uploadService
;
@RequestMapping
(value=
"/pic/upload"
,method=RequestMethod.
POST
)
@ResponseBody
public
UploadResult upload(MultipartFile
uploadFile
){
UploadResult
result
=
uploadService
.upload(
uploadFile
);
return
result
;
}
}