java 实现ftp上传如何创建文件夹?

Python013

java 实现ftp上传如何创建文件夹?,第1张

准备条件: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()

}

}

通过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.ftp.FtpClient

/**

* Java自带的API对FTP的操作

* @Title:Ftp.java

* @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 = 0i <filepath.lengthi++) {

fu.download(filepath[i], localfilepath[i])

}

String localfile = "E:\\号码.txt"

String remotefile = "/temp/哈哈.txt"

//上传

fu.upload(localfile, remotefile)

fu.closeConnect()

}

}

1.使用的FileZillaServer开源,安装过后建立的本地FTP服务器。2.使用的apache上FTP工具包,引用到工程目录中。3.IDE,Eclipse,JDK6上传和目录的实现原理:对每一个层级的目录进行判断,是为目录类型、还是文件类型。如果为目录类型,采用递归调用方法,检查到最底层的目录为止结束。如果为文件类型,则调用上传或者方法对文件进行上传或者操作。贴出代码:(其中有些没有代码,可以看看,还是很有用处的)!