spring上传文件到ftp服务器 有大神解答一下吗!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
apache提供的网络包,commons-net.jar3
http://www.open-open.com/lib/view/open1384090071946.html
@RequestMapping(value = "imageUpload", method = RequestMethod.POST)
@ResponseBody
public Map imageUpload(@RequestParam(value = "imgFile", required = false) MultipartFile imgFile,
HttpServletRequest request) {
String tomcatPath = request.getServletContext().getRealPath("/");
String path = tomcatPath + "/images/" + DateUtil.getDateInfo() + "/img/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = imgFile.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
String[] images = { ".jpg", ".jpeg", ".png", ".bmp", ".ico", ".gif" };// 定义图片格式数组
boolean flag = false;
for (int i = 0; i < images.length; i++) {
if (suffix.equals(images[i])) {
flag = true;
break;
}
}
Map succMap = new HashMap();
String returnCode = null;
Boolean boole = true;
if (flag) {
fileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
path = path + fileName;
File saveFile = new File(path);
if(!saveFile.exists()){
try {
saveFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
imgFile.transferTo(saveFile);
boole = ImageUtil.isImage(saveFile);
if (boole) {
returnCode = ReturnCode.UPLOAD_SUCCESS.toString();
// FTP同步图片
if (PropertiesUtils.STATIC_IMGFTP_STATUS.equals("true")) {// 启用ftp同步
ImageUtil ftp = new ImageUtil();
ftp.ftpLogin();
Boolean bool = ftp.uploadFile(saveFile, "/images/" + DateUtil.getDateInfo() + "/img/");
if (bool) {
returnCode = ReturnCode.UPLOAD_SUCCESS.toString();
} else {
returnCode = ReturnCode.UPLOAD_ERROR.toString();
}
// 关闭上传
ftp.ftpLogOut();
}
} else {
if (saveFile.exists()) {
saveFile.delete();// 删除文件
}
returnCode = ReturnCode.UPLOAD_ERROR.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
String result = path.replace(tomcatPath, "");
/*
* if(result!=null) { returnCode =
* ReturnCode.UPLOAD_SUCCESS.toString(); } else { returnCode =
* ReturnCode.UPLOAD_ERROR.toString(); }
*/
succMap.put("url", result);
} else {
returnCode = ReturnCode.UPLOAD_ASTRICT.toString();
}
// Nick
// JSONObject jsonObject = new JSONObject();
// jsonObject = JSONObject.fromObject(returnCode);
succMap.put("returnCode", returnCode);
return succMap;
}
直接贴程序,镇楼