Search in sources :

Example 11 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project azure-sdk-for-java by Azure.

the class Utils method uploadFileToFtp.

/**
     * Uploads a file to an Azure web app.
     * @param profile the publishing profile for the web app.
     * @param fileName the name of the file on server
     * @param file the local file
     */
public static void uploadFileToFtp(PublishingProfile profile, String fileName, InputStream file) {
    FTPClient ftpClient = new FTPClient();
    String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
    String server = ftpUrlSegments[0];
    String path = "./site/wwwroot/webapps";
    if (fileName.contains("/")) {
        int lastslash = fileName.lastIndexOf('/');
        path = path + "/" + fileName.substring(0, lastslash);
        fileName = fileName.substring(lastslash);
    }
    try {
        ftpClient.connect(server);
        ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory(path);
        ftpClient.storeFile(fileName, file);
        ftpClient.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ConnectionString(com.microsoft.azure.management.appservice.ConnectionString) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) TrafficManagerExternalEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerExternalEndpoint) TrafficManagerNestedProfileEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerNestedProfileEndpoint) TrafficManagerAzureEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerAzureEndpoint)

Example 12 with FTPClient

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

the class MMediaServer method deploy.

//	MMediaServer
/**
	 * 	(Re-)Deploy all media
	 * 	@param media array of media to deploy
	 * 	@return true if deployed
	 */
public boolean deploy(MMedia[] media) {
    // Check whether the host is our example localhost, we will not deploy locally, but this is no error
    if (this.getIP_Address().equals("127.0.0.1") || this.getName().equals("localhost")) {
        log.warning("You have not defined your own server, we will not really deploy to localhost!");
        return true;
    }
    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(getIP_Address());
        if (ftp.login(getUserName(), getPassword()))
            log.info("Connected to " + getIP_Address() + " as " + getUserName());
        else {
            log.warning("Could NOT connect to " + getIP_Address() + " as " + getUserName());
            return false;
        }
    } catch (Exception e) {
        log.log(Level.WARNING, "Could NOT connect to " + getIP_Address() + " as " + getUserName(), e);
        return false;
    }
    boolean success = true;
    String cmd = null;
    //	List the files in the directory
    try {
        cmd = "cwd";
        ftp.changeWorkingDirectory(getFolder());
        //
        cmd = "list";
        String[] fileNames = ftp.listNames();
        log.log(Level.FINE, "Number of files in " + getFolder() + ": " + fileNames.length);
        /*
			FTPFile[] files = ftp.listFiles();
			log.config("Number of files in " + getFolder() + ": " + files.length);
			for (int i = 0; i < files.length; i++)
				log.fine(files[i].getTimestamp() + " \t" + files[i].getName());*/
        //
        cmd = "bin";
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        //
        for (int i = 0; i < media.length; i++) {
            if (!media[i].isSummary()) {
                log.log(Level.INFO, " Deploying Media Item:" + media[i].get_ID() + media[i].getExtension());
                MImage thisImage = media[i].getImage();
                // Open the file and output streams
                byte[] buffer = thisImage.getData();
                ByteArrayInputStream is = new ByteArrayInputStream(buffer);
                String fileName = media[i].get_ID() + media[i].getExtension();
                cmd = "put " + fileName;
                ftp.storeFile(fileName, is);
                is.close();
            }
        }
    } catch (Exception e) {
        log.log(Level.WARNING, cmd, e);
        success = false;
    }
    //	Logout from the FTP Server and disconnect
    try {
        cmd = "logout";
        ftp.logout();
        cmd = "disconnect";
        ftp.disconnect();
    } catch (Exception e) {
        log.log(Level.WARNING, cmd, e);
    }
    ftp = null;
    return success;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 13 with FTPClient

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

the class Ftp method run.

/**
   * Run a thread to transfer files
   */
public void run() {
    byte[] data = null;
    Timer timer = new Timer();
    FTPClient ftp = this.ftp;
    if (currentThreadCnt.getAndIncrement() > 0) {
        ftp = openConnection(null);
    }
    while (true) {
        String file = filesQueue.poll();
        if (file == null) {
            break;
        }
        int num = currentFileCnt.getAndIncrement();
        FTPFile ftpFile = filesMap.get(file);
        long ftpSizeInBytes = ftpFile.getSize();
        String fmtSizeInBytes = Utils.formatSizeInBytes(ftpSizeInBytes);
        String targetFile = getTargetFileName(file);
        if (info) {
            info(null, "  " + file + " - started (" + num + " of " + fileCnt + ", " + fmtSizeInBytes + ")");
        }
        try {
            InputStream in = ftp.retrieveFileStream(file);
            OutputStream out = null;
            java.io.File targetLocalFile = null;
            File targetHdfsFile = null;
            if (local) {
                targetLocalFile = new java.io.File(targetFile);
                if (!targetLocalFile.exists()) {
                    targetLocalFile.getParentFile().mkdirs();
                    targetLocalFile.createNewFile();
                }
                out = new FileOutputStream(targetLocalFile, false);
            } else {
                targetHdfsFile = new File();
                out = targetHdfsFile.create(targetFile, true);
            }
            if (data == null) {
                data = new byte[3 * 1024 * 1024];
            }
            int bytesRead = -1;
            long bytesReadAll = 0;
            long start = timer.start();
            long prev = start;
            long readTime = 0;
            long writeTime = 0;
            long cur, cur2, cur3;
            while (true) {
                cur = timer.current();
                bytesRead = in.read(data);
                cur2 = timer.current();
                readTime += (cur2 - cur);
                if (bytesRead == -1) {
                    break;
                }
                out.write(data, 0, bytesRead);
                out.flush();
                cur3 = timer.current();
                writeTime += (cur3 - cur2);
                bytesReadAll += bytesRead;
                if (info) {
                    cur = timer.current();
                    if (cur - prev > 13000) {
                        long elapsed = cur - start;
                        info(null, "  " + file + " - in progress (" + Utils.formatSizeInBytes(bytesReadAll) + " of " + fmtSizeInBytes + ", " + Utils.formatPercent(bytesReadAll, ftpSizeInBytes) + ", " + Utils.formatTime(elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, readTime) + " read, " + Utils.formatBytesPerSec(bytesReadAll, writeTime) + " write)");
                        prev = cur;
                    }
                }
            }
            if (ftp.completePendingCommand()) {
                in.close();
                cur = timer.current();
                out.close();
                readTime += (timer.current() - cur);
                bytesTransferredAll.addAndGet(bytesReadAll);
                fileCntSuccess.incrementAndGet();
                if (info) {
                    long elapsed = timer.stop();
                    info(null, "  " + file + " - complete (" + Utils.formatSizeInBytes(bytesReadAll) + ", " + Utils.formatTime(elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, readTime) + " read, " + Utils.formatBytesPerSec(bytesReadAll, writeTime) + " write)");
                }
            } else {
                in.close();
                out.close();
                if (info) {
                    info(null, "  " + file + " - failed");
                }
                exec.signal(Signal.Type.SQLEXCEPTION, "File transfer failed: " + file);
            }
        } catch (IOException e) {
            exec.signal(e);
        }
    }
    try {
        if (ftp.isConnected()) {
            ftp.logout();
            ftp.disconnect();
        }
    } catch (IOException e) {
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 14 with FTPClient

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

the class FTPFileSystem method create.

/**
   * A stream obtained via this call must be closed before using other APIs of
   * this class or else the invocation will block.
   */
@Override
public FSDataOutputStream create(Path file, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    final FTPClient client = connect();
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    FileStatus status;
    try {
        status = getFileStatus(client, file);
    } catch (FileNotFoundException fnfe) {
        status = null;
    }
    if (status != null) {
        if (overwrite && !status.isDirectory()) {
            delete(client, file, false);
        } else {
            disconnect(client);
            throw new FileAlreadyExistsException("File already exists: " + file);
        }
    }
    Path parent = absolute.getParent();
    if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {
        parent = (parent == null) ? new Path("/") : parent;
        disconnect(client);
        throw new IOException("create(): Mkdirs failed to create: " + parent);
    }
    client.allocate(bufferSize);
    // Change to parent directory on the server. Only then can we write to the
    // file on the server by opening up an OutputStream. As a side effect the
    // working directory on the server is changed to the parent directory of the
    // file. The FTP client connection is closed when close() is called on the
    // FSDataOutputStream.
    client.changeWorkingDirectory(parent.toUri().getPath());
    FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file.getName()), statistics) {

        @Override
        public void close() throws IOException {
            super.close();
            if (!client.isConnected()) {
                throw new FTPException("Client not connected");
            }
            boolean cmdCompleted = client.completePendingCommand();
            disconnect(client);
            if (!cmdCompleted) {
                throw new FTPException("Could not complete transfer, Reply Code - " + client.getReplyCode());
            }
        }
    };
    if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
        // The ftpClient is an inconsistent state. Must close the stream
        // which in turn will logout and disconnect from FTP server
        fos.close();
        throw new IOException("Unable to create file: " + file + ", Aborting");
    }
    return fos;
}
Also used : Path(org.apache.hadoop.fs.Path) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) FileStatus(org.apache.hadoop.fs.FileStatus) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 15 with FTPClient

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

the class FTPFileSystem method getHomeDirectory.

@Override
public Path getHomeDirectory() {
    FTPClient client = null;
    try {
        client = connect();
        Path homeDir = new Path(client.printWorkingDirectory());
        return homeDir;
    } catch (IOException ioe) {
        throw new FTPException("Failed to get home directory", ioe);
    } finally {
        try {
            disconnect(client);
        } catch (IOException ioe) {
            throw new FTPException("Failed to disconnect", ioe);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Aggregations

FTPClient (org.apache.commons.net.ftp.FTPClient)27 IOException (java.io.IOException)12 InputStream (java.io.InputStream)3 Path (org.apache.hadoop.fs.Path)3 DataXException (com.alibaba.datax.common.exception.DataXException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ConnectException (java.net.ConnectException)2 MalformedURLException (java.net.MalformedURLException)2 UnknownHostException (java.net.UnknownHostException)2 FTPFile (org.apache.commons.net.ftp.FTPFile)2 Configuration (org.apache.hadoop.conf.Configuration)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 ConnectionString (com.microsoft.azure.management.appservice.ConnectionString)1 TrafficManagerAzureEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerAzureEndpoint)1 TrafficManagerExternalEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerExternalEndpoint)1