Search in sources :

Example 1 with FtpProtocolException

use of sun.net.ftp.FtpProtocolException in project jdk8u_jdk by JetBrains.

the class FtpURLConnection method getInputStream.

/**
     * Get the InputStream to retreive the remote file. It will issue the
     * "get" (or "dir") command to the ftp server.
     *
     * @return  the <code>InputStream</code> to the connection.
     *
     * @throws  IOException if already opened for output
     * @throws  FtpProtocolException if errors occur during the transfert.
     */
@Override
public InputStream getInputStream() throws IOException {
    if (!connected) {
        connect();
    }
    if (http != null) {
        return http.getInputStream();
    }
    if (os != null) {
        throw new IOException("Already opened for output");
    }
    if (is != null) {
        return is;
    }
    MessageHeader msgh = new MessageHeader();
    boolean isAdir = false;
    try {
        decodePath(url.getPath());
        if (filename == null || type == DIR) {
            ftp.setAsciiType();
            cd(pathname);
            if (filename == null) {
                is = new FtpInputStream(ftp, ftp.list(null));
            } else {
                is = new FtpInputStream(ftp, ftp.nameList(filename));
            }
        } else {
            if (type == ASCII) {
                ftp.setAsciiType();
            } else {
                ftp.setBinaryType();
            }
            cd(pathname);
            is = new FtpInputStream(ftp, ftp.getFileStream(filename));
        }
        /* Try to get the size of the file in bytes.  If that is
            successful, then create a MeteredStream. */
        try {
            long l = ftp.getLastTransferSize();
            msgh.add("content-length", Long.toString(l));
            if (l > 0) {
                // Wrap input stream with MeteredStream to ensure read() will always return -1
                // at expected length.
                // Check if URL should be metered
                boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
                ProgressSource pi = null;
                if (meteredInput) {
                    pi = new ProgressSource(url, "GET", l);
                    pi.beginTracking();
                }
                is = new MeteredStream(is, pi, l);
            }
        } catch (Exception e) {
            e.printStackTrace();
        /* do nothing, since all we were doing was trying to
            get the size in bytes of the file */
        }
        if (isAdir) {
            msgh.add("content-type", "text/plain");
            msgh.add("access-type", "directory");
        } else {
            msgh.add("access-type", "file");
            String ftype = guessContentTypeFromName(fullpath);
            if (ftype == null && is.markSupported()) {
                ftype = guessContentTypeFromStream(is);
            }
            if (ftype != null) {
                msgh.add("content-type", ftype);
            }
        }
    } catch (FileNotFoundException e) {
        try {
            cd(fullpath);
            /* if that worked, then make a directory listing
                and build an html stream with all the files in
                the directory */
            ftp.setAsciiType();
            is = new FtpInputStream(ftp, ftp.list(null));
            msgh.add("content-type", "text/plain");
            msgh.add("access-type", "directory");
        } catch (IOException ex) {
            throw new FileNotFoundException(fullpath);
        } catch (FtpProtocolException ex2) {
            throw new FileNotFoundException(fullpath);
        }
    } catch (FtpProtocolException ftpe) {
        throw new IOException(ftpe);
    }
    setProperties(msgh);
    return is;
}
Also used : ProgressSource(sun.net.ProgressSource) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MessageHeader(sun.net.www.MessageHeader) MeteredStream(sun.net.www.MeteredStream) FtpProtocolException(sun.net.ftp.FtpProtocolException) FtpProtocolException(sun.net.ftp.FtpProtocolException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with FtpProtocolException

use of sun.net.ftp.FtpProtocolException in project warn-report by saaavsaaa.

the class FtpUtil method connect.

/**
 * 服务器连接
 *
 * @param ip       服务器IP
 * @param port     服务器端口
 * @param user     用户名
 * @param password 密码
 * @param path     服务器路径
 * @throws FtpProtocolException
 */
public void connect(String ip, int port, String user, String password, String path) {
    try {
        ftpClient = FtpClient.create();
        ftpClient.connect(new InetSocketAddress(ip, port));
        // ftpClient = FtpClient.create(new InetSocketAddress(ip, port));
        ftpClient.login(user, password.toCharArray());
        // 设置成2进制传输
        ftpClient.setBinaryType();
        // ftpClient.setAsciiType();
        System.out.println("login success!");
        if (path.length() != 0) {
            // 把远程系统上的目录切换到参数path所指定的目录
            ftpClient.changeDirectory(path);
        }
        ftpClient.setBinaryType();
    // ftpClient.setAsciiType();
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } catch (FtpProtocolException e) {
        e.printStackTrace();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FtpProtocolException(sun.net.ftp.FtpProtocolException)

Example 3 with FtpProtocolException

use of sun.net.ftp.FtpProtocolException in project Bytecoder by mirkosertic.

the class FtpURLConnection method connect.

/**
 * Connects to the FTP server and logs in.
 *
 * @throws  FtpLoginException if the login is unsuccessful
 * @throws  FtpProtocolException if an error occurs
 * @throws  UnknownHostException if trying to connect to an unknown host
 */
public synchronized void connect() throws IOException {
    if (connected) {
        return;
    }
    Proxy p = null;
    if (instProxy == null) {
        // no per connection proxy specified
        /**
         * Do we have to use a proxy?
         */
        ProxySelector sel = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<ProxySelector>() {

            public ProxySelector run() {
                return ProxySelector.getDefault();
            }
        });
        if (sel != null) {
            URI uri = sun.net.www.ParseUtil.toURI(url);
            Iterator<Proxy> it = sel.select(uri).iterator();
            while (it.hasNext()) {
                p = it.next();
                if (p == null || p == Proxy.NO_PROXY || p.type() == Proxy.Type.SOCKS) {
                    break;
                }
                if (p.type() != Proxy.Type.HTTP || !(p.address() instanceof InetSocketAddress)) {
                    sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type"));
                    continue;
                }
                // OK, we have an http proxy
                InetSocketAddress paddr = (InetSocketAddress) p.address();
                try {
                    http = new HttpURLConnection(url, p);
                    http.setDoInput(getDoInput());
                    http.setDoOutput(getDoOutput());
                    if (connectTimeout >= 0) {
                        http.setConnectTimeout(connectTimeout);
                    }
                    if (readTimeout >= 0) {
                        http.setReadTimeout(readTimeout);
                    }
                    http.connect();
                    connected = true;
                    return;
                } catch (IOException ioe) {
                    sel.connectFailed(uri, paddr, ioe);
                    http = null;
                }
            }
        }
    } else {
        // per connection proxy specified
        p = instProxy;
        if (p.type() == Proxy.Type.HTTP) {
            http = new HttpURLConnection(url, instProxy);
            http.setDoInput(getDoInput());
            http.setDoOutput(getDoOutput());
            if (connectTimeout >= 0) {
                http.setConnectTimeout(connectTimeout);
            }
            if (readTimeout >= 0) {
                http.setReadTimeout(readTimeout);
            }
            http.connect();
            connected = true;
            return;
        }
    }
    if (user == null) {
        user = "anonymous";
        Properties props = GetPropertyAction.privilegedGetProperties();
        String vers = props.getProperty("java.version");
        password = props.getProperty("ftp.protocol.user", "Java" + vers + "@");
    }
    try {
        ftp = FtpClient.create();
        if (p != null) {
            ftp.setProxy(p);
        }
        setTimeouts();
        if (port != -1) {
            ftp.connect(new InetSocketAddress(host, port));
        } else {
            ftp.connect(new InetSocketAddress(host, FtpClient.defaultPort()));
        }
    } catch (UnknownHostException e) {
        // Just keep throwing for now.
        throw e;
    } catch (FtpProtocolException fe) {
        if (ftp != null) {
            try {
                ftp.close();
            } catch (IOException ioe) {
                fe.addSuppressed(ioe);
            }
        }
        throw new IOException(fe);
    }
    try {
        ftp.login(user, password == null ? null : password.toCharArray());
    } catch (sun.net.ftp.FtpProtocolException e) {
        ftp.close();
        // Backward compatibility
        throw new sun.net.ftp.FtpLoginException("Invalid username/password");
    }
    connected = true;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) FtpProtocolException(sun.net.ftp.FtpProtocolException) Properties(java.util.Properties) URI(java.net.URI) ProxySelector(java.net.ProxySelector) FtpProtocolException(sun.net.ftp.FtpProtocolException) Proxy(java.net.Proxy) HttpURLConnection(sun.net.www.protocol.http.HttpURLConnection)

Example 4 with FtpProtocolException

use of sun.net.ftp.FtpProtocolException in project jdk8u_jdk by JetBrains.

the class FtpURLConnection method connect.

/**
     * Connects to the FTP server and logs in.
     *
     * @throws  FtpLoginException if the login is unsuccessful
     * @throws  FtpProtocolException if an error occurs
     * @throws  UnknownHostException if trying to connect to an unknown host
     */
public synchronized void connect() throws IOException {
    if (connected) {
        return;
    }
    Proxy p = null;
    if (instProxy == null) {
        // no per connection proxy specified
        /**
             * Do we have to use a proxy?
             */
        ProxySelector sel = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<ProxySelector>() {

            public ProxySelector run() {
                return ProxySelector.getDefault();
            }
        });
        if (sel != null) {
            URI uri = sun.net.www.ParseUtil.toURI(url);
            Iterator<Proxy> it = sel.select(uri).iterator();
            while (it.hasNext()) {
                p = it.next();
                if (p == null || p == Proxy.NO_PROXY || p.type() == Proxy.Type.SOCKS) {
                    break;
                }
                if (p.type() != Proxy.Type.HTTP || !(p.address() instanceof InetSocketAddress)) {
                    sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type"));
                    continue;
                }
                // OK, we have an http proxy
                InetSocketAddress paddr = (InetSocketAddress) p.address();
                try {
                    http = new HttpURLConnection(url, p);
                    http.setDoInput(getDoInput());
                    http.setDoOutput(getDoOutput());
                    if (connectTimeout >= 0) {
                        http.setConnectTimeout(connectTimeout);
                    }
                    if (readTimeout >= 0) {
                        http.setReadTimeout(readTimeout);
                    }
                    http.connect();
                    connected = true;
                    return;
                } catch (IOException ioe) {
                    sel.connectFailed(uri, paddr, ioe);
                    http = null;
                }
            }
        }
    } else {
        // per connection proxy specified
        p = instProxy;
        if (p.type() == Proxy.Type.HTTP) {
            http = new HttpURLConnection(url, instProxy);
            http.setDoInput(getDoInput());
            http.setDoOutput(getDoOutput());
            if (connectTimeout >= 0) {
                http.setConnectTimeout(connectTimeout);
            }
            if (readTimeout >= 0) {
                http.setReadTimeout(readTimeout);
            }
            http.connect();
            connected = true;
            return;
        }
    }
    if (user == null) {
        user = "anonymous";
        String vers = java.security.AccessController.doPrivileged(new GetPropertyAction("java.version"));
        password = java.security.AccessController.doPrivileged(new GetPropertyAction("ftp.protocol.user", "Java" + vers + "@"));
    }
    try {
        ftp = FtpClient.create();
        if (p != null) {
            ftp.setProxy(p);
        }
        setTimeouts();
        if (port != -1) {
            ftp.connect(new InetSocketAddress(host, port));
        } else {
            ftp.connect(new InetSocketAddress(host, FtpClient.defaultPort()));
        }
    } catch (UnknownHostException e) {
        // Just keep throwing for now.
        throw e;
    } catch (FtpProtocolException fe) {
        throw new IOException(fe);
    }
    try {
        ftp.login(user, password == null ? null : password.toCharArray());
    } catch (sun.net.ftp.FtpProtocolException e) {
        ftp.close();
        // Backward compatibility
        throw new sun.net.ftp.FtpLoginException("Invalid username/password");
    }
    connected = true;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) FtpProtocolException(sun.net.ftp.FtpProtocolException) URI(java.net.URI) ProxySelector(java.net.ProxySelector) FtpProtocolException(sun.net.ftp.FtpProtocolException) Proxy(java.net.Proxy) GetPropertyAction(sun.security.action.GetPropertyAction) HttpURLConnection(sun.net.www.protocol.http.HttpURLConnection)

Example 5 with FtpProtocolException

use of sun.net.ftp.FtpProtocolException in project jdk8u_jdk by JetBrains.

the class FtpURLConnection method getOutputStream.

/**
     * Get the OutputStream to store the remote file. It will issue the
     * "put" command to the ftp server.
     *
     * @return  the <code>OutputStream</code> to the connection.
     *
     * @throws  IOException if already opened for input or the URL
     *          points to a directory
     * @throws  FtpProtocolException if errors occur during the transfert.
     */
@Override
public OutputStream getOutputStream() throws IOException {
    if (!connected) {
        connect();
    }
    if (http != null) {
        OutputStream out = http.getOutputStream();
        // getInputStream() is neccessary to force a writeRequests()
        // on the http client.
        http.getInputStream();
        return out;
    }
    if (is != null) {
        throw new IOException("Already opened for input");
    }
    if (os != null) {
        return os;
    }
    decodePath(url.getPath());
    if (filename == null || filename.length() == 0) {
        throw new IOException("illegal filename for a PUT");
    }
    try {
        if (pathname != null) {
            cd(pathname);
        }
        if (type == ASCII) {
            ftp.setAsciiType();
        } else {
            ftp.setBinaryType();
        }
        os = new FtpOutputStream(ftp, ftp.putFileStream(filename, false));
    } catch (FtpProtocolException e) {
        throw new IOException(e);
    }
    return os;
}
Also used : OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream) IOException(java.io.IOException) FtpProtocolException(sun.net.ftp.FtpProtocolException)

Aggregations

FtpProtocolException (sun.net.ftp.FtpProtocolException)8 IOException (java.io.IOException)6 UnknownHostException (java.net.UnknownHostException)4 InetSocketAddress (java.net.InetSocketAddress)3 FileNotFoundException (java.io.FileNotFoundException)2 FilterOutputStream (java.io.FilterOutputStream)2 OutputStream (java.io.OutputStream)2 Proxy (java.net.Proxy)2 ProxySelector (java.net.ProxySelector)2 URI (java.net.URI)2 ProgressSource (sun.net.ProgressSource)2 MessageHeader (sun.net.www.MessageHeader)2 MeteredStream (sun.net.www.MeteredStream)2 HttpURLConnection (sun.net.www.protocol.http.HttpURLConnection)2 LocalDate (java.time.LocalDate)1 Properties (java.util.Properties)1 GetPropertyAction (sun.security.action.GetPropertyAction)1