FTPClient上传文件大小为0字节的问题顺利解决 一步到位

今天通过FTPClient上传图片时出现,虽然无错误出现但是上传到服务器端的图片大小为0。

之前的代码

public static boolean uploadFile(String host, int port, 
		String username, String password,String basePath,
        String filePath, String filename, InputStream input) {
		boolean result = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(host, port);// 连接FTP服务器
			// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
			ftp.login(username, password);// 登录
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return result;
			}
		
			//切换到上传目录
			if (!ftp.changeWorkingDirectory(basePath+filePath)) {
				//如果目录不存在创建目录
				String[] dirs = filePath.split("/");
				String tempPath = basePath;
				for (String dir : dirs) {
					if (null == dir || "".equals(dir)) continue;
					tempPath += "/" + dir;
					if (!ftp.changeWorkingDirectory(tempPath)) {
						if (!ftp.makeDirectory(tempPath)) {
							return result;
						} else {
							ftp.changeWorkingDirectory(tempPath);
						}
					}
				}
			}
			//设置上传文件的类型为二进制类型
			ftp.setFileType(FTP.BINARY_FILE_TYPE);
			//上传文件
			if (!ftp.storeFile(filename, input)) {
				return result;
			}
			input.close();
			ftp.logout();
			result = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return result;
}

打上断点debug,当运行到 

if (!ftp.storeFile(filename, input)) {
    return result;
}

return fasle。也就是文件上传没有成功。 经过查询资料,发现FTPClient存在着两种模式,主动模式和被动模式。

 FTP支持两种模式,一种方式叫做Standard主动方式,缺省时默认的方式,一种是 Passive 被动方式。 

下面介绍一个这两种方式的工作原理:

        主动模式:第一步FTP客户端首先随机选择一个大于1024的端口p1,并通过此端口发送请求连接到FTP服务器的21号端口建立TCP连接,在FTP中这个连接叫做控制连接,连接成功建立后,FTP客户端会发送port命令,紧接着FTP客户端会监视自己的p1+1端口,FTP服务器接收到port命令会从自己的20号端口向FTP客户端的p1+1端口发起请求建立TCP连接,这个连接叫做数据连接,用来发送数据,数据传输完毕后数据连接随即关闭,控制连接保持开启。

  被动模式:在建立控制连接的时候和主动模式类似,但建立连接后发送的不是Port命令,而是Pasv命令。FTP服务器收到Pasv命令后,随机打开一个临时端口(也叫自由端口,端口号大于1023小于65535)并且通知客户端在这个端口上传送数据的请求,FTP客户端发送请求连接FTP服务器此端口,成功建立连接后FTP服务器将通过这个端口进行数据的传送数据传输完毕后数据连接随即关闭,控制连接保持开启。

       因为很多防火墙在设置的时候都是不允许接受外部发起的连接的,所以许多位于防火墙后或内网后的FTP客户端不支持主动模式,因为服务器无法穿过防火墙或者无法连接到NAT后的客户端。

至此,找到了原因:我是用的本机上开启了防火墙,FTP服务器请求本机的端口被隔离阻止。

解决方案:将ftp模式修改为Passive模式。加上代码

ftp.enterLocalPassiveMode();

现在代码为:

public static boolean uploadFile(String host, int port, 
		String username, String password,String basePath,
        String filePath, String filename, InputStream input) {
		boolean result = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(host, port);// 连接FTP服务器
			// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
			ftp.login(username, password);// 登录
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return result;
			}
			//将客户端设置为被动模式
			ftp.enterLocalPassiveMode();
			//切换到上传目录
			if (!ftp.changeWorkingDirectory(basePath+filePath)) {
				//如果目录不存在创建目录
				String[] dirs = filePath.split("/");
				String tempPath = basePath;
				for (String dir : dirs) {
					if (null == dir || "".equals(dir)) continue;
					tempPath += "/" + dir;
					if (!ftp.changeWorkingDirectory(tempPath)) {
						if (!ftp.makeDirectory(tempPath)) {
							return result;
						} else {
							ftp.changeWorkingDirectory(tempPath);
						}
					}
				}
			}
			//设置上传文件的类型为二进制类型
			ftp.setFileType(FTP.BINARY_FILE_TYPE);
			//上传文件
			if (!ftp.storeFile(filename, input)) {
				return result;
			}
			input.close();
			ftp.logout();
			result = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return result;
}

问题解决。

 

 

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
以下是使用 Apache Commons Net 库的 Java FtpClient 上传文件的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; public class FtpClientDemo { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String password = "password"; String remotePath = "/remote/path/"; String localFilePath = "/local/path/file.txt"; FTPClient ftpClient = new FTPClient(); try { // 连接 FTP 服务器 ftpClient.connect(server, port); ftpClient.login(user, password); // 设置文件传输模式为二进制 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 切换到指定的远程目录 ftpClient.changeWorkingDirectory(remotePath); // 上传文件 File localFile = new File(localFilePath); FileInputStream inputStream = new FileInputStream(localFile); boolean success = ftpClient.storeFile(localFile.getName(), inputStream); inputStream.close(); if (success) { System.out.println("文件上传成功!"); } // 登出 FTP 服务器 ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 断开连接 ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 在示例代码中,首先使用 `FTPClient` 类连接 FTP 服务器,然后设置传输模式为二进制,切换到指定的远程目录,并上传指定的本地文件。最后,记得登出 FTP 服务器并断开连接。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值