gpt4 book ai didi

java - 如何更改 FTPClient 中的目录。单个 FTPClient InputStream 中的多个目录连接。未知的解析器类型 : "/Path" is current directory

转载 作者:行者123 更新时间:2023-12-04 10:51:01 27 4
gpt4 key购买 nike

FTPClient.changeWorkingDirectory & changeToParentDirectory()。如何更改 ftpCient 中的目录。在资源管理器中,ftp 路径如下所示 ftp://192.168.10.20 .一旦进入,我可以看到文件夹COMPLETED, FAILURE and QRCODEGENERATE .在我的 java 代码中,我首先进入 FAILURE文件夹并获取相应的文件。而这个过程就顺利完成了。拿到后想进入COMPLETED文件夹,我的意思是目录路径必须像这样更改 /FAILURECOMPLETED但它越来越错误。我在底部分享了控制台错误。
如果我关闭连接并再次打开,我可以获得该值。但我不想做这样的。我想在不关闭连接的情况下更改目录。
1.首先我要进入FAILURE我正在访问一个文件及其内容。
2.那我回COMPLETED我想访问文件及其内容。
这是我的 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;
}
}
在控制台我得到的错误是这样的。您可以看到首先显示的文件夹列表。
.....................:::::::::::::::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)
请帮我解决这个问题。提前致谢
解决方案
-----刚刚关闭了inputStream is.close()Boolean result = ftpClient.completePendingCommand();在 InputStream 中抛出 nullPointerException is =ftpClient.retrieveFileStream(filename+".json");这里如果文件夹没有请求的文件名,它会抛出 NullPointerException。不改如何处理 retrieveFileStream ?

最佳答案

更新:
看起来问题是由于您使用此方法引起的retrieveFileStream(); ,这需要一些额外的进程,一旦完成,服务器要么断开连接,要么表现异常。以下是来自 api referance 的关键部件信息:

retrieveFileStream - Returns an InputStream from which a named file fromthe server can be read. If the current file type is ASCII, thereturned InputStream will convert line separators in the file to thelocal representation. You must close the InputStream when you finishreading from it. The InputStream itself will take care of closing theparent data connection socket upon being closed.

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.


所以你需要关闭 Steam is.close();然后使用 ftpClient.completePendingCommand(); .完成后,您可以检查连接是否仍然有效 if(ftp.isConnected() == true){...}如果没有,那么您可以重新连接 ftp.connect(...);然后转到下一个命令以更改工作目录。
//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); .
ParserInitializationException 错误:
如果您对发生错误的其他原因更感兴趣,请参阅 apache FTPClient docs :

ParserInitializationException - Thrown if the parserKey parametercannot be resolved by the selected parser factory. In theDefaultFTPEntryParserFactory, this will happen when parserKey isneither the fully qualified class name of a class implementing theinterface org.apache.commons.net.ftp.FTPFileEntryParser nor a stringcontaining one of the recognized keys mapping to such a parser or ifclass loader security issues prevent its being loaded.

关于java - 如何更改 FTPClient 中的目录。单个 FTPClient InputStream 中的多个目录连接。未知的解析器类型 : "/Path" is current directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59475577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com