FtpUtils 工具类封装
public static boolean uploadFile(
String hostname, int port, String username, String password, String pathname, String remote,InputStream local) { boolean flag=false; try{ //创建 FtpClient 对象 FTPClient client=new FTPClient(); //建立和 ftp服务器的链接 client.connect(hostname, port); //登陆 ftp服务器 client.login(username, password);
//设置上传的文件的类型 client.setFileType(FTP.BINARY_FILE_TYPE); //切换工作目录,文件上传后保存到那个目录
if(!client.changeWorkingDirectory(pathname)){ if(client.makeDirectory(pathname)){
client.changeWorkingDirectory(pathname); } } local=new FileInputStream("D:/pic/1.jpg"); //实现文件上传 client.storeFile(remote, local); local.close(); client.logout(); client.disconnect(); flag=true; }catch(Exception ex){ ex.printStackTrace(); } return flag; }
通过 Http 访问图片
Ego 上传商品图片
前端控件参数说明
提交上传请求路径 url
提交图片参数名称
上传后返回值 json模型
参考 kindeditor 控件文件上传文档:
KindEditor 4.x Documentation
2.2 创建 PictureResult 封装返回值
package com.bjsxt.ego.beans;/*** * 封装上传图片之后的返回结果 * **/
public class PictureResult { private Integer error;
//状态 1 失败 0 成功 private String url; //
上传图片后,图片在服务器的 url private String message;
//响应到客户端的提示消息 public Integer getError() {
return error;
} public void setError(Integer error) { this.error = error; }
public String getUrl() { return url; } public void setUrl(String url) {this.url = url;} public String getMessage() { return message;
} public void setMessage(String message) { this.message = message; } }
ego-manager-web 功能实现
创建 ftp.properties
FTP_HOST=192.168.30.30FTP_PORT=21
FTP_USERNAME=ftpuser FTP_PASSWORD=ftpuser
FTP_PATH=/home/ftpuser/jd
IMAGE_HTTP_PATH=http://image.taotao.com/jd
创建工具类 IDUtils
ManagerItemService 接口添加方法
public PictureResult uploadItemPic(MultipartFile file);
ManagerItemServiceImpl 实现方法
//通过 spring 的 EL表达式注入 ftp信息 @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_PATH}") private String FTP_PATH; @Value("${IMAGE_HTTP_PATH}") private String IMAGE_HTTP_PATH;
@Override
public PictureResult uploadItemPic(MultipartFile file) { // TODO Auto-generated method stub boolean flag=false; String fileName=null; try{ //获得信息的文件名字 fileName=IDUtils.genImageName();
//获得上传的文件的原始名字
String oriName = file.getOriginalFilename(); //获得文件扩展名
String ext=oriName.substring(oriName.lastIndexOf(".")); fileName=fileName+ext; InputStream local = file.getInputStream(); //实现文件上传到ftp flag=FtpUtils.uploadFile(FTP_HOST, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_PATH, fileName, local); }catch(Exception ex){ ex.printStackTrace(); flag=false; } PictureResult result=null;
if(flag){ result=new PictureResult();result.setError(0); result.setUrl(IMAGE_HTTP_PATH+"/"+fileName); result.setMessage("ok"); }else{ result=new PictureResult(); result.setError(1); result.setUrl("url"); result.setMessage("error");
} return result;
}
创建 ItemImageUploadController类
package com.bjsxt.ego.manager.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import com.bjsxt.ego.beans.PictureResult;
import com.bjsxt.ego.manager.service.ManagerItemService; @Controller
public class ItemImageController { //注入service @Autowired private ManagerItemService managerItemService; /** * 处理图片上传请求 * ***/ @Request
Mapping(value="pic/upload",produces=MediaType. APPLICATION_JSON_VAL UE +";charset=UTF-8") @ResponseBody public PictureResult picUpload(MultipartFile uploadFile){ return managerItemService.uploadItemPic(uploadFile); }}
配置 springmvc.xml
发布测试