使用FTP下载文件connect.retrieveFileStream(filename) 获取不到InputStream流,返回null的问题

本文介绍了解决FTP下载中文文件名乱码的问题。通过指定字符集转换,成功解决了因文件名乱码导致的下载失败问题,并强调了设置被动模式的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    使用同事的代码做FTP下载文件,InputStream in = connect.retrieveFileStream(fileName);执行这句时InputStream总是获取为空



  后来把代码改成ftp.retrieveFileStream(new String(dirPath[1].getBytes("UTF-8"), "ISO-8859-1"));加上字符集指定就好了,因为获取文件时有中文,出现乱码而获取不到。



        还有这句也不能少 ftp.enterLocalPassiveMode(); 这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据,但是在Linux上,由于安全限制,可能某些端口没有开启,所以就出现阻塞。


### Java FTP Upload File Example Code and Tutorial For automating tasks such as uploading files via FTP using Java, developers often rely on libraries that simplify network operations. One of the most popular choices is Apache Commons Net, which provides an easy-to-use API for various protocols including FTP. Below is a simple example demonstrating how to upload a file through FTP in Java: ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.FileInputStream; import java.io.IOException; public class FtpUploadExample { public static void uploadFile(String server, int port, String user, String pass, String filePath, String remotePath) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); showServerReply(ftpClient); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Operation failed. Server replied: " + replyCode); return; } // Login to the FTP server boolean success = ftpClient.login(user, pass); showServerReply(ftpClient); if (!success) { System.out.println("Could not login to the server"); return; } else { System.out.println("Logged into server"); // Set passive mode ftpClient.enterLocalPassiveMode(); // Change to desired directory ftpClient.changeWorkingDirectory(remotePath); // Set binary transfer type ftpClient.setFileType(FTP.BINARY_FILE_TYPE); FileInputStream inputStream = null; try { // Prepare input stream from local file inputStream = new FileInputStream(filePath); // Store file on server String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1); success = ftpClient.storeFile(fileName, inputStream); showServerReply(ftpClient); if (success) { System.out.println("The file was uploaded successfully."); } } finally { if (inputStream != null) { inputStream.close(); } } } } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { System.out.println("SERVER: " + aReply); } } } } ``` This program connects to an FTP server specified by `server` and `port`, logs in with credentials provided (`user` and `pass`), changes directories based on `remotePath`, sets up parameters suitable for transferring files efficiently over FTP connections, opens a connection to read data from a given path locally (`filePath`) before sending it across the internet until completion or failure occurs during any step along this process chain[^1]. To use this function effectively within applications where frequent updates occur without manual intervention becoming cumbersome—such scenarios were mentioned previously regarding tedium involved when making small code changes repeatedly—it would be beneficial integrating automated deployment scripts utilizing methods similar above shown here but tailored specifically towards project requirements ensuring smooth operation while handling concurrent modifications gracefully too[^2].
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值