我想用Apache Commons Net实现一个FTP客户端,仅用于上传数据.连接和登录FTP服务器工作正常.但上传不能正常工作.文件与原件有点大.并且文件已损坏.我尝试了一个图像,一个视频和一个文本文件.只有文本文件没问题.
现在我看到调试时
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
Run Code Online (Sandbox Code Playgroud)
给了我false.所以它无法设置.为什么?(也许这不是问题?)
这是我的其余代码
client=new FTPClient();
try {
int reply;
client.connect(url, port);
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.login(user, pw);
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(300);
client.enterLocalPassiveMode();
if (client.isConnected())
{
try {
File file=new File(<FILE>);
FileInputStream inputStream = new FileInputStream(file);
OutputStream outputStream = client.storeFileStream(file.getName());
byte[] buffer = new byte[4096];
int l;
while((l = inputStream.read(buffer))!=-1)
{
outputStream.write(buffer, 0, l);
}
inputStream.close();
outputStream.flush();
outputStream.close();}
Run Code Online (Sandbox Code Playgroud) /*我在localhost上运行一个FTP服务器.当我下载文件时使用ftpClient.retrieveFile()方法,它的replyCode是550.我读了commons-net的API并找到了550 replyCode,定义是"public static final int FILE_UNAVAILABLE 550".但我无法从我的代码中找到问题.
谢谢你的帮助.
*/
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
try {
ftpClient.connect("192.168.1.102",2121);
ftpClient.login("myusername", "12345678");
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
String remoteFileName = "ftpserver.zip";//this file in the rootdir
fos = new FileOutputStream("f:/down.zip");
ftpClient.setBufferSize(1024);
ftpClient.enterLocalPassiveMode();
ftpClient.enterLocalActiveMode();
ftpClient.retrieveFile(remoteFileName, fos);
System.out.println("retrieveFile?"+ftpClient.getReplyCode());
fos.close();
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("??FTP??", e);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常小的Java程序,它能够使用apache-commons-net库执行FTP over SSL(不是SFTP)或FTPS.我编写此程序的原因是客户端机器是AIX 5.3,它不支持FTP over SSL(OOTB),FTP主机运行FileZilla服务器,只启用FTP over SSL.该程序运行良好,没有任何问题,但它生成的日志记录量很大.我的问题是 - 有没有办法控制日志记录的数量?
(再次注意 - 该程序对我的极简主义要求非常好)
以下是我的代码中的代码段
import java.io.*;
import java.text.MessageFormat;
import java.util.logging.Logger;
import org.apache.commons.
.....
....
....
try {
int reply;
logger.info("# Invoking Trust Manager");
client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
//client.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
logger.info("# Connect Call");
client.connect(server, port);
client.login(username, password);
logger.info("# Login Success");
client.setFileType(FTP.ASCII_FILE_TYPE);
client.execPBSZ(0); // Set protection buffer size
client.execPROT("P"); // Set data channel protection to private
client.enterLocalPassiveMode();
logger.info(MessageFormat.format("Connected to {0} .", server));
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
logger.severe("FTP server refused …Run Code Online (Sandbox Code Playgroud) 我正在使用Apache FTPClient获取文件和子目录文件列表。但是它无法从目录名称中获取带有空格的文件列表。这是一个示例-我尝试了两个不同的目录:
FTPClient client = new org.apache.commons.net.ftp.FTPClient();
client.connect("ftp.domain.com");
client.login("userid", "password");
FTPFile[] names = client.listDirectories("ABC XYZ"); //Empty array
FTPFile[] names2 = client.listDirectories("ABCXYZ"); //working
Run Code Online (Sandbox Code Playgroud)
因此带有空格的目录名称不返回任何内容。我试图将“%20”和“ +”放在空格处。我也尝试过“ \” ABC XYZ \”。但是仍然无法正常工作。我错过了什么吗?
我正在编写一个连接到 FTP 服务器以读取一些文件的 Rest 服务,然后对读取的数据进行一些操作以服务于服务请求。我正在使用 Apache 公共资源FTPClient。
作为临时解决方案,我正在创建一个FTPClient对象 - 然后连接它 - 然后使用凭据登录 -FTPClient在我的数据访问层中的一个方法(客户端是该方法的本地 - 这样做不是线程安全的),然后在退出方法之前(即......读取文件后)断开它。问题是,FTPClient登录需要 3-7 秒,这是非常高的。所以我正在考虑实现一个FTPClientPool可以在数据访问方法中提供一个已经准备好的客户端。
是否已经存在任何这样的 ClientPools?
如果是,那么我应该选择哪一种?
如果不是,实现的难点是一旦创建和连接, apache FTPClient 存活多久?无限时间??(我的意思是 FTPClient 的默认保持活动时间是多少 - 客户端断开连接后的空闲时间 - 因为我在 java 文档中看到了各种时间。:()接下来的问题是你如何让它始终保持活动状态??(可能会在一个单独的线程中定期发送 NOOPS ??)关于我应该如何前进的任何帮助都非常有帮助。
感谢和问候
java multithreading connection-pooling ftp-client apache-commons-net
我想在commons-net中创建一个包罗万象的ip范围,但是当我尝试时
SubnetUtils subnetUtils = new SubnetUtils("0.0.0.0", "0.0.0.0");
Run Code Online (Sandbox Code Playgroud)
或相同:
SubnetUtils subnetUtils = new SubnetUtils("0.0.0.0/0");
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
java.lang.IllegalArgumentException: Value [0] not in range (0,32]
at org.apache.commons.net.util.SubnetUtils.rangeCheck(SubnetUtils.java:304)
at org.apache.commons.net.util.SubnetUtils.calculate(SubnetUtils.java:229)
at org.apache.commons.net.util.SubnetUtils.<init>(SubnetUtils.java:63)
Run Code Online (Sandbox Code Playgroud)
我看到已经有了这张票:https://issues.apache.org/jira/browse/NET-511.他们说,这个问题在下一个(3.4)版本中得到了解决.
当commons-net 3.4发布时,是否有任何解决方法(如SubnetUtils对象列表)一起允许每个IPv4地址?
这是我必须将文件上传到 FTP 服务器的一些代码:
private boolean ftpSend(byte[] fileBytes, String filename, HashMap<String, String> endpointAddressData) throws Exception {
boolean success = false;
String login = endpointAddressData.get("user");
String hostname = endpointAddressData.get("host");
String password = endpointAddressData.get("password");
String portString = endpointAddressData.get("port");
int port = (portString == null) ? 21 : Integer.parseInt(portString);
FTPClient ftpClient = new FTPClient();
ByteArrayInputStream inputStream = null;
try {
ftpClient.setConnectTimeout(10000);
ftpClient.connect(hostname, port);
int reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
throw new Exception("FTP server " + hostname + " refused connection.");
}
if (password …Run Code Online (Sandbox Code Playgroud) 使用 Apache Commons-NetFTPSClient连接到现代 FTP/S 服务器不起作用。原因是它们需要 SSL 会话重用,即来自控制连接的 SSL 会话需要重新用于数据连接。
这通常可以在服务器中停用,但那是
正确的解决方案是让客户端实际重用会话。Commons-Net有一个开放的错误,但它看起来不会很快得到解决。
此外,还有一个由 Cyberduck(一个 FTP 客户端应用程序)的作者创建的“反射黑客”,在他们的错误跟踪器中进行了描述,更深入地在博客文章中进行了描述。StackOverflow 上还有一篇相关的文章描述了这个解决方案。他们使用反射来访问 JDK 的内部缓存SSLSessionContext并注入一个新条目。
在 JDK 8u161 和 9.0.4 (?) 之前,此 hack 一直运行良好,其中引入了一些更改日志中描述的 SSL更改。显然,一些实现细节发生了变化,导致黑客不再起作用。
据我所知,现在有以下选项:
SSLSessionContext实现的更改进行逆向工程以找到新的解决方法。这不仅看起来像是一项不平凡的任务 - 解决方案可能会再次变得笨拙,因此随时可能再次崩溃。任何人都可以建议如何在这里进行?
相关链接:
基本上我需要从 FTP 服务器下载匹配文件列表以进行搜索。我有从 FTP 服务器下载特定文件的代码。但是我需要使用通配符搜索下载所有匹配的文件。这在Java中怎么可能?
这是从 FTP 服务器下载特定文件名的文件的代码 -
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDownloadFileDemowithoutmodandfilefilter {
public static void main(String[] args) {
String server = "test.rebex.net";
int port = 21;
String user = "demo";
String pass = "password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File localFile = new File("C:\\project\\readme1.txt");
FTPFile remoteFile = ftpClient.mdtmFile("/readme.txt");
if (remoteFile != …Run Code Online (Sandbox Code Playgroud) 我正在使用apache的commons-net FTPClient来上传文件.
我正在使用storeFileStream方法.
这适用于第一次调用但在第二次调用时返回null并.getReplyStrings()返回"200 PORT命令成功"!
我的代码是(在每个文件的循环中称为方法):
FileInputStream fis = null;
File LF=new File(localFilePath);
InputStream is = new FileInputStream(LF);
for(String DP:(remoteBasepath+"/"+remoteFilePath).split("/")){
if(!client.changeWorkingDirectory(DP)){
client.makeDirectory(DP);
client.changeWorkingDirectory(DP);
}
}
for(String line:client.getReplyStrings()){
System.out.println(line);
}
OutputStream os = client.storeFileStream(LF.getName());
byte[] buffer = new byte[1024];
int len;
System.out.println("start");
long RBUN=0L;
for(String line:client.getReplyStrings()){
System.out.println(line);
}
while ((len = is.read(buffer)) != -1){
os.write(buffer, 0, len);
os.flush();
RBUN+=len;
CFPRGS.setValue(Math.round((RBUN*100/LF.length())));
}
for(String line:client.getReplyStrings()){
System.out.println(line);
}
is.close();
os.close();
Run Code Online (Sandbox Code Playgroud)
问题是什么?