在Android中使用FTP将图片上传到服务器

时间:2019-07-10 13:13:16

标签: android kotlin file-upload ftp

目前,我正在使用Android中的FTP将图片上传到服务器。但是,每当我连接ftp时,它将轻松连接到服务器。图像也成功上传到服务器,但是主要问题是图像大小为0kb。从字面上看,卡在了这里,无法在服务器上获得正确的映像。任何帮助都将是可贵的。

以下是我的代码:-

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import java.io.File;
import java.io.FileInputStream;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import org.apache.commons.net.ftp.FTPReply;


public class FtpUpload extends Activity implements OnClickListener {


/*********  work only for Dedicated IP ***********/
static final String FTP_HOST= "**.**.***.**";

/*********  FTP USERNAME ***********/
static final String FTP_USER = "***********";

/*********  FTP PASSWORD ***********/
static final String FTP_PASS  ="***************";

Button btnUpload;
public FTPClient mFTPClient = null; // Add top of the class


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ftp);

    btnUpload = (Button) findViewById(R.id.btnUpload);
    btnUpload.setOnClickListener(this);

//        ftpclient = new MyFTPClientFunctions();
//        new Thread(new Runnable() {
//            public void run() {
//                boolean status = false;
//                // host – your FTP address
//                // username & password – for your secured login
//                // 21 default gateway for FTP
//                status = ftpclient.ftpConnect(FTP_HOST, FTP_USER, 
FTP_PASS, 21);
//                if (status == true) {
//                    Log.d("", "Connection Success");
//                } else {
//                    Log.d("", "Connection failed");
//                }
//            }
//        }).start();

    }

public void onClick(View v) {

    /********** Pick file from sdcard *******/
    File f = new File("/sdcard/logo.png");

    // Upload sdcard file
    uploadFile(f);

}


 /* public boolean ftpConnect(String host, String username, String password, int port) {
    try {
        mFTPClient = new FTPClient();
        // connecting to the host
        mFTPClient.connect(host, port);
        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
            // login using username & password
            boolean status = mFTPClient.login(username, password);
            *//*
             * Set File Transfer Mode
             * To avoid corruption issue you must specified a correct
             * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
             * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
             * transferring text, image, and compressed files.
             *//*
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();
            return status;
        }
    } catch (Exception e) {
        Log.d("", "Error: could not connect to host " + host);
    }
    return false;
}


public boolean ftpDisconnect() {
    try {
        mFTPClient.logout();
        mFTPClient.disconnect();
        return true;
    } catch (Exception e) {
        Log.d(";", "Error occurred while disconnecting from ftp server.");
    }
    return false;
}


public boolean ftpUpload(String srcFilePath, String desFileName, String desDirectory, Context context) {
    boolean status = false;
    try {
        FileInputStream srcFileStream = new FileInputStream(srcFilePath);
        // change working directory to the destination directory
        // if (ftpChangeDirectory(desDirectory)) {
        status = mFTPClient.storeFile(desFileName, srcFileStream);
        // }
        srcFileStream.close();
        return status;
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("", "upload failed: " + e);
    }
    return status;
}*/


public void uploadFile(File fileName) {


    FTPClient client = new FTPClient();

    try {

        client.connect(FTP_HOST,21);
        client.login(FTP_USER, FTP_PASS);
        client.setType(FTPClient.TYPE_BINARY);
        client.changeDirectory("/upload/");

        client.upload(fileName, new MyTransferListener());

    } catch (Exception e) {
        e.printStackTrace();
        try {
            client.disconnect(true);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

}

/*******  Used to file upload and show progress  **********/

public class MyTransferListener implements FTPDataTransferListener {

    public void started() {

        btnUpload.setVisibility(View.GONE);
        // Transfer started
        Toast.makeText(getBaseContext(), " Upload Started ...", Toast.LENGTH_SHORT).show();
        //System.out.println(" Upload Started ...");
    }

    public void transferred(int length) {

        // Yet other length bytes has been transferred since the last time this
        // method was called
        Toast.makeText(getBaseContext(), " transferred ..." + length, Toast.LENGTH_SHORT).show();
        //System.out.println(" transferred ..." + length);
    }

    public void completed() {

        btnUpload.setVisibility(View.VISIBLE);
        // Transfer completed

        Toast.makeText(getBaseContext(), " completed ...", Toast.LENGTH_SHORT).show();
        //System.out.println(" completed ..." );
    }

    public void aborted() {

        btnUpload.setVisibility(View.VISIBLE);
        // Transfer aborted
        Toast.makeText(getBaseContext()," transfer aborted, please try again...", Toast.LENGTH_SHORT).show();
        //System.out.println(" aborted ..." );
    }

    public void failed() {

        btnUpload.setVisibility(View.VISIBLE);
        // Transfer failed
        System.out.println(" failed ..." );
    }
  }
}

0 个答案:

没有答案