例子代码
package getUrlPic; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply; public class FtpUploadFile {
public static void main(String[] args){
// public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
// boolean success = false;
FTPClient ftp = new FTPClient();
InputStream input = null;
try {
int reply;
ftp.connect("localhost", 21);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login("test", "test");//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("can not connect");
// return success;
}else{
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftp.changeWorkingDirectory(path);
input = new ByteArrayInputStream("中xuxxx".getBytes("utf-8"));
ftp.storeFile("test.txt", input); // 创建目录
ftp.makeDirectory("/test/bb"); //列出目录
FTPFile[] dirs = ftp.listDirectories("/test");
for(FTPFile f : dirs ){
System.out.println(f.getName());
}
}
// ftp.changeWorkingDirectory(path);
// ftp.storeFile(filename, input); // input.close();
// ftp.logout();
// success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if(input != null){
try{
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
// return success;
}
// }
}
参考
http://www.cnblogs.com/lucky_dai/p/6178076.html
http://www.jb51.net/article/86367.htm
http://blog.csdn.net/kardelpeng/article/details/6588284
https://zhidao.baidu.com/question/433380231.html
https://zhidao.baidu.com/question/1387264816675112740.html
http://www.jb51.net/article/86367.htm