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

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

Introduction

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

Prototype

public OutputStream appendFileStream(String remote) throws IOException 

Source Link

Document

Returns an OutputStream through which data can be written to append to a file on the server with the given name.

Usage

From source file:GridFDock.DataDistribute.java

public Status uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize)
        throws IOException {

    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    boolean tmp2 = true;
    Status result = null;//from ww w.  j a v  a 2  s. c  o  m
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    while (tmp2) {
        RandomAccessFile raf = new RandomAccessFile(localFile, "r");
        OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes(CODING_1), CODING_2));

        if (remoteSize > 0) {
            ftpClient.setRestartOffset(remoteSize);
            process = remoteSize / step;
            raf.seek(remoteSize);
            localreadbytes = remoteSize;
        }
        byte[] bytes = new byte[1024];
        int c;
        while ((c = raf.read(bytes)) != -1) {
            out.write(bytes, 0, c);
            localreadbytes += c;
            if (localreadbytes / step != process) {
                process = localreadbytes / step;
                // System.out.println("Upload Progress" + process);

            }
        }
        out.flush();
        raf.close();
        out.close();
        boolean judge = ftpClient.completePendingCommand();

        if (judge) {
            result = Status.Upload_From_Break_Success;
            tmp2 = false;
        } else {
            result = Status.Upload_New_File_Failed;
        }
    }
    return result;
}

From source file:co.nubetech.hiho.mapreduce.lib.output.FTPTextOutputFormat.java

@Override
public RecordWriter<K, V> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {

    Configuration conf = job.getConfiguration();

    String ip = conf.get(HIHOConf.FTP_ADDRESS);
    String portno = conf.get(HIHOConf.FTP_PORT);
    String usr = conf.get(HIHOConf.FTP_USER);
    String pwd = conf.get(HIHOConf.FTP_PASSWORD);
    String dir = getOutputPath(job).toString();
    System.out.println("\n\ninside ftpoutputformat" + ip + " " + portno + " " + usr + " " + pwd + " " + dir);
    String keyValueSeparator = conf.get("mapred.textoutputformat.separator", "\t");
    FTPClient f = new FTPClient();
    f.connect(ip, Integer.parseInt(portno));
    f.login(usr, pwd);/*from  w  ww.  j ava  2  s .  c om*/
    f.changeWorkingDirectory(dir);
    f.setFileType(FTP.BINARY_FILE_TYPE);

    boolean isCompressed = getCompressOutput(job);
    CompressionCodec codec = null;
    String extension = "";
    if (isCompressed) {
        Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class);
        codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
        extension = codec.getDefaultExtension();
    }
    Path file = getDefaultWorkFile(job, extension);
    FileSystem fs = file.getFileSystem(conf);
    String filename = file.getName();
    if (!isCompressed) {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(fileOut, new String(keyValueSeparator), f);

    } else {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(new DataOutputStream(codec.createOutputStream(fileOut)),
                keyValueSeparator, f);
    }
}

From source file:com.clican.pluto.cms.core.service.impl.IssueServiceImpl.java

private boolean issue(ISite site, ITemplate template, IDataModel dataModel, String relativePath,
        Map<ISite, FTPClient> ftpMap) {
    Template t = null;//from ww w .ja  va 2 s .  co m
    Writer w = null;
    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("this", dataModel);
    FTPClient client = null;
    try {
        SimpleNode node = RuntimeSingleton.getRuntimeServices().parse(template.getContent(),
                template.getName());
        t = new Template();
        t.setName(template.getName());
        t.setRuntimeServices(RuntimeSingleton.getRuntimeServices());
        t.setData(node);
        t.initDocument();
        OutputStream os = null;
        if (site.getUrl().startsWith("ftp://")) {
            client = null;
            if (ftpMap != null) {
                client = ftpMap.get(site);
            }
            if (client == null) {
                client = siteService.getFTPClient(site);
            }
            if (client == null) {
                log.error("This site[" + site.getName() + "] is unavailable");
                return false;
            }
            if (!relativePath.endsWith("/")) {
                relativePath = relativePath + "/";
            }
            client.mkd(relativePath);
            os = client.appendFileStream(relativePath + dataModel.getName() + "." + template.getSuffix());
        } else if (site.getUrl().startsWith("file://")) {
            String filePath = site.getUrl().substring(7);
            File file = new File(filePath + relativePath);
            if (file.exists()) {
                file.mkdirs();
            }
            os = new FileOutputStream(new File(file, dataModel.getName() + "." + template.getSuffix()));
        } else {
            throw new UnknownHostException(site.getUrl());
        }
        w = new OutputStreamWriter(os, "utf-8");
        t.merge(velocityContext, w);
        w.flush();
        return true;
    } catch (Exception e) {
        log.error("", e);
        return false;
    } finally {
        try {
            if (w != null) {
                w.close();
            }
        } catch (Exception e) {
            log.error("", e);
        }
        try {
            if (ftpMap == null && client != null) {
                client.logout();
                client.disconnect();
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }
}

From source file:nl.esciencecenter.xenon.adaptors.filesystems.ftp.FtpFileSystem.java

@Override
public OutputStream appendToFile(Path path) throws XenonException {
    LOGGER.debug("appendToFile path = {}", path);

    assertIsOpen();//from ww w .  j a  v a2  s.  c  o m
    Path absPath = toAbsolutePath(path);
    assertPathExists(absPath);
    assertPathIsNotDirectory(absPath);

    try {
        // Since FTP connections can only do a single thing a time, we need
        // a new FTPClient to handle the stream.
        FTPClient newClient = adaptor.connect(getLocation(), credential);
        newClient.enterLocalPassiveMode();
        OutputStream out = newClient.appendFileStream(absPath.toString());

        if (out == null) {
            checkClientReply("Failed to append to path: " + absPath.toString());
        }

        return new TransferClientOutputStream(out, new CloseableClient(newClient));
    } catch (IOException e) {
        throw new XenonException(ADAPTOR_NAME, "Failed to append to path: " + absPath);
    }
}

From source file:org.jumpmind.metl.core.runtime.resource.FtpDirectory.java

@Override
public OutputStream getOutputStream(final String relativePath, boolean mustExist) {
    FTPClient ftpClient = null;
    try {//from   w  w  w.  java2  s  . c om
        ftpClient = createClient();
        return new CloseableOutputStream(ftpClient.appendFileStream(relativePath), ftpClient);
    } catch (Exception e) {
        close(ftpClient);
        throw new IoException(e);
    }
}