2 novemberrainobject NovemberRainObject 于 2017.01.10 12:25 提问

spring上传文件到ftp服务器 有大神解答一下吗

spring上传文件到ftp服务器 有大神解答一下吗!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

2个回答

JE_GE
JE_GE   2017.01.10 12:47

apache提供的网络包,commons-net.jar3

http://www.open-open.com/lib/view/open1384090071946.html

u014266877
u014266877   2017.01.10 17:14

@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;
}
直接贴程序,镇楼

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!