Java FTP客户端上传示例– Apache Commons Net

Java FTP Client is used to upload files to FTP server. Recently I was working in a web project where I had to upload a lot of images to the FTP server. Few days back, I wrote a program to resize image in java. My actual program was to resize all the images in a directory and then upload to FTP server using Apache Commons Net API.

Java FTP Client用于将文件上传到FTP服务器。 最近,我在一个Web项目中工作,必须将大量图像上传到FTP服务器。 几天前,我写了一个程序来调整java中图像的大小 。 我的实际程序是调整目录中所有图像的大小,然后使用Apache Commons Net API上传到FTP服务器。

Java FTP客户端示例 (Java FTP Client Example)

Here I am providing a Java FTP client program to upload files to FTP server using Apache Commons Net API.

在这里,我提供了一个Java FTP客户端程序,可以使用Apache Commons Net API将文件上传到FTP服务器。

package com.journaldev.inheritance;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPUploader {
	
	FTPClient ftp = null;
	
	public FTPUploader(String host, String user, String pwd) throws Exception{
		ftp = new FTPClient();
		ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
		int reply;
		ftp.connect(host);
		reply = ftp.getReplyCode();
		if (!FTPReply.isPositiveCompletion(reply)) {
			ftp.disconnect();
			throw new Exception("Exception in connecting to FTP Server");
		}
		ftp.login(user, pwd);
		ftp.setFileType(FTP.BINARY_FILE_TYPE);
		ftp.enterLocalPassiveMode();
	}
	public void uploadFile(String localFileFullName, String fileName, String hostDir)
			throws Exception {
		try(InputStream input = new FileInputStream(new File(localFileFullName))){
		this.ftp.storeFile(hostDir + fileName, input);
		}
	}

	public void disconnect(){
		if (this.ftp.isConnected()) {
			try {
				this.ftp.logout();
				this.ftp.disconnect();
			} catch (IOException f) {
				// do nothing as file is already saved to server
			}
		}
	}
	public static void main(String[] args) throws Exception {
		System.out.println("Start");
		FTPUploader ftpUploader = new FTPUploader("ftp.journaldev.com", "ftpUser", "ftpPassword");
		//FTP server path is relative. So if FTP account HOME directory is "/home/pankaj/public_html/" and you need to upload 
		// files to "/home/pankaj/public_html/wp-content/uploads/image2/", you should pass directory parameter as "/wp-content/uploads/image2/"
		ftpUploader.uploadFile("D:\\Pankaj\\images\\MyImage.png", "image.png", "/wp-content/uploads/image2/");
		ftpUploader.disconnect();
		System.out.println("Done");
	}

}

You can use the above program to connect to an FTP server and then upload files to the server. Make sure you provide FTP Host, user, and password details correctly in the program. You can get these details when you create an FTP user.

您可以使用上述程序连接到FTP服务器,然后将文件上传到服务器。 确保在程序中正确提供了FTP主机,用户和密码的详细信息。 创建FTP用户时,您可以获得这些详细信息。

The other important point to note is the server directory location. It’s relative to the FTP user home directory. Also, once you are done with uploading all the files, close the connection to the server and release resources.

要注意的另一个重要点是服务器目录位置。 它是相对于FTP用户主目录的。 另外,完成所有文件的上传后,请关闭与服务器的连接并释放资源。

GitHub Repository. GitHub Repository中找到更多核心Java示例。

翻译自: https://www.journaldev.com/661/java-ftp-client-upload-example-apache-commons-net

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值