net.shopxx.plugin.ftpStorage.FtpStoragePlugin.java Source code

Java tutorial

Introduction

Here is the source code for net.shopxx.plugin.ftpStorage.FtpStoragePlugin.java

Source

/*
 * Copyright 2005-2015 shopxx.net. All rights reserved.
 * Support: http://3936242.01p.com/
 * License: http://3936242.01p.com/license
 */
package net.shopxx.plugin.ftpStorage;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import net.shopxx.entity.PluginConfig;
import net.shopxx.plugin.StoragePlugin;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;

@Component("ftpStoragePlugin")
public class FtpStoragePlugin extends StoragePlugin {

    @Override
    public String getName() {
        return "FTP";
    }

    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public String getAuthor() {
        return "xiaohe";
    }

    @Override
    public String getSiteUrl() {
        return "http://3936242.01p.com/";
    }

    @Override
    public String getInstallUrl() {
        return "ftp_storage/install.jhtml";
    }

    @Override
    public String getUninstallUrl() {
        return "ftp_storage/uninstall.jhtml";
    }

    @Override
    public String getSettingUrl() {
        return "ftp_storage/setting.jhtml";
    }

    @Override
    public void upload(String path, File file, String contentType) {
        PluginConfig pluginConfig = getPluginConfig();
        if (pluginConfig != null) {
            String host = pluginConfig.getAttribute("host");
            Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
            String username = pluginConfig.getAttribute("username");
            String password = pluginConfig.getAttribute("password");
            FTPClient ftpClient = new FTPClient();
            InputStream inputStream = null;
            try {
                inputStream = new BufferedInputStream(new FileInputStream(file));
                ftpClient.connect(host, port);
                ftpClient.login(username, password);
                ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                    String directory = StringUtils.substringBeforeLast(path, "/");
                    String filename = StringUtils.substringAfterLast(path, "/");
                    if (!ftpClient.changeWorkingDirectory(directory)) {
                        String[] paths = StringUtils.split(directory, "/");
                        String p = "/";
                        ftpClient.changeWorkingDirectory(p);
                        for (String s : paths) {
                            p += s + "/";
                            if (!ftpClient.changeWorkingDirectory(p)) {
                                ftpClient.makeDirectory(s);
                                ftpClient.changeWorkingDirectory(p);
                            }
                        }
                    }
                    ftpClient.storeFile(filename, inputStream);
                    ftpClient.logout();
                }
            } catch (SocketException e) {
                throw new RuntimeException(e.getMessage(), e);
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            } finally {
                IOUtils.closeQuietly(inputStream);
                try {
                    if (ftpClient.isConnected()) {
                        ftpClient.disconnect();
                    }
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String getUrl(String path) {
        PluginConfig pluginConfig = getPluginConfig();
        if (pluginConfig != null) {
            String urlPrefix = pluginConfig.getAttribute("urlPrefix");
            return urlPrefix + path;
        }
        return null;
    }

}