我只是想知道:在我的Java应用程序中,每次我需要连接到FTP服务器时,我应该创建一个新的FTPClient对象,还是应该创建一个FTPClient()对象并在每次连接到一个时重用它FTP服务器?
我想使用Apache的commons-net使用代理服务器建立FTP连接.
但是看看这个FTPClient是否支持通过FTP代理服务器的FTP连接?让我担心
我必须干涉系统属性,Sun文档声明"如果指定了socksProxyHost,那么所有TCP套接字都将使用SOCKS代理服务器建立连接或接受连接."
WTH?所有TCP套接字?我的数据库连接怎么样?或者其他FTP连接我可能想要在不使用代理的情况下同时打开?他们都会受到影响吗?
有没有其他方法可以做到这一点,不会弄乱我的应用程序的其余部分?
我正在尝试使用apache-commons net FTP lib从FTP服务器获取.如果目录中只有1个文件,代码工作正常,但是第二次调用retrieveFileStream()时总是返回null.有什么想法吗?我编写了以下示例代码来演示我的问题.
public static void main(String[] args) throws Exception
{
String strLine;
FTPClient client = null;
try{
client = new FTPClient();
client.connect("localhost", 21);
client.enterLocalPassiveMode();
client.login("ftptester", "letmein");
client.changeWorkingDirectory("remote");
FTPFile[] ftpFiles = client.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
InputStream fin = client.retrieveFileStream(filepath);
if (fin == null) {
System.out.println("could not retrieve file: " + filepath);
continue;
}
byte[] data = readBytes(fin); // helper method not shown, …Run Code Online (Sandbox Code Playgroud) 是否可以使用FTPClient(Apache commons-net)检查远程目录是否存在?
我想做这样的事情:
ftp.isDirectory(String path) //returns true, false
Run Code Online (Sandbox Code Playgroud)
然后获取目录的权限(chmod):
ftp.getPermisions(String path) //returns -rwxr-xr-x
Run Code Online (Sandbox Code Playgroud) 我正在尝试从Android手机(HTC Desire HD)上传图像到FTP服务器(在我的本地PC上).图像将进入FTP服务器,但它们已损坏.
并且方法(ftpClient.storeFile())抛出IOException(错误的文件号)
请帮我.
这是损坏的图像链接:
http://imageshack.us/photo/my-images/820/komikb.jpg/
这是代码:
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("192.168.2.14");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpClient.setSoTimeout(10000);
ftpClient.enterLocalPassiveMode();
if(ftpClient.login("Administrator", "xxxx"))
{
File sFile=new File("mnt/sdcard/DCIM/komik.jpg");
FileInputStream fs= new FileInputStream(sFile);
String fileName = sFile.getName();
Boolean result = ftpClient.storeFile("/ftpfile.atspace.co.uk/" + fileName, fs);
String has = "";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 在Eclipse中,以下导入...
import org.apache.commons.net.ftp.FTPClient;
Run Code Online (Sandbox Code Playgroud)
...产生以下错误消息......
The import org.apache cannot be resolved
Run Code Online (Sandbox Code Playgroud)
我不知道如何导入,也不知道在哪里下载导入(如果需要).我可能需要一组点击Eclipse方向来解决这个问题.
我有一条骆驼路线,就像永远被绞死一样,不确定它是Camel还是Ftp客户端问题.ftp组件的soTimeout设置为60000.感谢任何帮助.
组件版本:
camel-ftp:2.9.0
Commons Net:(2.2)
线程转储:
"Camel (some-ftp-route) thread #57 - ftp://user@ftphost/folder" Id=338 in RUNNABLE (running in native)
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
- locked java.io.InputStreamReader@6a3f933a
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
- locked java.io.InputStreamReader@6a3f933a
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:295)
at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:365)
at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:630)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:164)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:184)
at org.apache.camel.component.file.remote.FtpOperations.connect(FtpOperations.java:91)
at org.apache.camel.component.file.remote.RemoteFileConsumer.connectIfNecessary(RemoteFileConsumer.java:144)
at org.apache.camel.component.file.remote.RemoteFileConsumer.recoverableConnectIfNecessary(RemoteFileConsumer.java:123)
at org.apache.camel.component.file.remote.RemoteFileConsumer.prePollCheck(RemoteFileConsumer.java:56)
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:100)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:139)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:91)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:204)
at …Run Code Online (Sandbox Code Playgroud) 我尝试用库commons.net创建一个项目,通过ftp发送一些文件.但我创建了与我的服务器的连接我收到此错误.
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3
Run Code Online (Sandbox Code Playgroud)
我已经按照这篇文章创建了我的连接,并且通过官方示例我已经控制了文章.
我的java代码在这里:
private void connect(String host, String user, String pwd) {
try{
ftp = new FTPSClient(false);
//ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host,22);//error is here
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();
}catch(Exception ex){
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白我哪里出错了.
我有一个Android应用程序,列出了FTP目录的文件夹和文件.我使用 Apache Commons FTP Client.它归结为我在每个文件夹中调用的这一行
FTPFile[] folderElements = ftpClient.listFiles(folderName);
Run Code Online (Sandbox Code Playgroud)
工作正常,但非常耗时.整个任务(获取所有文件和文件夹)大约需要一分钟.我可以以某种方式加快速度吗?
我已经试过了
ftpClient.setBufferSize(1024000);
Run Code Online (Sandbox Code Playgroud) 我在我的Android应用程序中使用Apache Commons FTP库
我通过FTPS建立连接,虽然它与服务器完美连接,但在传输文件时遇到问题.
出于安全原因,订购应用程序的客户端在使用PROT P时请求在数据连接上恢复TLS会话.
因此,我在服务器上启用了此选项:
正如我所说,我可以连接到服务器,但不能传输文件. 如果我取消激活"使用PROT P时数据连接上所需的TLS会话恢复"框,则传输正常.
我正在寻找一种使用库进行文件传输的方法,但是没有成功,我知道必须有一种方法.
我给你相关代码的一部分:
TransferImagenesFTP.ftpClient = new FTPSClient();
TransferImagenesFTP.ftpClient.connect(InetAddress.getByName("XXX_XXX_XX_X"), 26);
TransferImagenesFTP.ftpClient.enterLocalPassiveMode();
TransferImagenesFTP.ftpClient.setBufferSize(1024000);
TransferImagenesFTP.ftpClient.login("xxxxxx", "zzzzzz");
TransferImagenesFTP.ftpClient.execPROT("P");
TransferImagenesFTP.ftpClient.type(FTP.BINARY_FILE_TYPE);
Run Code Online (Sandbox Code Playgroud)
我感谢任何帮助,谢谢.
java ×10
ftp ×6
ftp-client ×5
android ×4
ftps ×2
apache-camel ×1
eclipse ×1
import ×1
performance ×1
ssh ×1