Search in sources :

Example 31 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project loinc2hpo by monarch-initiative.

the class FileDownloader method copyURLToFileWithFTP.

private boolean copyURLToFileWithFTP(URL src, File dest) throws FileDownloadException {
    final FTPClient ftp = new FTPClient();
    // passive mode for firewalls
    ftp.enterLocalPassiveMode();
    try {
        if (src.getPort() != -1)
            ftp.connect(src.getHost(), src.getPort());
        else
            ftp.connect(src.getHost());
        if (!ftp.login("anonymous", "anonymous@example.com"))
            throw new IOException("Could not login with anonymous:anonymous@example.com");
        if (!ftp.isConnected())
            logger.error("Weird, not connected!");
    } catch (SocketException e) {
        throw new FileDownloadException("ERROR: problem connecting when downloading file.", e);
    } catch (IOException e) {
        throw new FileDownloadException("ERROR: problem connecting when downloading file.", e);
    }
    try {
        // binary file transfer
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
    } catch (IOException e) {
        try {
            ftp.logout();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        try {
            ftp.disconnect();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        throw new FileDownloadException("ERROR: could not use binary transfer.", e);
    }
    InputStream in = null;
    OutputStream out = null;
    try {
        final String parentDir = new File(src.getPath()).getParent().substring(1);
        final String fileName = new File(src.getPath()).getName();
        if (!ftp.changeWorkingDirectory(parentDir))
            throw new FileNotFoundException("Could not change directory to " + parentDir);
        // Try to get file size.
        FTPFile[] files = ftp.listFiles(fileName);
        long fileSize = -1;
        for (int i = 0; i < files.length; ++i) if (files[i].getName().equals(fileName))
            fileSize = files[i].getSize();
        ftp.pwd();
        ProgressBar pb = null;
        if (fileSize != -1)
            pb = new ProgressBar(0, fileSize, options.printProgressBar);
        else
            logger.info("(server did not tell us the file size, no progress bar)");
        // Download file.
        in = ftp.retrieveFileStream(fileName);
        if (in == null)
            throw new FileNotFoundException("Could not open connection for file " + fileName);
        out = new FileOutputStream(dest);
        BufferedInputStream inBf = new BufferedInputStream(in);
        byte[] buffer = new byte[128 * 1024];
        int readCount;
        long pos = 0;
        if (pb != null)
            pb.print(pos);
        while ((readCount = inBf.read(buffer)) > 0) {
            out.write(buffer, 0, readCount);
            pos += readCount;
            if (pb != null)
                pb.print(pos);
        }
        in.close();
        out.close();
        if (pb != null && pos != pb.getMax())
            pb.print(fileSize);
    // if (!ftp.completePendingCommand())
    // throw new IOException("Could not finish download!");
    } catch (FileNotFoundException e) {
        dest.delete();
        try {
            ftp.logout();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        try {
            ftp.disconnect();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        throw new FileDownloadException("ERROR: problem downloading file.", e);
    } catch (IOException e) {
        dest.delete();
        try {
            ftp.logout();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        try {
            ftp.disconnect();
        } catch (IOException e1) {
        // swallow, nothing we can do about it
        }
        throw new FileDownloadException("ERROR: problem downloading file.", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            // swallow, nothing we can do
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            // swallow, nothing we can do
            }
        }
    // if (ftp != null) {
    // try {
    // ftp.completePendingCommand();
    // } catch (IOException e) {
    // // swallow, nothing we can do
    // }
    // }
    }
    return false;
}
Also used : SocketException(java.net.SocketException) FTPFile(org.apache.commons.net.ftp.FTPFile) FTPClient(org.apache.commons.net.ftp.FTPClient) FTPFile(org.apache.commons.net.ftp.FTPFile) FileDownloadException(org.monarchinitiative.loinc2hpo.exception.FileDownloadException)

Example 32 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project pancm_project by xuwujing.

the class FtpHelper method testFtp.

private static boolean testFtp(String ip, int port, String user, String pwd) throws IOException {
    FTPClient ftpClient = new FTPClient();
    // 连接ftp
    ftpClient.connect(ip, port);
    // 登陆ftp
    ftpClient.login(user, pwd);
    return FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
}
Also used : FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 33 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project pancm_project by xuwujing.

the class FtpHelper method getListFiles.

/**
 * 获取文件列表文件的属性
 *
 * @param ip
 * @param port
 * @param user
 * @param pwd
 * @param url
 * @return
 */
public static List<Map<String, String>> getListFiles(String ip, int port, String user, String pwd, String url) throws IOException {
    List<Map<String, String>> mapList = new ArrayList<>();
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(ip, port);
    ftpClient.login(user, pwd);
    FTPFile[] ftpFiles = ftpClient.listFiles(url);
    if (ftpFiles != null && ftpFiles.length > 0) {
        for (FTPFile ftpFile : ftpFiles) {
            Map<String, String> map = new HashMap<>();
            map.put("fileName", ftpFile.getName());
            map.put("fileSize", getSize(ftpFile.getSize()));
            map.put("fileTime", DateHelper.getDateTime(ftpFile.getTimestamp().getTime()));
            mapList.add(map);
        }
    }
    return mapList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) Map(java.util.Map) HashMap(java.util.HashMap) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 34 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project pancm_project by xuwujing.

the class FtpHelper method testFtp.

private static boolean testFtp(String ip, int port, String user, String pwd) throws IOException {
    FTPClient ftpClient = new FTPClient();
    // 连接ftp
    ftpClient.connect(ip, port);
    // 登陆ftp
    ftpClient.login(user, pwd);
    return FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
}
Also used : FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 35 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project eol-globi-data by jhpoelen.

the class ResourceUtil method asInputStream.

public static InputStream asInputStream(URI resource, InputStreamFactory factory) throws IOException {
    try {
        InputStream is;
        if (isHttpURI(resource)) {
            LOG.info("caching of [" + resource + "] started...");
            is = getCachedRemoteInputStream(resource, factory);
            LOG.info("caching of [" + resource + "] complete.");
        } else if (isFileURI(resource)) {
            is = factory.create(new FileInputStream(new File(resource)));
        } else if (StringUtils.startsWith(resource.toString(), "jar:file:/")) {
            URL url = resource.toURL();
            URLConnection urlConnection = url.openConnection();
            // Prevent leaking of jar file descriptors by disabling jar cache.
            // see https://stackoverflow.com/a/36518430
            urlConnection.setUseCaches(false);
            is = factory.create(urlConnection.getInputStream());
        } else if (StringUtils.startsWith(resource.getScheme(), "ftp")) {
            FTPClient ftpClient = new FTPClient();
            try {
                ftpClient.connect(resource.getHost());
                ftpClient.enterLocalPassiveMode();
                ftpClient.login("anonymous", "info@globalbioticinteractions.org");
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
                ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                is = ftpClient.isConnected() ? cacheAndOpenStream(ftpClient.retrieveFileStream(resource.getPath()), factory) : null;
            } finally {
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
            }
        } else {
            String classpathResource = resource.toString();
            if (StringUtils.startsWith(classpathResource, "classpath:")) {
                classpathResource = StringUtils.replace(classpathResource, "classpath:", "");
            }
            is = factory.create(ResourceUtil.class.getResourceAsStream(classpathResource));
        }
        if (is == null) {
            final URI uri = fromShapefileDir(resource);
            if (uri == null) {
                throw new IOException("failed to open resource [" + resource + "]");
            } else {
                is = new FileInputStream(new File(uri));
            }
        }
        if (StringUtils.endsWith(resource.toString(), ".gz")) {
            is = new GZIPInputStream(is);
        }
        return is;
    } catch (IOException ex) {
        throw new IOException("issue accessing [" + resource + "]", ex);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) FileInputStream(java.io.FileInputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) FTPClient(org.apache.commons.net.ftp.FTPClient)

Aggregations

FTPClient (org.apache.commons.net.ftp.FTPClient)128 IOException (java.io.IOException)78 FTPFile (org.apache.commons.net.ftp.FTPFile)36 Test (org.junit.Test)25 InputStream (java.io.InputStream)20 FrameworkException (org.structr.common.error.FrameworkException)20 Tx (org.structr.core.graph.Tx)20 FtpTest (org.structr.web.files.FtpTest)20 File (java.io.File)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 FileInputStream (java.io.FileInputStream)10 FileOutputStream (java.io.FileOutputStream)8 OutputStream (java.io.OutputStream)5 UnknownHostException (java.net.UnknownHostException)5 ConnectException (java.net.ConnectException)4 PrintCommandListener (org.apache.commons.net.PrintCommandListener)4 FTPSClient (org.apache.commons.net.ftp.FTPSClient)4 FTPUtils (com.cas.sim.tis.util.FTPUtils)3 FileNotFoundException (java.io.FileNotFoundException)3 MalformedURLException (java.net.MalformedURLException)3