Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:10:41+00:00 2026-06-12T17:10:41+00:00

I am trying to access a file via a URI using the FTP protocol.

  • 0

I am trying to access a file via a URI using the FTP protocol. For obvious security reasons I had to make some changes but this is where the problems seem to be coming from.

My URI is as follows:
ftp://user:pasword@host.net/u/Bigpathname/XYZ/ABC/BigPathname/bigpathname/xyz/abc/MY_LOG.LOG

And I see this exception:

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:10:43+00:00Added an answer on June 12, 2026 at 5:10 pm

    In this case I think the problem is also Server Related, It all works fine for me with Filezilla Server except when the filename length(including directories) exceeds 255 chars but if you want to use the URL class with another FTP you must override or implement your own URLStreamHandlerFactory.

           URL.setURLStreamHandlerFactory(...);
    

    I haven’t found any for my favorite java FTP Client witch is Apache one so I have developed one but may need a few touch ups.

    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 );  
        }  
    } 
    

    *Keep in mind that you can implement new ways to handle different protocols for the java.net.URL this way.

    Your code…

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

    G’Bye

    **(To err is human, to forgive is divine)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a file via FTP per FtpWebrequest - the download fails
so I am trying to access some content in an external php file (not
I am trying to install vTigercrm-5.4.0.tar.gz file via ftp path.. and I am following
I am trying to access a file for xml parsing with this: InputStream inputStream
I'm trying to access a file from one class inside another package inside a
So in C# I am trying to access a file on a network, for
I am trying to access a resource file from a servlet but getting HTTP
I am trying to access an XML file from JSP on my Tomcat server.
i am trying to access a Excel file stored in my Resources and build-embed
I'm trying to access an xml file over http. The address is similar to:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.