0

I have a code that I use to upload some txt on ftp server. So if I try to use this code sometimes works and sometimes not works.

I have this error:

2015-06-17 20:43:33 DEBUG [LoggerFactory.MyLog4J:42] - java.net.UnknownHostException: easyeuc.altervista.org
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.ftp.impl.FtpClient.doConnect(Unknown Source)
    at sun.net.ftp.impl.FtpClient.tryConnect(Unknown Source)
    at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
    at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
    at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(Unknown Source)
    at prove.FileUpload.upload(FileUpload.java:86)
    at Backup.PanelChiusuraGiornata$1.doInBackground(PanelChiusuraGiornata.java:393)
    at Backup.PanelChiusuraGiornata$1.doInBackground(PanelChiusuraGiornata.java:1)
    at javax.swing.SwingWorker$1.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at javax.swing.SwingWorker.run(Unknown Source)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

This is the code that I use:

public void upload( String ftpServer, String user, String password,
        String fileName, File source ) throws MalformedURLException,
        IOException{
    MyLog4J log = getMyLog4j();
    if (ftpServer != null && fileName != null && source != null)
    {
        StringBuffer sb = new StringBuffer( "ftp://" );
        // check for authentication else assume its anonymous access.
        if (user != null && password != null)
        {
            sb.append( user );
            sb.append( ':' );
            sb.append( password );
            sb.append( '@' );
        }
        sb.append( ftpServer );
        sb.append( '/' );
        sb.append( fileName );
        /*
         * type ==> a=ASCII mode, i=image (binary) mode, d= file directory
         * listing
         */
        sb.append( ";type=i" );

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try
        {
            URL url = new URL( sb.toString() );
            URLConnection urlc = url.openConnection();

            bos = new BufferedOutputStream( urlc.getOutputStream() );
            bis = new BufferedInputStream( new FileInputStream( source ) );

            int i;
            // read byte by byte until end of stream
            while ((i = bis.read()) != -1)
            {
                bos.write( i );
            }
        }
        finally
        {
            if (bis != null)
                try
            {
                    bis.close();
            }
            catch (IOException ioe)
            {
                log.logStackTrace(ioe);
            }
            if (bos != null)
                try
            {
                    bos.close();
            }
            catch (IOException ioe)
            {
                log.logStackTrace(ioe);
            }
        }
    }
    else
    {
        System.out.println( "Input not available." );
    }
}

The error in on this row

bos = new BufferedOutputStream( urlc.getOutputStream() );

I'm not able to find the problem.

2
  • Have you tried with the IP address? Commented Jun 18, 2015 at 12:24
  • from what I can see, you get the error because the host (IP) you are connecting to, does not seem to be valid, or your machine can't connect to it. Commented Jun 18, 2015 at 12:29

1 Answer 1

1

In connections to FTP's, as you know, several problems can happen, disconnection, lost of information, host unreachable, connection timeouts etc... You can never be sure ALL will happen as expected, so, you must check all the problems (Exceptions) that can happen (thrown) during the FTP connection.

As I mentioned, there are SEVERAL problems in a FTP connection, so in this example we will catch just your shown problem and all other exceptions

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
    URL url = new URL( sb.toString() );
    URLConnection urlc = url.openConnection();

    bos = new BufferedOutputStream( urlc.getOutputStream() );
    bis = new BufferedInputStream( new FileInputStream( source ) );

    int i;
    // read byte by byte until end of stream
    while ((i = bis.read()) != -1)
    {
        bos.write( i );
    }
} 
// catch UnknownHostException problem
catch (UnknownHostException e) {
    System.out.println( "Unknown Host.");
}
// catch all other problems that can happen with ftp connection
catch (Exception e) {
    // do what you want in case of other errors
}
finally
{
    // finally block (same you have)
}

But, I would highly recommend you to check FTPClient from ApacheCommons. Easy to use and understand, FTPClient will make your life easier and your code more stable and readable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I have used FTPClient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.