介绍:前台上传控件采用uploadify,后台框架springMVC,FTP为vsftp系统centos6.5。采用sun.net下的ftp相应api。
1. Controller中接收上传文件
/**
* 附件ajax上传处理
* @author xxx
* @date 2015-7-10 下午4:30:32
* @param request
* @param response
* @throws Exception
* @throws
*/
@RequestMapping(value = "uploadFiles")
public void uploadFiles(HttpServletRequest request, HttpServletResponse response) throws Exception{
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
// 上传文件处理
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iter = multiRequest.getFileNames();
Attach attach = null;
while (iter.hasNext()) { // 只有一个附件
MultipartFile file = multiRequest.getFile(iter.next()); // 取得上传文件
String fileName = file.getOriginalFilename(); // 取得当前上传文件的文件名称
if (fileName.trim() != "") {
String formName = file.getName(); // 文件所在表单的name值,对应前台fileObjName值
String md5 = FtpUtil.getMD5(file.getInputStream()); // 文件的MD5名称
long result = FtpUtil.upload(md5, file.getInputStream()); // 上传文件到ftp
if(result > 0){
// attach = UploadFileUtil.packAttach(fileName, file, formName);
}
}
}
try {
response.getWriter().write(JSONObject.fromObject(attach).toString());
} catch (IOException e) {
e.printStackTrace();
}
}
2.ftp上传工具类
/**
* ftp工具类
* @author xxx
* @date 2015-10-9 下午2:06:14
*/
public class FtpUtil {
private static FtpClient ftpClient = null;
/**
* 连接ftp服务器
* @author xxx
* @date 2015-10-9 上午10:21:01
* @param ip ftp服务器IP地址
* @param port ftp开放端口
* @param path ftp文件路径
* @param user 用户名
* @param password 密码
* @return
* @throws IOException
*/
public static boolean connectServer(String ip, int port, String path, String user, String password) throws IOException {
boolean bool = false; // 是否连接成功
try{
ftpClient = new FtpClient();
ftpClient.openServer(ip , port); // 开启服务
ftpClient.login(user, password); // 登录
if (path.length() != 0){
ftpClient.cd(path);
}
ftpClient.binary(); // 用2进制上传、下载
bool = true;
}catch(Exception e){
e.printStackTrace();
if(ftpClient != null){
ftpClient.closeServer();
}
bool = false;
}
return bool;
}
public static boolean connectServer(){
try {
return connectServer("192.168.137.0", 21, "/ftp", "ftp_ppp", "123456");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 上传文件到ftp
* @author xxx
* @date 2015-10-9 上午10:30:35
* @param fileName 保存的文件名称
* @param inst 输入流
* @return
* @throws Exception
*/
public static long upload(String fileName, InputStream inst) throws Exception {
if(!connectServer()){
return -1; // 如果连接失败返回-1
}
long result = 0;
TelnetOutputStream os = null; // ftp输出流
try {
ftpClient.sendServer("passive");
os = ftpClient.put(fileName);
byte[] bytes = new byte[10240];
int c;
while ((c = inst.read(bytes)) != -1) {
os.write(bytes, 0, c);
result +=c;
}
} catch(Exception e){
e.printStackTrace();
}finally {
if (inst != null) {
inst.close();
}
if (os != null) {
os.close();
}
ftpClient.closeServer();
}
return result;
}
/**
* 下载ftp文件
* @author xxx
* @date 2015-10-9 下午2:09:37
* @param ftpFileName 保存在ftp的文件名称
* @param response
* @throws Exception
*/
public static void download(String ftpFileName, HttpServletResponse response) throws Exception{
if(connectServer()){
TelnetInputStream is = null;
ServletOutputStream os = null;
try {
is = ftpClient.get(ftpFileName);
os = response.getOutputStream();
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
is.close();
os.close();
ftpClient.closeServer();
}
}
}
/**
* 获得文件md5值
* @author xxx
* @date 2015-10-9 上午10:52:28
* @param is
* @return
*/
public static String getMD5(InputStream is){
String md5 = "";
try {
md5 = DigestUtils.md5Hex(is);
} catch (IOException e) {
e.printStackTrace();
}
IOUtils.closeQuietly(is);
return md5;
}
}