package com.bjsxt.ego.utils;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
/**
* tomcat上传工具类
* @author Administrator
*
*/
public class FtpUtils {
public static void uploadFile(
String host, //主机ip
Integer port, //端口号
String username, //用户名
String password, //密码
String path,//文件路径
String remote,//存放名
InputStream local) {
try {
//创建FTPClient对象
FTPClient client=new FTPClient();
//获取连接
client.connect(host, port);
//连接登录
client.login(username, password);
//设置文件上传类型
client.setFileType(FTP.BINARY_FILE_TYPE);
//将文件保存再该目录下 如果没有就创建
if(!client.changeWorkingDirectory(path)) {
client.makeDirectory(path);
client.changeWorkingDirectory(path);
}
//保存文件
client.storeFile(remote, local);
//断开连接
client.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
还没有评论,来说两句吧...