文件没有在java上的Ftp服务器上传?

时间:2014-03-05 11:57:35

标签: java ftp apache-commons ftp-client

Hello我每个人都在工作一个项目,我需要使用我的java独立应用程序在我的ftp服务器上传文件

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class Ftpdemo {  


     public static void main(String args[]) {  

      // get an ftpClient object  
      FTPClient ftpClient = new FTPClient();  
      ftpClient.setConnectTimeout(300);
      FileInputStream inputStream = null;  

      try {  
       // pass directory path on server to connect  
       ftpClient.connect("ftp.mydomain.in");  

       // pass username and password, returned true if authentication is  
       // successful  
       boolean login = ftpClient.login("myusername", "mypassword");  

       if (login) {  
        System.out.println("Connection established...");  
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpClient.enterLocalPassiveMode();

        inputStream = new FileInputStream("/home/simmant/Desktop/mypic.png");  

        boolean uploaded = ftpClient.storeFile("user_screens/test3.png",inputStream);



              if (uploaded) {  
         System.out.println("File uploaded successfully !");  
        } else {  
         System.out.println("Error in uploading file !");  
        }  

        // logout the user, returned true if logout successfully  
        boolean logout = ftpClient.logout();  
        if (logout) {  
         System.out.println("Connection close...");  
        }  
       } else {  
        System.out.println("Connection fail...");  
       }  

      } catch (SocketException e) {  
       e.printStackTrace();  
      } catch (IOException e) {  
       e.printStackTrace();  
      } finally {  
       try {  
        ftpClient.disconnect();  
       } catch (IOException e) {  
        e.printStackTrace();  
       }  
      }  
     }


    }  

它对1或2 kb的数据工作正常,但是当我尝试上传50 kb和100 kb的文件时,它不能正常工作。上传到服务器上的图像为空白。

3 个答案:

答案 0 :(得分:2)

据我所知,代码没有任何问题。正如您所说,1或2 kb的数据上传工作正常。所以,在我看来,你的互联网存在问题。上传50 kb或更大的文件大小可能太慢了。

答案 1 :(得分:1)

你的代码只有问题与文件或网络速度有问题。在这里工作正常,并建议不要直接使用FTP详细信息请避免申请中的这些东西你有更好的选择使用Web服务为您的服务器相关的东西。 祝你好运

答案 2 :(得分:1)

您好像没有正确连接实例。 请参考我的工作代码:

public class FTPUploader {
    FTPClient ftp = null;
    public FTPUploader() throws Exception {
        ftp = new FTPClient();
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(new FileOutputStream("c:\\ftp.log"))));
        int reply;
        ftp.connect(Constant.FTP_HOST);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new Exception(Constant.FTP_SERVER_EXCEPTION);
        }
        ftp.login(CommonConstant.FTP_USERNAME, Constant.FTP_PASSWORD);
        ftp.setFileType(FTP.TELNET_TEXT_FORMAT);
        ftp.enterLocalPassiveMode();
    }
    public boolean uploadFile(File localFileFullName, String fileName) throws Exception {
        InputStream input = new FileInputStream(localFileFullName);
        boolean reply = this.ftp.storeFile("'" + fileName +"'", input);
        disconnect();
        return reply;
    }
    public void disconnect() {
        if (this.ftp.isConnected()) {
            try {
                this.ftp.logout();
                this.ftp.disconnect();
            } catch (IOException e) {
                System.out.println(this.getClass()+ e);
            }
        }
    }
    public static void main(String[] args) throws Exception {
        System.out.println("Start");
        FTPUploader ftpUploader = new FTPUploader();
        System.out.println(ftpUploader.uploadFile(new File("C:\\test.txt"), "Destinate_DIR"));
        System.out.println("Done");
    }
}

一切顺利...... !!!