准备条件: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()
}
}
import java.io.Fileimport java.io.FileInputStream
import java.io.IOException
import java.io.OutputStream
import java.io.UnsupportedEncodingException
import sun.net.ftp.FtpClient
/**
*
* @author chenc
* @version 1.0
*
* 2008-02-19
*
*/
public class Ftp {
private String ip = ""
private String username = ""
private String password = ""
private String ftpDir = ""
private String localFileFullName = ""// 待上传的文件全名
private String ftpFileName = ""// 文件上传到FTP后的名称
FtpClient ftpClient = null
OutputStream os = null
FileInputStream is = null
public Ftp(String serverIP, String username, String password, String ftpDir) {
this.ip = serverIP
this.username = username
this.password = password
if (ftpDir == null) {
this.ftpDir = "/ftpfileload"
} else {
try {
this.ftpDir = "/"
+ new String(ftpDir.getBytes("ISO-8859-1"), "GBK")
.toString()
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace()
}
}
}
private void createDir(String dir, FtpClient ftpClient) {
System.out.println(this.ftpDir)
ftpClient.sendServer("MKD " + dir + "\r\n")
try {
ftpClient.readServerResponse()
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace()
}
}
private Boolean isDirExist(String dir, FtpClient ftpClient) {
try {
ftpClient.cd(dir)
} catch (Exception e) {
// TODO 自动生成 catch 块
return false
}
return true
}
public String upload(String localFileFullName) {
// this.ftpFileName = "aaa.test"
// 获取文件后缀名
String ext = localFileFullName.substring(localFileFullName
.lastIndexOf("."))
// System.out.println(ext)
// 产生新文件名,用系统当前时间+文件原有后缀名
long newFileName = System.currentTimeMillis()
String newFileFullName = newFileName + ext
// System.out.println("new file name:"+newFileFullName)
this.ftpFileName = newFileFullName
try {
String savefilename = new String(localFileFullName
.getBytes("ISO-8859-1"), "GBK")
// 新建一个FTP客户端连接
ftpClient = new FtpClient()
ftpClient.openServer(this.ip)
ftpClient.login(this.username, this.password)
// 判断并创建目录
if (!isDirExist(this.ftpDir, ftpClient)) {
createDir(this.ftpDir, ftpClient)
}
ftpClient.cd(this.ftpDir)// 切换到FTP目录
ftpClient.binary()
os = ftpClient.put(this.ftpFileName)
// 打开本地待长传的文件
File file_in = new File(savefilename)
is = new FileInputStream(file_in)
byte[] bytes = new byte[1024]
// 开始复制
int c
// 暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c)
}
} catch (Exception e) {
e.printStackTrace()
System.err.println("Exception e in Ftp upload(): " + e.toString())
} finally {
try {
if (is != null) {
is.close()
}
if (os != null) {
os.close()
}
if (ftpClient != null) {
ftpClient.closeServer()
}
} catch (Exception e) {
System.err.println("Exception e in Ftp upload() finally"
+ e.toString())
}
}
return this.ftpFileName
}
public void delFile(String dir, String filename) {
ftpClient = new FtpClient()
try {
ftpClient.openServer(this.ip)
ftpClient.login(this.username, this.password)
if (dir.length() >0) {
ftpClient.cd(dir)
}
ftpClient.sendServer("DELE " + filename + "\r\n")
ftpClient.readServerResponse()
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace()
}
}
}
我写的一个FTP类,你看看你能不能用。。。。。
使用File类中方法就可以实现File[] listFiles() 返回目录下所有的文件
File file=new File("你的ftp的根路径")
File files[]=file.listFiles()
for(int i=0i<files.lengthi++){
System.out.println(files[i].getName() )
}
如果要获取所有的文件和文件夹可以使用String[] list()方法。返回的是String类型的数组,其中所有文件和文件夹的相对路径表示。
补充---
如果那样的话那么就需要在你的ftp服务器上做一个socket服务端,你通过一个客户端连接上去。然后服务器端将获取的文件列表数组传递给你,就可以了。如果想直接获取别人的机器的文件列表是很难的,基本上是不可能的,当然是出于安全的考虑
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)