首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能使用ftp4j上载文件

未能使用ftp4j上载文件
EN

Stack Overflow用户
提问于 2013-04-18 08:29:53
回答 1查看 2.7K关注 0票数 1

我正在使用ftp4j作为FTP客户端。

代码语言:javascript
复制
FTPClient client = new FTPClient();
client.connect("86.22.11.178");
client.login("usr", "pwd");
client.changeDirectory("/dir");
client.upload(file);

它在本地主机上工作得很好,但如果封装在部署在web服务器上的JSF web应用程序中,则无法工作。我成功地进行了连接和登录,当代码到达upload命令时,它就跳过了,什么也不做。不抛出任何例外。

FTP服务器是完全兼容的,这不会是一个问题。我还对文件设置了chmod 777权限,它们属于同一个所有者。

这段代码在Windows机器上工作,是不是Linux上运行的机器有不同的“规则”?

EN

回答 1

Stack Overflow用户

发布于 2013-04-18 09:04:35

你的代码似乎是正确的。尝试找出它抛出的FTP错误。有时候超时可能会发生,这是我面对的!

进口org.apache.commons.net.ftp.;

进口java.io.;

/** *这个类用于演示Jakarta包的使用情况*/

公共类TestFTP {

代码语言:javascript
复制
/** Creates a new instance of TestFTP */  
public TestFTP() {  
}  

/** 
 * main - Unit test program 
 * @param args Command line arguments 
 * 
 */  
public static void main(String args[]) {  
    try {  
        String ftpHost = "157.227.38.131";  
        String ftpUserName = "firebird";  
        String ftpPassword = "tcs@12345";  
        String ftpRemoteDirectory = "/etc/vlp/uploaded_files";  
        String fileToTransmit = "c:\\temp\\VLPDYN18022010174439.an";  

        //Create a Jakarta Commons Net FTP Client object  
        FTPClient ftp = new FTPClient();  

        //A datatype to store responses from the FTP server  
        int reply;  

        //  
        //Connect to the server  
        //  
        ftp.connect(ftpHost);  

        //  
        // After connection attempt, you should check the reply code to verify success.  
        //  
        reply = ftp.getReplyCode();      
        if(!FTPReply.isPositiveCompletion(reply)) {  
            try {  
                ftp.disconnect();  
            } catch (Exception e) {  
                System.err.println("Unable to disconnect from FTP server " +  
                                   "after server refused connection. "+e.toString());  
            }  
            throw new Exception ("FTP server refused connection.");  
        }                
        System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString());  

        //  
        //Try to login  
        //  
        if (!ftp.login(ftpUserName, ftpPassword)) {  
            throw new Exception ("Unable to login to FTP server " +  
                                 "using username "+ftpUserName+" " +  
                                 "and password "+ftpPassword);  
        }  

        System.out.println(ftp.getReplyString());  
        System.out.println("Remote system is " + ftp.getSystemName());  

        //  
        //Set our file transfer mode to either ASCII or Binary  
        //  
        //ftp.setFileType(FTP.ASCII_FILE_TYPE);  
        ftp.setFileType(FTP.BINARY_FILE_TYPE);  

        //  
        //Change the remote directory  
        //  
        if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) {  
            System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);  
            ftp.changeWorkingDirectory(ftpRemoteDirectory);  
            reply = ftp.getReplyCode();  

            if(!FTPReply.isPositiveCompletion(reply)) {  
                throw new Exception ("Unable to change working directory " +  
                                     "to:"+ftpRemoteDirectory);  
            }  
        }  

        //  
        //Get the file that we will transfer and send it.  
        //  
        File f = new File(fileToTransmit);  
        System.out.println("Storing file as remote filename: " + f.getName());  
        boolean retValue=true;  
       try{   
        retValue = ftp.storeFile(f.getName(), new FileInputStream(f));  
       }catch(Exception e){e.printStackTrace();}  
        if (!retValue) {  
          throw new Exception ("Storing of remote file failed. ftp.storeFile() returned false.");  
        }  

        //Disconnect from the FTP server  
        //  
        try {  
            //ftp.logout();  
            ftp.disconnect();  
        } catch (Exception exc) {  
            System.err.println("Unable to disconnect from FTP server. " + exc.toString());  
        }  

    } catch (Exception e) {  
        System.err.println("Error: "+e.toString());  
    }  

    System.out.println("Process Complete.");  
    System.exit(0);  
}      

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16078060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档