Java在FTP上建立文件 .

Java在FTP上创建文件

public boolean createDirectory() {
		boolean flag = false;
		
		// 创建FTP客户端
		FTPClient ftpClient = new FTPClient();
		
		try {
			// 建立FTP连接
			ftpClient.connect(this.ftpServerIP);
			// 如果登录成功
			if (ftpClient.login(this.ftpName, this.ftpPassword)) {
				
				// 切换文件路径, 到FTP上的"xxx"文件夹下
				if (this.ftpPath != null && this.ftpPath.compareTo("") != 0
						&& ftpClient.changeWorkingDirectory(this.ftpPath)) {
					SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
					String time = f.format(new Date());
					
					String reTransmitFolderName = time + "_Folder";
					ftpClient.makeDirectory(reTransmitFolderName);
					flag = true;
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
			flag = false;
		} catch (IOException e) {
			e.printStackTrace();
			flag = false;
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		} finally {
			try {
				// 关闭连接
				ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return flag;
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java解压FTP服务器上的文件需要以下步骤: 第一步,建立FTP服务器的连接。可以使用Java中的org.apache.commons.net.ftp.FTPClient类来实现连接。首先创建一个FTPClient对象,然后使用connect()方法连接到FTP服务器,并使用login()方法进行登录验证。 第二步,切换到FTP服务器上的目标压缩文件所在的目录。可以使用changeWorkingDirectory()方法指定目录路径。 第三步,从FTP服务器上下载压缩文件到本地。使用retrieveFile()方法将压缩文件下载到本地。 第四步,关闭与FTP服务器的连接。 第五步,使用Java的压缩解压缩工具类,例如java.util.zip.ZipInputStream或java.util.zip.GZIPInputStream,来解压下载到本地的压缩文件。可以使用FileInputStream读取压缩文件的输入流,并将其传递给解压缩工具类的构造函数。 第六步,遍历解压缩后的文件,并进行相关操作。 以下是一个示例代码,展示如何使用Java解压FTP服务器上的文件: ```java import org.apache.commons.net.ftp.FTPClient; import java.io.*; public class FTPFileDecompression { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String password = "password"; String remoteFilePath = "/path/to/zipfile.zip"; String localFilePath = "path/to/save/file.zip"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, password); ftpClient.changeWorkingDirectory(remoteFilePath); File localFile = new File(localFilePath); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); ftpClient.retrieveFile(remoteFilePath, outputStream); outputStream.close(); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } // 解压缩文件 try { FileInputStream fileInputStream = new FileInputStream(localFile); ZipInputStream zipInputStream = new ZipInputStream(fileInputStream); ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { String entryName = zipEntry.getName(); File file = new File(entryName); // 对解压缩后的文件进行操作 // ... } zipInputStream.close(); fileInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码提供了一个基本框架,你需要根据实际的FTP服务器和文件路径进行修改和调整。同时,你可能还需要处理登录失败、目录不存在、文件不存在等异常情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值