Example usage for org.apache.commons.net.ftp FTPClient retrieveFileStream

List of usage examples for org.apache.commons.net.ftp FTPClient retrieveFileStream

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient retrieveFileStream.

Prototype

public InputStream retrieveFileStream(String remote) throws IOException 

Source Link

Document

Returns an InputStream from which a named file from the server can be read.

Usage

From source file:edu.stanford.epad.common.util.FTPUtil.java

public void getFile(String remoteFile, File localFile) throws Exception {

    FTPClient ftpClient = new FTPClient();
    OutputStream outputStream = null;
    try {/*from  w w  w.  j av a 2 s  .c  om*/
        ftpClient.connect(ftpHost, ftpPort);
        ftpClient.enterLocalPassiveMode();
        if (ftpUser != null && ftpUser.length() > 0)
            ftpClient.login(ftpUser, ftpPassword);
        else
            ftpClient.login("anonymous", "");
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
        InputStream inputStream = ftpClient.retrieveFileStream(remoteFile);
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
        boolean commandOK = ftpClient.completePendingCommand();
        //boolean success = ftpClient.retrieveFile(remoteFile, outputStream);

        //if (!success) {
        //   throw new Exception("Error retrieving remote file: " + remoteFile);
        //}   
    } finally {
        outputStream.close();
        ftpClient.disconnect();
    }
}

From source file:facturacion.ftp.FtpServer.java

public static InputStream getTokenInputStream(String remote_file_ruta) {
    FTPClient ftpClient = new FTPClient();
    try {/*from  w w w. j  av a2 s.  c om*/
        //ftpClient.connect("127.0.0.1", 21 );
        //ftpClient.login("erpftp", "Tribut@2014");
        ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken),
                Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken)));
        ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken),
                Config.getInstance().getProperty(Config.PassFtpToken));
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        int reply = ftpClient.getReplyCode();

        System.out.println("Respuesta recibida de conexin FTP:" + reply);

        if (!FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Imposible conectarse al servidor");
            //return -1;
        } else {
            System.out.println("se conecto al servidor");
        }
        //ftpClient.enterLocalPassiveMode();
        // crear directorio
        //OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(file_ruta));
        //System.out.println("File #1 has been downloaded successfully. 1");
        //FileInputStream fis = new FileInputStream("C:\\Users\\aaguerra\\Desktop\\firmado2.p12");
        //InputStream is = fis;
        System.out.println("File rutaqq=" + "1-/" + remote_file_ruta);
        InputStream is = ftpClient.retrieveFileStream(remote_file_ruta);

        if (is == null)
            System.out.println("File #1 es null token");
        else
            System.out.println("File #1 no es null token");

        //return ftpClient.retrieveFileStream(remote_file_ruta);
        return is;
    } catch (IOException ex) {
        System.out.println("File #1 has been downloaded successfully. 222");
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            System.out.println("File #1 has been downloaded successfully. 3");
            return null;
            //ex.printStackTrace();
        }
    }
    return null;
}

From source file:com.jsystem.j2autoit.AutoItAgent.java

private static void getFileFtp(String user, String password, String host, int port, String fileName,
        String location) throws Exception {
    Log.info("\nretrieve " + fileName + NEW_LINE);
    FTPClient client = new FTPClient();

    client.connect(host, port); //connect to the management ftp server
    int reply = client.getReplyCode(); // check connection

    if (!FTPReply.isPositiveCompletion(reply)) {
        throw new Exception("FTP fail to connect");
    }/*from  www  .ja v  a 2s  .co m*/

    if (!client.login(user, password)) { //check login
        throw new Exception("FTP fail to login");
    }

    //      FileOutputStream fos = null;
    try {
        File locationFile = new File(location);
        File dest = new File(locationFile, fileName);
        if (dest.exists()) {
            dest.delete();
        } else {
            locationFile.mkdirs();
        }
        boolean status = client.changeWorkingDirectory("/");
        Log.info("chdir-status:" + status + NEW_LINE);
        client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        client.setFileType(FTPClient.BINARY_FILE_TYPE);
        client.enterLocalActiveMode();

        InputStream in = client.retrieveFileStream(fileName);
        if (in == null) {
            Log.error("Input stream is null\n");
            throw new Exception("Fail to retrieve file " + fileName);
        }
        Thread.sleep(3000);
        saveInputStreamToFile(in, new File(location, fileName));
    } finally {
        client.disconnect();
    }

}

From source file:com.tobias.vocabulary_trainer.UpdateVocabularies.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.update_vocabularies_layout);
    prefs = getSharedPreferences(USER_PREFERENCE, Activity.MODE_PRIVATE);
    serverAdresseEditText = (EditText) findViewById(R.id.server_adresse_edit_text);
    serverUsernameEditText = (EditText) findViewById(R.id.server_username_edit_text);
    serverPasswordEditText = (EditText) findViewById(R.id.server_password_edit_text);
    serverPortEditText = (EditText) findViewById(R.id.server_port_edit_text);
    serverFileEditText = (EditText) findViewById(R.id.server_file_edit_text);
    localFileEditText = (EditText) findViewById(R.id.local_file_edit_text);

    vocabularies = new VocabularyData(this);
    System.out.println("before updateUIFromPreferences();");
    updateUIFromPreferences();// w w  w . j a v  a 2  s  .co m
    System.out.println("after updateUIFromPreferences();");

    final Button ServerOkButton = (Button) findViewById(R.id.server_ok);
    ServerOkButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            System.out.println("ServerOKButton pressed");
            savePreferences();
            InputStream in;
            String serverAdresse = serverAdresseEditText.getText().toString();
            String serverUsername = serverUsernameEditText.getText().toString();
            String serverPassword = serverPasswordEditText.getText().toString();
            int serverPort = Integer.parseInt(serverPortEditText.getText().toString());
            String serverFile = serverFileEditText.getText().toString();

            FTPClient ftp;
            ftp = new FTPClient();
            try {
                int reply;
                System.out.println("try to connect to ftp server");
                ftp.connect(serverAdresse, serverPort);
                System.out.print(ftp.getReplyString());
                // After connection attempt, you should check the reply code to verify
                // success.
                reply = ftp.getReplyCode();
                if (FTPReply.isPositiveCompletion(reply)) {
                    System.out.println("connected to ftp server");
                } else {
                    ftp.disconnect();
                    System.out.println("FTP server refused connection.");
                }

                // transfer files
                System.out.println("try to login");
                ftp.login(serverUsername, serverPassword);
                System.out.println("current working directory: " + ftp.printWorkingDirectory());
                System.out.println("try to start downloading");
                in = ftp.retrieveFileStream(serverFile);
                // files transferred
                //write to database and textfile on sdcard
                vocabularies.readVocabularies(in, false);
                ftp.logout();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (ftp.isConnected()) {
                    try {
                        ftp.disconnect();
                    } catch (IOException ioe) {
                        // do nothing
                    }
                }
            }
            //               settings.populateSpinners();
            finish();
        }
    });
    final Button LocalFileOkButton = (Button) findViewById(R.id.local_file_ok);
    LocalFileOkButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            File f = new File(Environment.getExternalStorageDirectory(),
                    localFileEditText.getText().toString());
            savePreferences();
            vocabularies.readVocabularies(f, false);
            //               settings.populateSpinners();
            finish();
        }
    });
    final Button CancelButton = (Button) findViewById(R.id.Cancel);
    CancelButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    });
    vocabularies.close();
}

From source file:com.seajas.search.contender.service.modifier.FeedModifierService.java

/**
 * Retrieve the content of a result feed URL.
 * //w w  w  . j  av  a2s  .c  om
 * @param uri
 * @param encodingOverride
 * @param userAgent
 * @param resultHeaders
 * @return Reader
 */
private Reader getContent(final URI uri, final String encodingOverride, final String userAgent,
        final Map<String, String> resultHeaders) {
    Reader result = null;
    String contentType = null;

    // Retrieve the feed

    try {
        InputStream inputStream = null;

        if (uri.getScheme().equalsIgnoreCase("ftp") || uri.getScheme().equalsIgnoreCase("ftps")) {
            FTPClient ftpClient = uri.getScheme().equalsIgnoreCase("ftps") ? new FTPSClient() : new FTPClient();

            try {
                ftpClient.connect(uri.getHost(), uri.getPort() != -1 ? uri.getPort() : 21);

                if (StringUtils.hasText(uri.getUserInfo())) {
                    if (uri.getUserInfo().contains(":"))
                        ftpClient.login(uri.getUserInfo().substring(0, uri.getUserInfo().indexOf(":")),
                                uri.getUserInfo().substring(uri.getUserInfo().indexOf(":") + 1));
                    else
                        ftpClient.login(uri.getUserInfo(), "");

                    inputStream = ftpClient.retrieveFileStream(uri.getPath());
                }
            } finally {
                ftpClient.disconnect();
            }
        } else if (uri.getScheme().equalsIgnoreCase("file")) {
            File file = new File(uri);

            if (!file.isDirectory())
                inputStream = new FileInputStream(uri.getPath());
            else
                inputStream = RSSDirectoryBuilder.build(file);
        } else if (uri.getScheme().equalsIgnoreCase("http") || uri.getScheme().equalsIgnoreCase("https")) {
            try {
                HttpGet method = new HttpGet(uri.toString());

                if (resultHeaders != null)
                    for (Entry<String, String> resultHeader : resultHeaders.entrySet())
                        method.setHeader(new BasicHeader(resultHeader.getKey(), resultHeader.getValue()));
                if (userAgent != null)
                    method.setHeader(CoreProtocolPNames.USER_AGENT, userAgent);

                SizeRestrictedHttpResponse response = httpClient.execute(method,
                        new SizeRestrictedResponseHandler(maximumContentLength, uri));

                try {
                    if (response != null) {
                        inputStream = new ByteArrayInputStream(response.getResponse());
                        contentType = response.getContentType() != null ? response.getContentType().getValue()
                                : null;
                    } else
                        return null;
                } catch (RuntimeException e) {
                    method.abort();

                    throw e;
                }
            } catch (IllegalArgumentException e) {
                logger.error("Invalid URL " + uri.toString() + " - not returning content", e);

                return null;
            }
        } else {
            logger.error("Unknown protocol " + uri.getScheme() + ". Skipping feed.");

            return null;
        }

        // Guess the character encoding using ROME's reader, then buffer it so we can discard the input stream (and close the connection)

        InputStream readerInputStream = new BufferedInputStream(inputStream);
        MediaType mediaType = autoDetectParser.getDetector().detect(readerInputStream, new Metadata());

        try {
            Reader reader = null;

            if (mediaType.getType().equals("application")) {
                if (mediaType.getSubtype().equals("x-gzip")) {
                    GZIPInputStream gzipInputStream = new GZIPInputStream(readerInputStream);

                    if (encodingOverride != null)
                        reader = readerToBuffer(new StringBuffer(),
                                new InputStreamReader(gzipInputStream, encodingOverride), false);
                    else
                        reader = readerToBuffer(new StringBuffer(),
                                contentType != null ? new XmlHtmlReader(gzipInputStream, contentType, true)
                                        : new XmlReader(gzipInputStream, true),
                                false);

                    gzipInputStream.close();
                } else if (mediaType.getSubtype().equals("zip")) {
                    ZipFile zipFile = null;

                    // ZipInputStream can't do read-aheads, so we have to use a temporary on-disk file instead

                    File temporaryFile = File.createTempFile("profiler-", ".zip");

                    try {
                        FileOutputStream zipOutputStream = new FileOutputStream(temporaryFile);
                        IOUtils.copy(readerInputStream, zipOutputStream);

                        readerInputStream.close();

                        zipOutputStream.flush();
                        zipOutputStream.close();

                        // Create a new entry and process it

                        zipFile = new ZipFile(temporaryFile);
                        Enumeration<? extends ZipEntry> zipEnumeration = zipFile.entries();

                        ZipEntry zipEntry = zipEnumeration.nextElement();

                        if (zipEntry == null || zipEntry.isDirectory() || zipEnumeration.hasMoreElements()) {
                            logger.error(
                                    "ZIP files are currently expected to contain one and only one entry, which is to be a file");

                            return null;
                        }

                        // We currently only perform prolog stripping for ZIP files

                        InputStream zipInputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));

                        if (encodingOverride != null)
                            reader = readerToBuffer(new StringBuffer(), new InputStreamReader(
                                    new BufferedInputStream(zipInputStream), encodingOverride), true);
                        else
                            result = readerToBuffer(new StringBuffer(),
                                    contentType != null
                                            ? new XmlHtmlReader(new BufferedInputStream(zipInputStream),
                                                    contentType, true)
                                            : new XmlReader(new BufferedInputStream(zipInputStream), true),
                                    true);
                    } catch (Exception e) {
                        logger.error("An error occurred during ZIP file processing", e);

                        return null;
                    } finally {
                        if (zipFile != null)
                            zipFile.close();

                        if (!temporaryFile.delete())
                            logger.error("Unable to delete temporary file");
                    }
                }
            }

            if (result == null) {
                if (encodingOverride != null)
                    result = readerToBuffer(new StringBuffer(), reader != null ? reader
                            : new InputStreamReader(readerInputStream, encodingOverride), false);
                else
                    result = readerToBuffer(new StringBuffer(),
                            reader != null ? reader
                                    : contentType != null
                                            ? new XmlHtmlReader(readerInputStream, contentType, true)
                                            : new XmlReader(readerInputStream, true),
                            false);
            }
        } catch (Exception e) {
            logger.error("An error occurred during stream processing", e);

            return null;
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        logger.error("Could not retrieve the given feed: " + e.getMessage(), e);

        return null;
    }

    return result;
}

From source file:edu.lternet.pasta.dml.download.DownloadHandler.java

/**
 * Gets content from given source and writes it to DataStorageInterface 
 * to store them. This method will be called by run()
 * /*www.ja  va2 s .  co  m*/
 * @param resourceName  the URL to the source data to be retrieved
 */
protected boolean getContentFromSource(String resourceName) {
    boolean successFlag = false;
    QualityCheck onlineURLsQualityCheck = null;
    boolean onlineURLsException = false; // used to determine status of onlineURLs quality check

    if (resourceName != null) {
        resourceName = resourceName.trim();
    }

    if (resourceName != null && (resourceName.startsWith("http://") || resourceName.startsWith("https://")
            || resourceName.startsWith("file://") || resourceName.startsWith("ftp://"))) {
        // get the data from a URL
        int responseCode = 0;
        String responseMessage = null;

        try {
            URL url = new URL(resourceName);
            boolean isFTP = false;

            if (entity != null) {
                String contentType = null;

                // Find the right MIME type and set it as content type
                if (resourceName.startsWith("http")) {
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("HEAD");
                    httpURLConnection.connect();
                    contentType = httpURLConnection.getContentType();
                    responseCode = httpURLConnection.getResponseCode();
                    responseMessage = httpURLConnection.getResponseMessage();
                } else if (resourceName.startsWith("file")) {
                    URLConnection urlConnection = url.openConnection();
                    urlConnection.connect();
                    contentType = urlConnection.getContentType();
                } else { // FTP
                    isFTP = true;
                    contentType = "application/octet-stream";
                }

                entity.setUrlContentType(contentType);
            }

            if (!isFTP) { // HTTP(S) or FILE
                InputStream filestream = url.openStream();

                try {
                    successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream);
                } catch (IOException e) {
                    exception = e;
                    String errorMessage = e.getMessage();
                    if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) {
                        onlineURLsException = true;
                    }
                } finally {
                    filestream.close();
                }
            } else { // FTP
                String[] urlParts = resourceName.split("/");
                String address = urlParts[2];
                String dir = "/";
                for (int i = 3; i < urlParts.length - 1; i++) {
                    dir += urlParts[i] + "/";
                }
                String fileName = urlParts[urlParts.length - 1];
                FTPClient ftpClient = new FTPClient();
                ftpClient.connect(address);
                ftpClient.login(ANONYMOUS, anonymousFtpPasswd);
                ftpClient.changeWorkingDirectory(dir);
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode(); // necessary to avoid firewall blocking
                InputStream filestream = ftpClient.retrieveFileStream(fileName);
                try {
                    successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream);
                } catch (IOException e) {
                    exception = e;
                    String errorMessage = e.getMessage();
                    if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) {
                        onlineURLsException = true;
                    }
                } finally {
                    try {
                        filestream.close();
                    } catch (IOException e) {
                        exception = new DataSourceNotFoundException(String
                                .format("Error closing local file '%s': %s", resourceName, e.getMessage()));
                        onlineURLsException = true;
                    }
                }

                // logout and disconnect if FTP session
                if (resourceName.startsWith("ftp") && ftpClient != null) {
                    try {
                        ftpClient.enterLocalActiveMode();
                        ftpClient.logout();
                        ftpClient.disconnect();
                    } catch (IOException e) {
                        exception = new DataSourceNotFoundException(
                                String.format("Error disconnecting from FTP with resource '%s': %s",
                                        resourceName, e.getMessage()));
                        onlineURLsException = true;
                    }
                }
            }
        } catch (MalformedURLException e) {
            String eClassName = e.getClass().getName();
            String eMessage = String.format("%s: %s", eClassName, e.getMessage());
            onlineURLsException = true;
            exception = new DataSourceNotFoundException(
                    String.format("The URL '%s' is a malformed URL: %s", resourceName, eMessage));
        } catch (IOException e) {
            String eClassName = e.getClass().getName();
            String eMessage = String.format("%s: %s", eClassName, e.getMessage());
            if (responseCode > 0) {
                eMessage = String.format("Response Code: %d %s; %s", responseCode, responseMessage, eMessage);
            }
            onlineURLsException = true;
            exception = new DataSourceNotFoundException(
                    String.format("The URL '%s' is not reachable: %s", resourceName, eMessage));
        }

        // Initialize the "Online URLs are live" quality check
        String qualityCheckIdentifier = "onlineURLs";
        QualityCheck qualityCheckTemplate = QualityReport.getQualityCheckTemplate(qualityCheckIdentifier);
        onlineURLsQualityCheck = new QualityCheck(qualityCheckIdentifier, qualityCheckTemplate);

        if (QualityCheck.shouldRunQualityCheck(entity, onlineURLsQualityCheck)) {
            String resourceNameEscaped = embedInCDATA(resourceName);

            if (!onlineURLsException) {
                onlineURLsQualityCheck.setStatus(Status.valid);
                onlineURLsQualityCheck.setFound("true");
                onlineURLsQualityCheck.setExplanation("Succeeded in accessing URL: " + resourceNameEscaped);
            } else {
                onlineURLsQualityCheck.setFailedStatus();
                onlineURLsQualityCheck.setFound("false");
                String explanation = "Failed to access URL: " + resourceNameEscaped;
                explanation = explanation + "; " + embedInCDATA(exception.getMessage());
                onlineURLsQualityCheck.setExplanation(explanation);
            }

            entity.addQualityCheck(onlineURLsQualityCheck);
        }

        return successFlag;
    } else if (resourceName != null && resourceName.startsWith("ecogrid://")) {
        // get the docid from url
        int start = resourceName.indexOf("/", 11) + 1;
        //log.debug("start: " + start);
        int end = resourceName.indexOf("/", start);

        if (end == -1) {
            end = resourceName.length();
        }

        //log.debug("end: " + end);
        String ecogridIdentifier = resourceName.substring(start, end);
        // pass this docid and get data item
        //System.out.println("the endpoint is "+ECOGRIDENDPOINT);
        //System.out.println("The identifier is "+ecogridIdentifier);
        //return false;
        return getContentFromEcoGridSource(ecogridEndPoint, ecogridIdentifier);
    } else if (resourceName != null && resourceName.startsWith("srb://")) {
        // get srb docid from the url
        String srbIdentifier = transformSRBurlToDocid(resourceName);
        // reset endpoint for srb (This is hack we need to figure ou
        // elegent way to do this
        //mEndPoint = Config.getValue("//ecogridService/srb/endPoint");
        // pass this docid and get data item
        //log.debug("before get srb data");
        return getContentFromEcoGridSource(SRBENDPOINT, srbIdentifier);
    } else {
        successFlag = false;
        return successFlag;
    }
}

From source file:com.jaeksoft.searchlib.scheduler.task.TaskFtpXmlFeed.java

@Override
public void execute(Client client, TaskProperties properties, Variables variables, TaskLog taskLog)
        throws SearchLibException {
    String server = properties.getValue(propServer);
    String path = properties.getValue(propPath);
    String login = properties.getValue(propLogin);
    String password = properties.getValue(propPassword);
    String fileNamePattern = properties.getValue(propFileNamePattern);
    boolean deleteAfterLoad = Boolean.TRUE.toString().equals(properties.getValue(propDeleteAfterLoad));
    boolean truncateWhenFilesFound = Boolean.TRUE.toString()
            .equals(properties.getValue(propTruncateIndexWhenFilesFound));
    Pattern pattern = null;//from   ww w  .  j a v a2s.  c om
    if (fileNamePattern != null && fileNamePattern.length() > 0)
        pattern = Pattern.compile(fileNamePattern);

    String p = properties.getValue(propBuffersize);
    String xsl = properties.getValue(propXsl);
    File xmlTempResult = null;
    int bufferSize = 50;
    if (p != null && p.length() > 0)
        bufferSize = Integer.parseInt(p);
    HttpDownloader httpDownloader = client.getWebCrawlMaster().getNewHttpDownloader(true);
    FTPClient ftp = null;
    InputStream inputStream = null;
    try {
        // FTP Connection
        ftp = new FTPClient();
        checkConnect(ftp, server, login, password);
        FTPFile[] files = ftp.listFiles(path, new FtpFileInstance.FtpInstanceFileFilter(true, false, null));
        if (files == null)
            return;
        // Sort by ascendant filename
        String[] fileNames = new String[files.length];
        int i = 0;
        for (FTPFile file : files)
            fileNames[i++] = file.getName();
        Arrays.sort(fileNames);
        int ignored = 0;
        int loaded = 0;
        boolean bAlreadyTruncated = false;
        for (String fileName : fileNames) {
            String filePathName = FilenameUtils.concat(path, fileName);
            if (pattern != null)
                if (!pattern.matcher(fileName).find()) {
                    ignored++;
                    continue;
                }
            if (truncateWhenFilesFound && !bAlreadyTruncated) {
                client.deleteAll();
                bAlreadyTruncated = true;
            }
            taskLog.setInfo("Working on: " + filePathName);
            inputStream = ftp.retrieveFileStream(filePathName);
            Node xmlDoc = null;
            if (xsl != null && xsl.length() > 0) {
                xmlTempResult = File.createTempFile("ossftpfeed", ".xml");
                DomUtils.xslt(new StreamSource(inputStream), xsl, xmlTempResult);
                xmlDoc = DomUtils.readXml(new StreamSource(xmlTempResult), false);
            } else
                xmlDoc = DomUtils.readXml(new StreamSource(inputStream), false);
            client.updateXmlDocuments(xmlDoc, bufferSize, null, httpDownloader, taskLog);
            client.deleteXmlDocuments(xmlDoc, bufferSize, taskLog);
            inputStream.close();
            inputStream = null;
            if (!ftp.completePendingCommand())
                throw new SearchLibException("FTP Error");
            if (xmlTempResult != null) {
                xmlTempResult.delete();
                xmlTempResult = null;
            }
            checkConnect(ftp, server, login, password);
            if (deleteAfterLoad)
                ftp.deleteFile(filePathName);
            loaded++;
        }
        taskLog.setInfo(loaded + " file(s) loaded - " + ignored + " file(s) ignored");
    } catch (XPathExpressionException e) {
        throw new SearchLibException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SearchLibException(e);
    } catch (ParserConfigurationException e) {
        throw new SearchLibException(e);
    } catch (SAXException e) {
        throw new SearchLibException(e);
    } catch (IOException e) {
        throw new SearchLibException(e);
    } catch (URISyntaxException e) {
        throw new SearchLibException(e);
    } catch (InstantiationException e) {
        throw new SearchLibException(e);
    } catch (IllegalAccessException e) {
        throw new SearchLibException(e);
    } catch (ClassNotFoundException e) {
        throw new SearchLibException(e);
    } catch (TransformerException e) {
        throw new SearchLibException(e);
    } finally {
        if (xmlTempResult != null)
            xmlTempResult.delete();
        IOUtils.close(inputStream);
        try {
            if (ftp != null)
                if (ftp.isConnected())
                    ftp.disconnect();
        } catch (IOException e) {
            Logging.warn(e);
        }
        if (httpDownloader != null)
            httpDownloader.release();
    }
}

From source file:com.wheelermarine.android.publicAccesses.Updater.java

@Override
protected Integer doInBackground(URL... urls) {

    try {/*from w  w w  .jav  a2  s .co m*/
        final DatabaseHelper db = new DatabaseHelper(context);

        SQLiteDatabase database = db.getWritableDatabase();
        if (database == null)
            throw new IllegalStateException("Unable to open database!");

        database.beginTransaction();
        try {
            // Clear out the old data.
            database.delete(DatabaseHelper.PublicAccessEntry.TABLE_NAME, null, null);

            // Connect to the web server and locate the FTP download link.
            Log.v(TAG, "Finding update: " + urls[0]);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progress.setMessage("Locating update...");
                    progress.setIndeterminate(true);
                }
            });
            Document doc = Jsoup.connect(urls[0].toString()).timeout(timeout * 1000).userAgent(userAgent).get();
            URL dataURL = null;
            for (Element element : doc.select("a")) {
                if (element.hasAttr("href") && element.attr("href").startsWith("ftp://ftp.dnr.state.mn.us")) {
                    dataURL = new URL(element.attr("href"));
                }
            }

            // Make sure the download URL was fund.
            if (dataURL == null)
                throw new FileNotFoundException("Unable to locate data URL.");

            // Connect to the FTP server and download the update.
            Log.v(TAG, "Downloading update: " + dataURL);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progress.setMessage("Downloading update...");
                    progress.setIndeterminate(true);
                }
            });
            FTPClient ftp = new FTPClient();
            try {
                ftp.setConnectTimeout(timeout * 1000);
                ftp.setDefaultTimeout(timeout * 1000);
                ftp.connect(dataURL.getHost());
                ftp.enterLocalPassiveMode();

                // After connection attempt, you should check the reply code
                // to verify success.
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    ftp.disconnect();
                    throw new IOException("FTP server refused connection: " + ftp.getReplyString());
                }

                // Login using the standard anonymous credentials.
                if (!ftp.login("anonymous", "anonymous")) {
                    ftp.disconnect();
                    throw new IOException("FTP Error: " + ftp.getReplyString());
                }

                Map<Integer, Location> locations = null;

                // Download the ZIP archive.
                Log.v(TAG, "Downloading: " + dataURL.getFile());
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                InputStream in = ftp.retrieveFileStream(dataURL.getFile());
                if (in == null)
                    throw new FileNotFoundException(dataURL.getFile() + " was not found!");
                try {
                    ZipInputStream zin = new ZipInputStream(in);
                    try {
                        // Locate the .dbf entry in the ZIP archive.
                        ZipEntry entry;
                        while ((entry = zin.getNextEntry()) != null) {
                            if (entry.getName().endsWith(entryName)) {
                                readDBaseFile(zin, database);
                            } else if (entry.getName().endsWith(shapeEntryName)) {
                                locations = readShapeFile(zin);
                            }
                        }
                    } finally {
                        try {
                            zin.close();
                        } catch (Exception e) {
                            // Ignore this error.
                        }
                    }
                } finally {
                    in.close();
                }

                if (locations != null) {
                    final int recordCount = locations.size();
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progress.setIndeterminate(false);
                            progress.setMessage("Updating locations...");
                            progress.setMax(recordCount);
                        }
                    });

                    int progress = 0;
                    for (int recordNumber : locations.keySet()) {
                        PublicAccess access = db.getPublicAccessByRecordNumber(recordNumber);
                        Location loc = locations.get(recordNumber);
                        access.setLatitude(loc.getLatitude());
                        access.setLongitude(loc.getLongitude());
                        db.updatePublicAccess(access);
                        publishProgress(++progress);
                    }
                }
            } finally {
                if (ftp.isConnected())
                    ftp.disconnect();
            }
            database.setTransactionSuccessful();
            return db.getPublicAccessesCount();
        } finally {
            database.endTransaction();
        }
    } catch (Exception e) {
        error = e;
        Log.e(TAG, "Error loading data: " + e.getLocalizedMessage(), e);
        return -1;
    }
}

From source file:com.cladonia.xngreditor.URLUtilities.java

public static InputStream open(URL url) throws IOException {
    //        System.out.println( "URLUtilities.open( "+url+")");
    InputStream stream = null;//from  w  w  w  . j  av  a  2  s. c  om

    String password = URLUtilities.getPassword(url);
    String username = URLUtilities.getUsername(url);

    String protocol = url.getProtocol();

    if (protocol.equals("http") || protocol.equals("https")) {
        try {
            DefaultAuthenticator authenticator = Main.getDefaultAuthenticator();

            URL newURL = new URL(URLUtilities.toString(url));

            if (authenticator != null && password != null && username != null) {
                authenticator.setPasswordAuthentication(
                        new PasswordAuthentication(username, password.toCharArray()));
            }

            stream = newURL.openStream();

            if (authenticator != null && password != null && username != null) {
                authenticator.setPasswordAuthentication(null);
            }
        } catch (Exception e) {
            //            System.out.println( "Could not use normal http connection, because of:\n"+e.getMessage());
            // try it with webdav
            WebdavResource webdav = createWebdavResource(toString(url), username, password);

            stream = webdav.getMethodData(toString(url));

            webdav.close();
        }
    } else if (protocol.equals("ftp")) {
        FTPClient client = null;
        String host = url.getHost();

        try {
            //               System.out.println( "Connecting to: "+host+" ...");

            client = new FTPClient();
            client.connect(host);

            //               System.out.println( "Connected.");

            // After connection attempt, you should check the reply code to verify
            // success.
            int reply = client.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                //                  System.out.println( "Could not connect.");
                client.disconnect();
                throw new IOException("FTP Server \"" + host + "\" refused connection.");
            }

            //               System.out.println( "Logging in using: "+username+", "+password+" ...");

            if (!client.login(username, password)) {
                //                  System.out.println( "Could not log in.");
                // TODO bring up login dialog?
                client.disconnect();

                throw new IOException("Could not login to FTP Server \"" + host + "\".");
            }

            //               System.out.println( "Logged in.");

            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.enterLocalPassiveMode();

            //               System.out.println( "Opening file \""+url.getFile()+"\" ...");

            stream = client.retrieveFileStream(url.getFile());
            //               System.out.println( "File opened.");

            //               System.out.println( "Disconnecting ...");
            client.disconnect();
            //               System.out.println( "Disconnected.");

        } catch (IOException e) {
            if (client.isConnected()) {
                try {
                    client.disconnect();
                } catch (IOException f) {
                    // do nothing
                    e.printStackTrace();
                }
            }

            throw new IOException("Could not connect to FTP Server \"" + host + "\".");
        }

    } else if (protocol.equals("file")) {
        stream = url.openStream();
    } else {

        //unknown protocol but try anyways
        try {
            stream = url.openStream();
        } catch (IOException ioe) {

            throw new IOException("The \"" + protocol + "\" protocol is not supported.");
        }
    }

    return stream;
}

From source file:fr.bmartel.speedtest.SpeedTestTask.java

/**
 * start FTP download with specific port, user, password.
 *
 * @param hostname ftp host//w  w  w  .  java2  s  .  c  o m
 * @param uri      ftp uri
 * @param user     ftp username
 * @param password ftp password
 */
public void startFtpDownload(final String hostname, final int port, final String uri, final String user,
        final String password) {

    mSpeedTestMode = SpeedTestMode.DOWNLOAD;

    mErrorDispatched = false;
    mForceCloseSocket = false;

    if (mReadExecutorService == null || mReadExecutorService.isShutdown()) {
        mReadExecutorService = Executors.newSingleThreadExecutor();
    }

    mReadExecutorService.execute(new Runnable() {

        @Override
        public void run() {

            final FTPClient ftpclient = new FTPClient();

            try {
                ftpclient.connect(hostname, port);
                ftpclient.login(user, password);
                ftpclient.enterLocalPassiveMode();
                ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

                mDownloadTemporaryPacketSize = 0;

                mTimeStart = System.currentTimeMillis();
                mTimeEnd = 0;

                if (mRepeatWrapper.isFirstDownload()) {
                    mRepeatWrapper.setFirstDownloadRepeat(false);
                    mRepeatWrapper.setStartDate(mTimeStart);
                }

                mDownloadPckSize = new BigDecimal(getFileSize(ftpclient, uri));

                if (mRepeatWrapper.isRepeatDownload()) {
                    mRepeatWrapper.updatePacketSize(mDownloadPckSize);
                }

                mFtpInputstream = ftpclient.retrieveFileStream(uri);

                if (mFtpInputstream != null) {

                    final byte[] bytesArray = new byte[SpeedTestConst.READ_BUFFER_SIZE];

                    int read;
                    while ((read = mFtpInputstream.read(bytesArray)) != -1) {
                        mDownloadTemporaryPacketSize += read;

                        if (mRepeatWrapper.isRepeatDownload()) {
                            mRepeatWrapper.updateTempPacketSize(read);
                        }

                        if (!mReportInterval) {
                            final SpeedTestReport report = mSocketInterface.getLiveDownloadReport();

                            for (int i = 0; i < mListenerList.size(); i++) {
                                mListenerList.get(i).onDownloadProgress(report.getProgressPercent(), report);
                            }
                        }

                        if (mDownloadTemporaryPacketSize == mDownloadPckSize.longValueExact()) {
                            break;
                        }
                    }

                    mFtpInputstream.close();

                    mTimeEnd = System.currentTimeMillis();

                    mReportInterval = false;
                    final SpeedTestReport report = mSocketInterface.getLiveDownloadReport();

                    for (int i = 0; i < mListenerList.size(); i++) {
                        mListenerList.get(i).onDownloadFinished(report);
                    }

                } else {

                    mReportInterval = false;
                    SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, true, "cant create stream "
                            + "from uri " + uri + " with reply code : " + ftpclient.getReplyCode());
                }

                if (!mRepeatWrapper.isRepeatDownload()) {
                    closeExecutors();
                }

            } catch (IOException e) {
                //e.printStackTrace();
                mReportInterval = false;
                catchError(true, e.getMessage());
            } finally {
                mErrorDispatched = false;
                mSpeedTestMode = SpeedTestMode.NONE;
                disconnectFtp(ftpclient);
            }
        }
    });
}