简体   繁体   English

Apache Commons Ftp文件上传了0个字节

[英]Apache Commons Ftp file uploaded 0 bytes

I'm developing an application that needs to upload something to an FTP. 我正在开发一个需要将某些内容上传到FTP的应用程序。 I've been unable to make apache commons works with file upload. 我一直无法使apache commons可用于文件上传。 The file uploaded has 0 bytes. 上传的文件有0个字节。 I've tryed ASCII and binary mode with the same results. 我尝试过ASCII和二进制模式,结果相同。 Here is my code 这是我的代码

    Log.d("FTP", "Connecting");
    ftpsClient.connect(server,port);


    if(ftpsClient.isConnected())
    {
        Log.d("FTP", "Sending File");
        ftpsClient.login("XXXXXXX", "XXXXXXXX");
        ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
        InputStream is = new FileInputStream("sdcard/log.txt");
        ftpsClient.storeFile("log.txt", is);
        is.close();
        ftpsClient.logout();
        ftpsClient.disconnect();
    }

What I'm doing wrong? 我做错了什么?

Been there, done that, saw the exact same problem. 到那边去,看到了完全一样的问题。

After

ftpsClient.login("XXXXXXX", "XXXXXXXX");

add

ftpsClient.enterLocalPassiveMode();

And to expand on the answer a little bit, here's a complete snippet with progress: 为了进一步扩展答案,下面是一个完整的进度片段:

private class FTPUploadTask extends AsyncTask<AFileDescriptor, Long, Boolean> {

    private static final int bufferSize = 64;
    private boolean isCanceling = false;
    private long fileSize = 0;

    private ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        pd = new ProgressDialog( ThisActivity.this );
        pd.setCancelable( false );
        pd.setMessage( "Uploading something..." );
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setProgress(0);
        pd.setMax(100);
        pd.show();
    }

    @Override
    protected void onProgressUpdate(Long... progress) {
        pd.setProgress( (int)( progress[0] * 100 / fileSize ) );
     }

    private int storeFile( OutputStream out, String local ) {
        try {
            InputStream in = new FileInputStream( local );

            byte[] buffer = new byte[ bufferSize * 1024 ];
            int read;
            int allRead = 0;
            while( ( ( read = in.read( buffer ) ) != - 1 ) && !isCanceling ) {
                allRead += read;
                this.publishProgress( (long) allRead );
                out.write( buffer, 0, read );
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            return allRead;
        } catch( Throwable th ) {
            Log.d( "Error: ", th.getMessage() );
            return -1;
        }
    }

    @Override
    protected Boolean doInBackground( AFileDescriptor... params ) {
        ASubFileDescriptor file = params[0].getDefaultFile();
        FTPClient ftp = new FTPClient();
        try {
            ftp.connect( FTPHostName );
            ftp.login( FTPUser, FTPPass );
            ftp.cwd( FTPTargetFolder );

            ftp.enterLocalPassiveMode();

                            String storeName = file.getStoreName();
            ftp.setFileType( FTP.BINARY_FILE_TYPE );
            this.fileSize =  file.getFile().length();
            OutputStream out = ftp.storeFileStream( storeName );
            int bytes = storeFile( out,
                       file.getFile().getAbsolutePath() );

            ftp.logout();
            ftp.disconnect();
            return bytes > 0;
        } catch( Throwable th ) {
            Log.d( "FTP Error:", th.getMessage() );
            return false;
        }
    }

    @Override
    protected void onPostExecute( Boolean success ) {
        try {
            pd.dismiss();
        } catch( Throwable th ) {

        }

        if( success ) {
                        // do something
        } else {
                        // do something else
    }
}

This snippet is used to upload certain debug information to a local server for level-2 support. 此代码段用于将某些调试信息上载到本地服务器以获得2级支持。 Depending on how you use your upload, you may have to add more error-checking/recovery and user-interaction. 根据您使用上传的方式,您可能必须添加更多的错误检查/恢复和用户交互功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM