Java基于FTPClient上传文件到FTP服务器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fjssharpsword/article/details/68066419

1、上传文件到FTP服务器,用FTPClient类,引入commons-net-3.1.jar包

2、参考代码:

//上传ftp
    public static boolean uploadFile(String filename) {
    	boolean success = false;
    	FTPClient ftp = new FTPClient();
    	try {
    		int reply;
    		ftp.connect("IP");//连接FTP服务器,默认端口
    		ftp.login("account", "pwd");//登录
    		reply = ftp.getReplyCode();
    		if (!FTPReply.isPositiveCompletion(reply)) {
    			ftp.disconnect();
    			return success;
    		}
    		ftp.changeWorkingDirectory("/");
    		FileInputStream in=new FileInputStream(new File("D://tmp//img//"+filename)); 
    		ftp.storeFile(filename, in);  
    		in.close();
    		ftp.logout();
    		success = true;
    	} catch (IOException e) {e.printStackTrace();} 
    	finally {
    		if (ftp.isConnected()) {
    			try {
    				ftp.disconnect();
    			} catch (IOException ioe) {}
    		}
    	}
    	return success;
    }


展开阅读全文

没有更多推荐了,返回首页