SpringMVC 实现图片上传

思路
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 文件上传返回格式
* @author ccnulyq
*
*/
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{
 
@Value ( "${FTP_HOST}" )
private String FTP_HOST ;
@Value ( "${FTP_PORT}" )
private Integer FTP_PORT ;
@Value ( "${FTP_USERNAME}" )
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 ;
}
}

转载于:https://my.oschina.net/u/4117393/blog/3043224

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值