问题描述
FTPClient.changeWorkingDirectory& changeToParentDirectory().如何在ftpCient中更改目录.在资源管理器中,ftp路径如下所示:ftp://192.168.10.20
.输入此内容后,我可以看到文件夹COMPLETED, FAILURE and QRCODEGENERATE
.首先在我的Java代码中,进入FAILURE
文件夹并获取相应的文件.并且此过程已成功完成.得到它之后,我想进入COMPLETED
文件夹,我的意思是目录路径必须像这样从/FAILURE
更改为COMPLETED
,但是它出错了.我在底部共享了控制台错误.
FTPClient.changeWorkingDirectory & changeToParentDirectory(). How to change directory in ftpCient. In explorer the ftp path looks like this ftp://192.168.10.20
. once entering this, I can see folders COMPLETED, FAILURE and QRCODEGENERATE
. In my java code first I'm entering inside FAILURE
folder and getting the respective file. And this process is successfully done. After getting it I want to enter into COMPLETED
folder, I mean the directory path has to be changed like this /FAILURE
to COMPLETED
but it's getting error. I shared the Console error at the bottom.
如果我关闭连接并再次打开,则可以获取该值.但是我不想那样做.我想在不关闭连接的情况下更改目录.
I can get the value if I close the connection and open again. But I dont want to do like that. I want to change the directory with out closing the connection.
1.首先,我进入FAILURE
,然后访问文件及其内容.
1.First I'm getting into FAILURE
and I'm accessing a file and its contents.
2.然后我要回到COMPLETED
,我想访问文件及其内容.
2.Then I'm going back to COMPLETED
and I want to access the files and its contents.
这是我的JAVA代码
InputStream is = null;
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
strDir= "FAILURE";
ftpClient.printWorkingDirectory();
String[] ftpList=ftpClient.listNames();
//Here I'm listing the folders present in parent directory
for(int i =0; i<ftpList.length;i++)
{
String ftpFilsList=ftpList[i];
System.out.println(".........::::::::::"+ftpFilsList);
}
//getting into Failure Folder and fetching file
ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory()+strDir);
System.out.println("prnt#WorkingDir"+ftpClient.printWorkingDirectory());
is =ftpClient.retrieveFileStream(filename+".json");
int i=0;
String valuesinside="";
while((i = is.read())!=-1)
{
//valuesinside+=valuesinside+i;
char c = (char)i;
valuesinside+=Character.toString(c);
ftpfilelist=valuesinside;
}
//Successfully Fetched file and its contents
//change the directory to COMPLETED Folder
String strFullFileName = "";
File fileJson=null;
int intfilecount=00;
String strFileCount="";
strDir = "COMPLETED";
ftpClient.changeToParentDirectory();//To go back to previos directory
System.out.println("PARENRT+++++:::::"+ftpClient.printWorkingDirectory());//This shows null
ftpClient.changeWorkingDirectory(strDir);
ftpClient.printWorkingDirectory();
System.out.println("COMPLETED+++++:::::"+ftpClient.printWorkingDirectory());//Shows "/COMPLETED" in the console. but not getting the files.
FTPFile[] fileJsonReader=ftpClient.listFiles(ftpClient.printWorkingDirectory());
for(FTPFile ftpFile:fileJsonReader)
{
String strJsonFileName=null;
strJsonFileName=ftpFile.getName();
System.out.println("strJsonFileName:::+++++"+strJsonFileName);
String strBillNoFile=strJsonFileName.substring(0, strJsonFileName.lastIndexOf("-"));//-01
System.out.println("strBillNoFile+++++::::::"+strBillNoFile);
strBillNoFile=strBillNoFile+".json";
System.out.println("strBillNoFile:::::+++++"+strBillNoFile);
if(strBillNoFile.equalsIgnoreCase(filename.getName()))
{
intfilecount=intfilecount+1;
}
}
在控制台中,我收到这样的错误.您可以先看到显示的文件夹列表.
In console I'm getting error like this. You can see the list of folders shown first.
.....................:::::::::::::::COMPLETED
.......................:::::::::::::::FAILURE
.......................:::::::::::::::QRCODEGENERATE
prnt#WorkingDir/FAILURE
PARENT+++++:::::::null
COMPLETED+++++:::::/COMPLETED
org.apache.commons.net.ftp.parser.ParserInitializationException: Unknown parser type: "/COMPLETED" is current directory.
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:132)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2263)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2046)
at com.restService.FtpJson.FTPAuthJSONReader(FtpJson.java:130)
at com.restService.JsonFailCompleted.getMessage(JsonFailCompleted.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:209)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:244)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:610)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:503)
at java.lang.Thread.run(Unknown Source)
请帮助我解决这个问题.预先感谢
please help me to solve this. Thanks in advance
-----只需关闭inputStream is.close()
和Boolean result = ftpClient.completePendingCommand();
-----Just closed the inputStream is.close()
and Boolean result = ftpClient.completePendingCommand();
is =ftpClient.retrieveFileStream(filename+".json");
,如果文件夹没有所请求的文件名,则抛出NullPointerException.如何在不更改retrieveFileStream
的情况下进行处理?
is =ftpClient.retrieveFileStream(filename+".json");
here if the folder doesn't have the requested file name, it throws NullPointerException. How to handle it without changing retrieveFileStream
?
推荐答案
更新:
问题似乎是由于使用此方法retrieveFileStream();
引起的,该方法需要一些额外的过程,并且一旦完成,服务器要么断开连接,要么表现异常.以下是 api参考
Update:
It looks like the issue is caused because you use this method retrieveFileStream();
, which needs some extra processes and the server either disconnects or behaves oddly once it is finished. Here is the key part information from the api referance:
要完成文件传输,您必须调用completePendingCommand ,然后检查其返回值以验证是否成功.如果不这样做,后续命令的行为可能会异常.
To finalize the file transfer you must call completePendingCommand andcheck its return value to verify success. If this is not done,subsequent commands may behave unexpectedly.
因此,您需要关闭蒸汽is.close();
,然后使用ftpClient.completePendingCommand();
.完成此操作后,您可以检查连接是否仍处于活动状态if(ftp.isConnected() == true){...}
,否则,可以重新连接ftp.connect(...);
,然后移至下一个命令来更改工作目录.
So you need to close the steam is.close();
then use ftpClient.completePendingCommand();
. Once this is done, you can check if the connection is still alive if(ftp.isConnected() == true){...}
and if not then you can reconnect ftp.connect(...);
then move onto your next command to change the working directory.
//Open the stream
is =ftpClient.retrieveFileStream(filename+".json");
//Process the stream data
int i=0;
String valuesinside="";
while((i = is.read())!=-1)
{
//valuesinside+=valuesinside+i;
char c = (char)i;
valuesinside+=Character.toString(c);
ftpfilelist=valuesinside;
}
//Close the stream and complete the command
is.close();
Boolean result = ftpClient.completePendingCommand();
//check if connection is alive
if(ftp.isConnected() == false){
//reconnect here with your server details
//...
}
//Now move onto the next commands
ftpClient.changeToParentDirectory();/
ftpClient.changeWorkingDirectory("COMPLETED");
System.out.println("PARENRT+++++:::::"+ftpClient.printWorkingDirectory());
或者,您可以使用其他方法手动管理流而不是使用API,例如,可以执行retrieveFile(fileName, outputStream);
.
Alternatly you could use a different method where you manage the stream manually rather than using the API, for example you could do retrieveFile(fileName, outputStream);
.
如果您对发生错误的其他原因有更多的兴趣,请参阅apache FTPClient文档:
If you are interested more about other reasons why the error happens then see here at the apache FTPClient docs:
这篇关于如何在FTPClient中更改目录.单个FTPClient InputStream中的多个目录连接.未知的解析器类型:"/Path"是当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!