gpt4 book ai didi

java - FTP 异常 501 "pathname"超过 8 个字符

转载 作者:行者123 更新时间:2023-12-02 00:11:56 30 4
gpt4 key购买 nike

我正在尝试使用 FTP 协议(protocol)通过 URI 访问文件。出于明显的安全原因,我必须做出一些更改,但这似乎就是问题的根源。

我的 URI 如下: ftp://user:pasword@host.net/u/Bigpathname/XYZ/ABC/BigPathname/bigpathname/xyz/abc/MY_LOG.LOG

我看到了这个异常:

sun.net.ftp.FtpProtocolException: CWD Bigpathname:501 A qualifier in "Bigpathname" is more than 8 characters

This is really confusing as I can access the file from a Windows 7 command line with the CD command just fine. Both one directory at a time and as a full path.

I found one article mentioning that MVS file names must be 8 or fewer characters but this does not explain how I can get to these same files from my command line! They do exist there is data there that I can download manual but I can not get there via a URI in Java.

PS I use .toURL().openStream() to get files on my local machine just fine, it only fails when I try to get them from my server.

EDIT October 1st

I am able to access files on the MVS host using FileZilla and the basic FTP client from the Windows 7 command line - but I still cannot get them from a URI/URL. I downloaded a very basic Java built FTP client and tried accessing the same file in my program from there and the path works but because my file name has a dot in it "MY_LOG.LOG" I am getting File does not exist 501 Invalid data set name "MY_LOG.LOG". Use MVS Dsname conventions. I am utterly perplexed by this...

EDIT Ocotober 1st afternoon :)

OK I finally got it to work with a FTP client in my Java code - but I still want to use the URL class as I have logs on both local and remote machines. Is there a way to encode a URL string so that it can retrieve a file from a remote machine with the FTP protocol? I am not sure how it works in the Java URL class but in the FTP client I had to use the CWD and then the RETR command.

If I can do this then I have one solution for getting all my logs, otherwise I will have to detect if it is a file or ftp URL and then behave differently. Not the end of the world but not what I want...

The code that tries to get the file with just a URL is as follows: (sysc is a valid host)

void testFTP()
{
String ftp = "ftp://user:pword@sysc/u/Xxxxxxxxxx/ICS/YT7/XxxxxXxxxxxxx/xxxxxxxxx/logs/xxxxxxxx/XX_YT.LOG";

try
{
URI uri = new URI(ftp);
URL ftpFile = uri.toURL();

BufferedReader in = new BufferedReader(new InputStreamReader(ftpFile.openStream()));

String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

最佳答案

在这种情况下,我认为问题也与服务器相关,对于我来说,Filezilla Server 一切正常,除非文件名长度(包括目录)超过 255 个字符,但如果您想将 URL 类与另一个 FTP 一起使用,则必须重写或实现您自己的 URLStreamHandlerFactory。

       URL.setURLStreamHandlerFactory(...);

我还没有找到任何适合我最喜欢的 java FTP 客户端的 Apache,所以我开发了一个,但可能需要一些修改。

package net.custom.streamhandler.apacheftp;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;


public class ApacheURLStreamHandlerFactory implements URLStreamHandlerFactory {
public URLStreamHandler createURLStreamHandler(String protocol) {
//this will only override the chosen protocol
if ( protocol.equalsIgnoreCase("ftp") )
return new CustomHandler();
else
return null;
}
}
class CustomHandler extends URLStreamHandler {
protected URLConnection openConnection(URL url)
throws IOException {
return new CustomURLConnection(url);
}
}

class CustomURLConnection extends URLConnection {

int reply;
FTPClient ftp = new FTPClient();
InputStream in;
static int defaultPort = 21;
static String defaultPath = "/";

CustomURLConnection ( URL url)
throws IOException {
super( url );
}
synchronized public void connect() throws IOException {
try {
int port;
if ((port = url.getPort()) == -1 )
port = defaultPort;

ftp.connect(url.getHost(), port);
String login = "anonymous";
String password = "";
if(url.getAuthority().indexOf(':')>-1 &&
url.getAuthority().indexOf('@')>-1){
String []auxArray = url.getAuthority().replaceAll("@", ":").split(":");
login = auxArray[0];
password = auxArray[1];
}

ftp.login(login, password);

reply = ftp.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
System.out.println("Connected Apache Success");
} else {
System.out.println("Connection Apache Failed");
ftp.disconnect();
}
in = ftp.retrieveFileStream(url.getFile());

} catch (SocketException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
connected = true;

}
synchronized public InputStream getInputStream()
throws IOException {
if (!connected)
connect();
return ( in );
}
}

*请记住,您可以通过这种方式实现新的方法来处理 java.net.URL 的不同协议(protocol)。

你的代码...

    ...
{
String ftp = "ftp://user:pword@sysc/u/Xxxxxxxxxx/ICS/YT7/XxxxxXxxxxxxx/xxxxxxxxx/logs/xxxxxxxx/XX_YT.LOG";
try
{
URL.setURLStreamHandlerFactory(new ApacheURLStreamHandlerFactory());
...

再见

**(犯错是人之常情,宽恕是神圣的)

关于java - FTP 异常 501 "pathname"超过 8 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12645325/

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