I’m using Camel 2.15.2 with Apache Commons Net 3.3 on Java 8 deployed into a Tomcat container.
The issue is that consistently after processing just over 200 files (> 4000 files in the directory) the route stops, the FTP client disconnects and the following message is logged out:
[ogs.sharp-stream.com:21/root/] FtpConsumer WARN Error processing file RemoteFile[route/to/file] due to File operation failed: Connection closed without indication.. Code: 421. Caused by: [org.apache.camel.component.file.GenericFileOperationFailedException - File operation failed: Connection closed without indication.. Code: 421]
org.apache.camel.component.file.GenericFileOperationFailedException: File operation failed: Connection closed without indication.. Code: 421
at org.apache.camel.component.file.remote.FtpOperations.getCurrentDirectory(FtpOperations.java:713)
at org.apache.camel.component.file.remote.FtpOperations.retrieveFileToFileInLocalWorkDirectory(FtpOperations.java:440)
at org.apache.camel.component.file.remote.FtpOperations.retrieveFile(FtpOperations.java:310)
at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:384)
at org.apache.camel.component.file.remote.RemoteFileConsumer.processExchange(RemoteFileConsumer.java:137)
at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:211)
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:175)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:187)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:114)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:582)
at org.apache.commons.net.ftp.FTP.pwd(FTP.java:1454)
at org.apache.commons.net.ftp.FTPClient.printWorkingDirectory(FTPClient.java:2658)
at org.apache.camel.component.file.remote.FtpOperations.getCurrentDirectory(FtpOperations.java:709)
... 15 more
This is the URI used at the begining of the related route.
As you can tell from the URI I’m also using a FileIdempotentRepository. It’s defined like this
<property name="fileStore" value="target/fileidempotent/.filestore1.dat" />
<property name="maxFileStoreSize" value="512000" />
<property name="cacheSize" value="250" />
</bean>
Any ideas why the connection might be closing before all files are processed?
Error message
FTP upload file report org apache. commons. net. ftp. Ftpconnectionclosedexception: connection closed without indication error
In the project, due to ftp migration, the ftpclient used by the code logs in successfully, but this error is reported when uploading the file.
code
public boolean uploadFile(String ip, int port, String username, String password, String serverpath, String file) { // Initial indicates upload failed boolean success = false; // Create FTPClient object FTPClient ftp = new FTPClient(); ftp.setControlEncoding("UTF-8"); ftp.setConnectTimeout(20000); ftp.setDataTimeout(600000); ftp.enterLocalPassiveMode(); ftp.setActivePortRange(4000, 4100); try { int reply=0; // Connect to FTP server // If you use the default port, you can use FTP Connect (IP) directly to the FTP server ftp.connect(ip, port); //ftp.connect("192.168.20.221", 21); // Login ftp ftp.login(username, password); // Check whether the returned value is reply > = 200 & & reply < 300. If yes, it indicates that the login is successful reply = ftp.getReplyCode(); logger.info("connect ftp Server response code, reply={}",reply); // The return value starting with 2 will be true if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.setActivePortRange(40000, 41000); logger.info("ftp Connection succeeded... file = {}, serverpath = {}", file, serverpath); checkPathExist(ftp,iso8859ToGbk(serverpath)); //Input stream InputStream input=null; try { file=gbkToIso8859(file); input = new FileInputStream(iso8859ToGbk(file)); } catch (Exception e) { LoggerFactory.getLogger(this.getClass()).error("Error reading uploaded file",e); } // Store the uploaded file in the specified directory file=iso8859ToGbk(file); String fileName = getFilename(file);//8859 int index = fileName.lastIndexOf("."); String tmpFileName = fileName.substring(0,index)+".tmp"; ftp.deleteFile(iso8859ToGbk(fileName)); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); String ftpPath = iso8859ToGbk(serverpath)+"/"+iso8859ToGbk(tmpFileName); logger.info("Before uploading ftp route, ftpPath = {}",ftpPath); boolean flag = ftp.storeFile(ftpPath, input); logger.info("After uploading the file, upload the results flag = {}", flag); // Close input stream input.close(); if(flag){ ftp.rename(iso8859ToGbk(serverpath)+"/"+iso8859ToGbk(tmpFileName), iso8859ToGbk(serverpath)+"/"+iso8859ToGbk(fileName)); success = true; } // Exit ftp ftp.logout(); } catch (IOException e) { success = false; LoggerFactory.getLogger(this.getClass()).error("Upload data to ftp error",e); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { LoggerFactory.getLogger(this.getClass()).info(ioe.toString()); } } } return success; }
Introduction to ftp active and passive mode
Active mode: the FTP client sends a connection request to the FTP control PORT (21 by default) of the server, and the server accepts the connection and establishes a command link; When it is necessary to transmit data, the client uses the PORT command on the command link to tell the server that I have opened a PORT, and you come to connect me. Therefore, the server sends a connection request from PORT 20 to the PORT of the client and establishes a data link to transmit data. In the process of data link establishment, the server actively requests, so it is called active mode.
Passive mode: the FTP client sends a connection request to the FTP control port (default 21) of the server, and the server accepts the connection and establishes a command link; When data needs to be transmitted, the server uses PASV command on the command link to tell the client that I have opened a port and you come to connect me. So the client sends a connection request to the port of the server and establishes a data link to transmit data. In the process of data link establishment, the server passively waits for the request of the client, so it is called passive mode.
Cause analysis
From the code above, we can see that after the ftp login is successful, ftp is executed setActivePortRange(40000, 41000);, This means that the active mode is used to transmit data. The active mode requires the ftp server to connect to our port. As a result, the ftp server cannot connect to the client port due to network problems, so this error is reported.
Solution
For this problem, there should be two solutions: first, open the network, which is the port where the ftp server can connect the client 40000 ~ 41000; 2, Is to change to passive mode.
Here I take the second method: after the ftp client logs in successfully (the execution before login is invalid), execute ftp enterLocalPassiveMode(); The final code is as follows:
public boolean uploadFile(String ip, int port, String username, String password, String serverpath, String file) { // Initial indicates upload failed boolean success = false; // Create FTPClient object FTPClient ftp = new FTPClient(); ftp.setControlEncoding("UTF-8"); ftp.setConnectTimeout(20000); ftp.setDataTimeout(600000); ftp.enterLocalPassiveMode(); ftp.setActivePortRange(4000, 4100); try { int reply=0; // Connect to FTP server // If you use the default port, you can use FTP Connect (IP) directly to the FTP server ftp.connect(ip, port); //ftp.connect("192.168.20.221", 21); // Login ftp ftp.login(username, password); // Check whether the returned value is reply > = 200 & & reply < 300. If yes, it indicates that the login is successful reply = ftp.getReplyCode(); logger.info("connect ftp Server response code, reply={}",reply); // The return value starting with 2 will be true if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.enterLocalPassiveMode();// This is changed to passive mode logger.info("ftp Connection succeeded... file = {}, serverpath = {}", file, serverpath); checkPathExist(ftp,iso8859ToGbk(serverpath)); //Input stream InputStream input=null; try { file=gbkToIso8859(file); input = new FileInputStream(iso8859ToGbk(file)); } catch (Exception e) { LoggerFactory.getLogger(this.getClass()).error("Error reading uploaded file",e); } // Store the uploaded file in the specified directory file=iso8859ToGbk(file); String fileName = getFilename(file);//8859 int index = fileName.lastIndexOf("."); String tmpFileName = fileName.substring(0,index)+".tmp"; ftp.deleteFile(iso8859ToGbk(fileName)); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); String ftpPath = iso8859ToGbk(serverpath)+"/"+iso8859ToGbk(tmpFileName); logger.info("Before uploading ftp route, ftpPath = {}",ftpPath); boolean flag = ftp.storeFile(ftpPath, input); logger.info("After uploading the file, upload the results flag = {}", flag); // Close input stream input.close(); if(flag){ ftp.rename(iso8859ToGbk(serverpath)+"/"+iso8859ToGbk(tmpFileName), iso8859ToGbk(serverpath)+"/"+iso8859ToGbk(fileName)); success = true; } // Exit ftp ftp.logout(); } catch (IOException e) { success = false; LoggerFactory.getLogger(this.getClass()).error("Upload data to ftp error",e); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { LoggerFactory.getLogger(this.getClass()).info(ioe.toString()); } } } return success; }
I hope it will be helpful to you.
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
Please follow the issue template and provide more details. issues like this do not provide any way of replicating the problem and spotting if it’s an Apache Commons FTP bug or an Android Upload Service bug. Thank you!
English is poor, please forgive me。
Using FTP to upload files to the window server was correct yesterday, but it went wrong today. The device system is android4.4.4
Sorry, but you have to follow what is in the issue template and provide necessary details, otherwise I couldn’t help you in any way
The same file, second pass will make mistakes ,and return «Error while uploading: xx to xx»
I don’t understand the code
try { String remoteFileName = getRemoteFileName(file); if (!ftpClient.storeFile(remoteFileName, localStream)) { throw new IOException("Error while uploading: " + file.getName(service) + " to: " + file.getProperty(PARAM_REMOTE_PATH)); } setPermission(remoteFileName, file.getProperty(PARAM_PERMISSIONS)); } finally { localStream.close(); }
The exception is thrown because the transfer of the file to your FTP directory is not possible. I don’t have the Apache Commons Net FTP docs at hand, but this may be due to permission policy for your FTP user or if the file already exists, in which case it doesn’t get overwritten.
Can you successfully upload the file for the first time? If I understand correctly, you get the exception only the second time you try to upload it.
Successfully upload the file for the first time.But,Some FTP servers can upload second times。
I’ll see if the FTP service is insufficient。
Example:
new FTPUploadRequest(mContext, _ip, _port)
.setUsernameAndPassword(_accout, _pwd)
.addFileToUpload(path, to)
.setNotificationConfig(config)
.setCreatedDirectoriesPermissions(new UnixPermissions(«777»))
.setSocketTimeout(301000)
.setConnectTimeout(301000)
Here it’s storeFile method JavaDoc. The method should overwrite the existing file the second time you try to make an upload.
The best way to check if the problem is in the library or in your FTP server config is to use one of the test FTP servers
You are right,this is FTP server Permission denied。
thanks
Below sample code is used with BPXBATCH to Connect to a MAINFRAME FTP Client.
When I run the below from Eclipse on my windows PC. its Successfull.
The issue comes when I try to execute the same with BPXBATCH on a Mainframe.
The code throws an FTPConnectionClosedException : Connection closed without indication
at client.connect(«XXX.XXX.XXX.XXX»);
FTPClient client = new FTPClient();
try {
client.connect("XXX.XXX.XXX.XXX");
client.login("user1", "pass123");
int reply ;
reply = client.getReplyCode();
System.out.println("Reply Code:"+reply);
if(client.isConnected())
client.logout();
else
System.out.println("Negative reply");
} catch(final Throwable t){
t.printStackTrace();
}
Эта ошибка возникает на файлы загрузки Linux, используя ftpclient
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.cwd(FTP.java:828)
at org.apache.commons.net.ftp.FTPClient.changeWorkingDirectory(FTPClient.java:1128)
at com.taotao.test.FTPTest.testFTP(FTPTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
В конце концов, путем изменения разрешений файла на сервере его можно успешно выполнить, и этот метод использовался для сохранения файла для открытия разрешений этого пользователя для выполнения (X), я напрямую устанавливаю (rwx)