准备条件: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
ftp.connect(addr)
System.out.println("连接到:" + addr + ":" + port)
System.out.print(ftp.getReplyString())
reply = ftp.getReplyCode()
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect()
System.err.println("FTP目标服务器积极拒绝.")
System.exit(1)
return false
}else{
ftp.login(username, password)
ftp.enterLocalPassiveMode()
ftp.setFileType(FTPClient.BINARY_FILE_TYPE)
ftp.changeWorkingDirectory(path)
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{
ftp.makeDirectory(dirname)
System.out.println("在目标服务器上成功建立了文件夹: " + dirname)
}catch(Exception ex){
System.out.println(ex.getMessage())
}
}
断开host连接
public void disconnect(){try {
ftp.disconnect()
} 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()
}
}
package com.quantongfu.ftp.ftpimport java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.net.ServerSocket
import java.util.List
import org.apache.commons.net.ftp.FTPReply
import org.apache.log4j.Logger
import org.apache.log4j.net.SocketServer
import com.quantongfu.conf.FtpConf
/**
* @项目名称: telinSyslog
* @文件名称: Ftp.java
* @创建日期:2015年9月14日 下午3:22:08
* @功能描述:ftp实体类,用于连接,上传
* @修订记录:
*/
public class Ftp {
private static Logger logger = Logger.getLogger(Ftp.class)
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
ftp.connect(FtpConf.FTP_HOST, FtpConf.FTP_PORT)
ftp.login(FtpConf.FTP_USER_NAME, FtpConf.FTP_PASSWORD)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE)
ftp.setDataTimeout(60000)
reply = ftp.getReplyCode()
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect()
return result
}
if (FtpConf.IS_FTP_DIRECTORY) {
ftp.changeWorkingDirectory(FtpConf.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 = ftp.storeFile(file.getName() + ".tmp", input)
if (b) {
b = ftp.rename(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 = ftp.storeFile(server, file.getName() + ".tmp", input)
if (b) {
b = ftp.rename(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) {
ftp.disconnect()
}
} catch (IOException e) {
e.printStackTrace()
}
}
/*获取连接*/
public static Ftp getFtp() {
Ftp ftp = new Ftp()
try {
ftp.connect()
} catch (Exception e) {
logger.error("FTP连接异常" + e.getMessage())
e.printStackTrace()
}
return ftp
}
/*重连*/
public Ftp reconnect() {
disConnect()
return getFtp()
}
}
使用Apache FtpClient jar包,获取jar : http://commons.apache.org/net/