开发者社区> 问答> 正文

java ftp上传文件 报错

"

用 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"));
    }
}
" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/a2a1a078b5dc41cb9f973cc62ec2ffb6.png)

展开
收起
因为相信,所以看见。 2020-05-26 13:56:31 1174 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    "

    注意路径分隔符,windows和linux下不一致,会导致文件找不到!

    ######

    尝试调整下代码顺序

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

    " ![image.png](https://ucc.alicdn.com/pic/developer-ecology/8b6c62b249284e49afe26817abe7c5d9.png)
    2020-05-27 10:08:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载