java ftp上传文件
PHPz
PHPz 2017-04-18 09:05:29
0
1
438

用 java 上传 ftp 文件如果是在 window 环境执行,就可以上传成功,但将同样的代码部署到 linux 上传就不成功,但也没有报错

boolean b = ftp.storeFile(new String(file2.getName().getBytes("UTF-8"),"iso-8859-1"), input);

windows 执行的时候 b = true 上传成功
linux 同样的代码 执行时 b = false

下面是完整的代码 ,这和操作系统有关系吗?

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FtpClientUtil {

    private static FTPClient ftp;
    private static final Log LOG = LogFactory.getLog(FtpClientUtil.class);

    static {
        String username = AppConfig.get("ftp.information.username").toString();
        String password = AppConfig.get("ftp.information.password").toString();
        String server = AppConfig.get("ftp.information.server").toString();
        String port = AppConfig.get("ftp.information.port").toString();
        String path = AppConfig.get("ftp.information.path").toString();
        try {
//            LOG.info("path\t" + path);
//            LOG.info("password\t" + password);
//            LOG.info("server\t" + server);
//            LOG.info("port\t" + port);
//            LOG.info("path\t" + path);
            connect(path, server, Integer.parseInt(port), username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param path 上传到ftp服务器哪个路径下
     * @param addr 地址
     * @param port 端口号
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws Exception
     */
    public static boolean connect(String path,String addr,int port,String username,String password) throws Exception {
        boolean result = false;
        ftp = new FTPClient();
        int reply;
        ftp.connect(addr,port);
        ftp.login(username,password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
//        ftp.enterLocalPassiveMode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            LOG.info("-------------------------1");
            return result;
        }
        LOG.info("-------------------------2");
        ftp.changeWorkingDirectory(path);
        result = true;
        return result;
    }
    /**
     *
     * @param file 上传的文件或文件夹
     * @throws Exception
     */
    public static void upload(File file) throws Exception{
        if(file.isDirectory()){
            ftp.makeDirectory(file.getName());
            ftp.changeWorkingDirectory(file.getName());
            String[] files = file.list();
            for (int i = 0; i < files.length; i++) {
                File file1 = new File(file.getPath()+"\\"+files[i] );
                if(file1.isDirectory()){
                    upload(file1);
                    ftp.changeToParentDirectory();
                }else{
                    File file2 = new File(file.getPath()+"\\"+files[i]);
                    FileInputStream input = new FileInputStream(file2);
                    ftp.storeFile(file2.getName(), input);
                    input.close();
                }
            }
        }else{
//            LOG.info(file.getPath());
            File file2 = new File(file.getPath());
            FileInputStream input = new FileInputStream(file2);
            LOG.info(input);
            ftp.setControlEncoding("UTF-8");


            boolean b = ftp.storeFile(new String(file2.getName().getBytes("UTF-8"),"iso-8859-1"), input);
//            boolean b = ftp.storeFile(file.getPath(), input);
            ftp.enterLocalPassiveMode();
            LOG.info("storeFile:\t" + b);
            input.close();
        }
    }

    public static void main(String[] args) throws Exception{
        FtpClientUtil.upload(new File("C:/Users/Jilinwula/Desktop/bing/update_wfportal_source_20160604.zip"));
    }
}
PHPz
PHPz

学习是最好的投资!

reply all(1)
阿神

Pay attention to the path separator, it is inconsistent between windows and linux, which will cause the file to not be found!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template