- package cn.com.songjy;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
-
- import org.apache.log4j.Logger;
-
- import sun.net.TelnetOutputStream;
- import sun.net.ftp.FtpClient;
-
- public class FTPUPload {
-
- private static Logger log = Logger.getLogger(FTPUPload.class);
- static FtpClient ftpClient = null;
-
-
-
-
-
-
-
-
-
-
- public static boolean connection(String ip, String user, String password,
- String newpath) {
-
- log.info("远程IP地址:"+ip);
- log.info("FTP账户:"+user);
- log.info("FTP账户密码:"+password);
- log.info("远程目录:"+newpath);
-
- ftpClient = new FtpClient();
- try {
- ftpClient.openServer(ip);
- ftpClient.login(user, password);
- if (newpath.length() > 0) {
- ftpClient.cd(newpath);
- }
- ftpClient.binary();
- return true;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
- public static void upload(String ip, String user, String password,
- String path, String newPath) {
-
- File file = new File(path);
-
- if (!file.exists()){
- log.info("文件不存在!");
- return;
- }
-
- log.info("上传文件全路径:"+file.getAbsolutePath());
-
-
- boolean conn=connection(ip,user,password,newPath);
-
- if(false==conn){
- log.info("建立FTP连接失败!");
- return;
- }
-
- System.getProperties().put("file.encoding", "UTF-8");
- TelnetOutputStream os = null;
- FileInputStream is = null;
-
- try {
-
- is = new FileInputStream(file);
- File newfile=new File(newPath);
- if(newfile.exists()){
- newfile.mkdirs();
- newfile.createNewFile();
- }
-
-
- os = ftpClient.put(newPath+file.getName());
- byte[] b = new byte[1024];
- int c;
- log.info("开始上传文件...");
- while (-1 != (c=is.read(b))) {
- os.write(b, 0, c);
- }
- os.flush();
- os.close();
- is.close();
-
- log.info("文件上传完毕!");
-
- if (null != ftpClient)
- ftpClient.closeServer();
-
- } catch (IOException e) {
- System.err.println("上传文件异常!");
- e.printStackTrace();
- }
-
- }
-
- public static void main(String[] args) {
- upload("192.168.23.1", "songjy", "123456","E:\\Workspaces\\java\\srt\\ezg\\demo-web\\pom.xml", "/songjy/upload/");
- }
-
- }
package cn.com.songjy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class FTPUPload {
private static Logger log = Logger.getLogger(FTPUPload.class);
static FtpClient ftpClient = null;
/**
* 建立FTP连接
* @method connection
* @param ip 远程计算机IP
* @param user ftp用户
* @param password ftp用户的密码
* @param newpath 远程计算机目录
* @return
*/
public static boolean connection(String ip, String user, String password,
String newpath) {
log.info("远程IP地址:"+ip);
log.info("FTP账户:"+user);
log.info("FTP账户密码:"+password);
log.info("远程目录:"+newpath);
ftpClient = new FtpClient();
try {
ftpClient.openServer(ip);
ftpClient.login(user, password);
if (newpath.length() > 0) {
ftpClient.cd(newpath);
}
ftpClient.binary();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 上传文件至远程计算机
* @method upload
* @param ip 远程计算机IP
* @param user ftp用户
* @param password ftp用户的密码
* @param path 需上传文件的路径
* @param newPath 远程ftp上传目录
*/
public static void upload(String ip, String user, String password,
String path, String newPath) {
File file = new File(path);
if (!file.exists()){
log.info("文件不存在!");
return;
}
log.info("上传文件全路径:"+file.getAbsolutePath());
//建立FTP连接
boolean conn=connection(ip,user,password,newPath);
if(false==conn){
log.info("建立FTP连接失败!");
return;
}
System.getProperties().put("file.encoding", "UTF-8");
TelnetOutputStream os = null;
FileInputStream is = null;
try {
is = new FileInputStream(file);
File newfile=new File(newPath);
if(newfile.exists()){
newfile.mkdirs();
newfile.createNewFile();
}
os = ftpClient.put(newPath+file.getName());
byte[] b = new byte[1024];
int c;
log.info("开始上传文件...");
while (-1 != (c=is.read(b))) {
os.write(b, 0, c);
}
os.flush();
os.close();
is.close();
log.info("文件上传完毕!");
if (null != ftpClient)
ftpClient.closeServer();
} catch (IOException e) {
System.err.println("上传文件异常!");
e.printStackTrace();
}
}
public static void main(String[] args) {
upload("192.168.23.1", "songjy", "123456","E:\\Workspaces\\java\\srt\\ezg\\demo-web\\pom.xml", "/songjy/upload/");
}
}