stomach + springmvc
Scenario: Upload the uploaded file to an update directory under webapp first, then upload it to the ftp server, and then delete the update folder
controller
/**
*
* @author 刘中华
* @date 2018/3/14 22:02
* @param [file, request]
* @return com.mmall.common.ServerResponse
* @Description 图片上传ftp服务器
*/
@RequestMapping("/upload.do")
@ResponseBody
public ServerResponse upload(HttpSession httpSession,MultipartFile file , HttpServletRequest request){
//检查是否登陆
if (httpSession.getAttribute(Const.CURRENT_USER)==null){
return ServerResponse.createByError("管理员未登陆");
}
//检查是否是管理员
if (iUserService.checkAdmin((User) httpSession.getAttribute(Const.CURRENT_USER)).isSuccess()){
String path = request.getSession().getServletContext().getRealPath("upload");
String targetName = iFileService.upload(file, path);
String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetName;
HashMap fileMap = Maps.newHashMap();
fileMap.put("url",url);
fileMap.put("uri",targetName);
return ServerResponse.createBySuccess(fileMap);
}
return ServerResponse.createByError("权限不够");
}
iFileService upload file service
public String upload(MultipartFile file , String filePath){
//首先得到这个文件的名字
String filename = file.getOriginalFilename();
//然后得到这个文件的扩展名
String jpg = filename.substring(filename.indexOf(".") + 1);
//然后生成一个uuid随机数加扩展名jpg
String newFileName = UUID.randomUUID() +"."+ jpg;
//然后把文件复制到指定的文件中
File f=new File(filePath);
if (!f.exists()){
f.setWritable(true);
f.mkdirs();
}
File targetFile = new File(f,newFileName);
try {
file.transferTo(targetFile);
// 将图片上传到图片服务器
boolean b = FTPUtil.uploadFile(Lists.<File>newArrayList(targetFile));
if (b){
boolean delete = targetFile.delete();
if (delete){
log.info("文件已经从upload文件夹删除");
}else {
log.error("文件从upload文件夹删除失败");
}
}else {
log.error("文件从将图片上传到图片服务器失败");
}
} catch (IOException e) {
log.error("上传图片失败");
e.printStackTrace();
}
return targetFile.getName();
}
FTPUtil ftp tool class
package com.mmall.untis;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
/**
* Created by 敲代码的卡卡罗特
* on 2018/3/14 20:51.
*/
@Slf4j
@Data
public class FTPUtil {
private static String ftpIp=PropertiesUtil.getProperty("ftp.server.ip");
private static String ftpUser=PropertiesUtil.getProperty("ftp.user");
private static String ftpPass=PropertiesUtil.getProperty("ftp.pass");
private String ip;
private int port;
private String user;
private String pwd;
private FTPClient ftpClient;
public FTPUtil(String ip, int port, String user, String pwd) {
this.ip = ip;
this.port = port;
this.user = user;
this.pwd = pwd;
}
public static boolean uploadFile(List<File> list) throws IOException {
FTPUtil ftpUtil = new FTPUtil(ftpIp, 21, ftpUser, ftpPass);
log.info("ftp开始连接。。。");
boolean b = ftpUtil.uploadFile("G:\\\\FTP", list);
log.info("ftp已经连接服务器,开始上传,上传结果为{}",b);
return b;
}
private boolean uploadFile(String remotePath,List<File> list) throws IOException {
boolean uploaded =true;
FileInputStream fis = null;
//连接ftp服务器
if (connectFTP(ftpIp,port,user,pwd)){
try {
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
ftpClient.enterLocalActiveMode();
for (File fileitem :list){
fis = new FileInputStream(fileitem);
ftpClient.storeFile(fileitem.getName(),fis);
}
} catch (IOException e) {
log.error("ftp上传文件异常");
uploaded=false;
e.printStackTrace();
}finally {
fis.close();
ftpClient.disconnect();
}
}
return uploaded;
}
private boolean connectFTP(String ip , int port , String user ,String pwd){
boolean isSuccess=false;
ftpClient = new FTPClient();
try {
ftpClient.connect(ip);
ftpClient.login(user,pwd);
isSuccess=true;
} catch (IOException e) {
log.error("ftp连接失败");
e.printStackTrace();
}
return isSuccess;
}
}
Read configuration file tool class
package com.mmall.untis;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* Created by 敲代码的卡卡罗特
* on 2018/3/13 21:00.
*/
@Slf4j
public class PropertiesUtil {
public static Properties properties = new Properties();
static {
try {
InputStreamReader inputStreamReader = new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream("mmall.properties"), "UTF-8");
properties.load(inputStreamReader);
} catch (IOException e) {
log.error("加载mmall.properties配置文件失败");
e.printStackTrace();
}
}
public static String getProperty(String key,String defaultValue){
String property = properties.getProperty(key.trim());
if (property==null){
return defaultValue;
}
return property;
}
public static String getProperty(String key){
String property = properties.getProperty(key.trim());
return property;
}
}