重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package com.quantongfu.;
成都创新互联公司专注于企业全网营销推广、网站重做改版、富顺网站定制设计、自适应品牌网站建设、HTML5、成都商城网站开发、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为富顺等各大城市提供网站开发制作服务。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.List;
import org.apache.commons.net.;
import org.apache.log4j.Logger;
import org.apache.log4j.net.SocketServer;
import com.quantongfu.conf.FtpConf;
/**
* @项目名称: telinSyslog
* @文件名称:
* @创建日期:2015年9月14日 下午3:22:08
* @功能描述:ftp实体类,用于连接,上传
* @修订记录:
*/
public class Ftp {
private static Logger logger = Logger.getLogger();
private FTPClient ftp;
/**
*
* @param path
* 上传到ftp服务器哪个路径下
* @param addr
* 地址
* @param port
* 端口号
* @param username
* 用户名
* @param password
* 密码
* @return
* @throws Exception
*/
public boolean connect() throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
(FtpConf.FTP_HOST, FtpConf.FTP_PORT);
(FtpConf.FTP_USER_NAME, FtpConf.FTP_PASSWORD);
;
;
reply = ;
if (!FTPReply.isPositiveCompletion(reply)) {
;
return result;
}
if (FtpConf.IS_FTP_DIRECTORY) {
;
}
result = true;
return result;
}
/**
*
* @param files
* 上传的文件
* @throws Exception
*/
public boolean upload(File file) throws IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
boolean b = (file.getName() + ".tmp", input);
if (b) {
b = (file.getName() + ".tmp", file.getName());
}
return b;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (input != null) {
input.close();
}
}
}
/**
*
* @param files
* 上传的文件
* @throws Exception
*/
public boolean upload(ServerSocket server, File file) throws Exception {
FileInputStream input = null;
try {
if (!file.exists()) {
return true;
}
input = new FileInputStream(file);
boolean b = (server, file.getName() + ".tmp", input);
if (b) {
b = (file.getName() + ".tmp", file.getName());
if (b) {
file.delete();
}
}
return b;
} catch (Exception e) {
logger.error("ftp error" + e.getMessage());
return false;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*断开连接*/
public void disConnect() {
try {
if (ftp != null) {
;
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*获取连接*/
public static Ftp getFtp() {
Ftp ftp = new Ftp();
try {
;
} catch (Exception e) {
logger.error("FTP连接异常" + e.getMessage());
e.printStackTrace();
}
return ftp;
}
/*重连*/
public Ftp reconnect() {
disConnect();
return getFtp();
}
}
使用Apache FtpClient jar包,获取jar :
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.;
import org.apache.commons.net.;
public class test {
private FTPClient ftp;
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
;
;
;
reply = ;
if (!FTPReply.isPositiveCompletion(reply)) {
;
return result;
}
;
result = true;
return result;
}
/**
*
* @param file 上传的文件或文件夹
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
(file.getName());
(file.getName());
String[] files = file.list();
for (int i = 0; i files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
;
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
准备条件:java实现ftp上传用到了commons-net-3.3.jar包
首先建立ftphost连接
public boolean connect(String path, String addr, int port, String username, String password) {
try {
//FTPClient ftp = new FTPHTTPClient(addr, port, username, password);
ftp = new FTPClient();
int reply;
;
System.out.println("连接到:" + addr + ":" + port);
System.out.print();
reply = ;
if (!FTPReply.isPositiveCompletion(reply)) {
;
System.err.println("FTP目标服务器积极拒绝.");
System.exit(1);
return false;
}else{
(username, password);
;
;
;
System.out.println("已连接:" + addr + ":" + port);
return true;
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
return false;
}
}
然后再利用ftpclient的makeDirectory方法创建文件夹
public void createDir(String dirname){
try{
;
System.out.println("在目标服务器上成功建立了文件夹: " + dirname);
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
断开host连接
public void disconnect(){
try {
;
} catch (IOException e) {
e.printStackTrace();
}
}
最后是程序的调用方法
public static void main(String[] args) {
FtpUploadTest ftpupload = new FtpUploadTest();
if(ftpupload.connect("", "172.39.8.x", 20, "administrator", "abc@123")){
ftpupload.createDir("/UPLOAD");
ftpupload.disconnect();
}
}
上传下载的代码
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import sun.net.TelnetOutputStream;
import sun.net.TelnetInputStream;
import sun.net.;
public class download {
String localfilename;
String remotefilename;
FtpClient ftpClient;
// server:服务器名字
// user:用户名
// password:密码
// path:服务器上的路径
public void connectServer(String ip, int port,String user
, String password,String path) {
try {
ftpClient = new FtpClient();
ftpClient.openServer(ip,port);
ftpClient.login(user, password);
System.out.println("login success!");
if (path.length() != 0) ftpClient.cd(path);
ftpClient.binary();
} catch (IOException ex) {
System.out.println("not login");
System.out.println(ex);
}
}
public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
System.out.println(ex);
}
}
public void upload() {
this.localfilename = "D://test2//test.txt";
this.remotefilename = "test.txt";
try {
TelnetOutputStream os = ftpClient.put(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
is.close();
os.close();
} catch (IOException ex) {
System.out.println("not upload");
System.out.println(ex);
}
}
public void download() {
try {
TelnetInputStream is = ftpClient.get(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remotePath,String remoteFile,String localFile) {
try {
if (remotePath.length() != 0) ftpClient.cd(remotePath);
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remoteFile,String localFile) {
try {
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public static void main(String agrs[]) {
String filepath[] = { "/callcenter/index.jsp", "/callcenter/ip.txt",
"/callcenter/mainframe/image/processing_bar_2.gif",
"/callcenter/mainframe/image/logo_01.jpg" };
String localfilepath[] = { "C:\\FTP_Test\\index.jsp",
"C:\\FTP_Test\\ip.txt", "C:\\FTP_Test\\processing_bar_2.gif",
"C:\\FTP_Test\\logo_01.jpg" };
download fu = new download();
fu.connectServer("172.16.1.66",22, "web_test", "123456","/callcenter");
for(int i=0;ifilepath.length;i++){
fu.download(filepath[i],localfilepath[i]);
}
//fu.upload();
//fu.download();
fu.closeConnect();
}
}
通过JDK自带的API实现
package com.cloudpower.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.;
/**
* Java自带的API对FTP的操作
* @Title:
* @author: 周玲斌
*/
public class Ftp {
/**
* 本地文件名
*/
private String localfilename;
/**
* 远程文件名
*/
private String remotefilename;
/**
* FTP客户端
*/
private FtpClient ftpClient;
/**
* 服务器连接
* @param ip 服务器IP
* @param port 服务器端口
* @param user 用户名
* @param password 密码
* @param path 服务器路径
* @author 周玲斌
* @date 2012-7-11
*/
public void connectServer(String ip, int port, String user,
String password, String path) {
try {
/* ******连接服务器的两种方法*******/
//第一种方法
// ftpClient = new FtpClient();
// ftpClient.openServer(ip, port);
//第二种方法
ftpClient = new FtpClient(ip);
ftpClient.login(user, password);
// 设置成2进制传输
ftpClient.binary();
System.out.println("login success!");
if (path.length() != 0){
//把远程系统上的目录切换到参数path所指定的目录
ftpClient.cd(path);
}
ftpClient.binary();
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/**
* 关闭连接
* @author 周玲斌
* @date 2012-7-11
*/
public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/**
* 上传文件
* @param localFile 本地文件
* @param remoteFile 远程文件
* @author 周玲斌
* @date 2012-7-11
*/
public void upload(String localFile, String remoteFile) {
this.localfilename = localFile;
this.remotefilename = remoteFile;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
//将远程文件加入输出流中
os = ftpClient.put(this.remotefilename);
//获取本地文件的输入流
File file_in = new File(this.localfilename);
is = new FileInputStream(file_in);
//创建一个缓冲区
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
} catch (IOException ex) {
System.out.println("not upload");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 下载文件
* @param remoteFile 远程文件路径(服务器端)
* @param localFile 本地文件路径(客户端)
* @author 周玲斌
* @date 2012-7-11
*/
public void download(String remoteFile, String localFile) {
TelnetInputStream is = null;
FileOutputStream os = null;
try {
//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is = ftpClient.get(remoteFile);
File file_in = new File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("download success");
} catch (IOException ex) {
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String agrs[]) {
String filepath[] = { "/temp/aa.txt", "/temp/regist.log"};
String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"};
Ftp fu = new Ftp();
/*
* 使用默认的端口号、用户名、密码以及根目录连接FTP服务器
*/
fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp");
//下载
for (int i = 0; i filepath.length; i++) {
fu.download(filepath[i], localfilepath[i]);
}
String localfile = "E:\\号码.txt";
String remotefile = "/temp/哈哈.txt";
//上传
fu.upload(localfile, remotefile);
fu.closeConnect();
}
}