ftp实现文件上传(下载)

来源:互联网 发布:枣想核你在一起 淘宝 编辑:程序博客网 时间:2024/06/05 19:19

例子代码

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



0 0
原创粉丝点击