在android中使用ftp4j-1.7.2.jar上传图片

时间:2016-05-08 16:09:47

标签: java android ftp

我使用ftp4j-1.7.2.jar将图片上传到ftp。但是当我尝试执行上传时,它总是显示失败。它总是在上传文件中出现异常并向我显示失败的toast。你能帮助我吗?谢谢先进

MainActivity.Java

import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;

public class MainActivity extends AppCompatActivity {
    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;

    Button btn;

    static final String FTP_HOST= "**********";

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

    /*********  FTP PASSWORD ***********/
    static final String FTP_PASS  ="********";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.buttonUploadPicture);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File f = new File(imgDecodableString);
                String name=f.getName();
                // Upload sdcard file
                uploadFile(f);
            }
        });
    }

//    public void onClick(View v){
//
////        Bundle b=new Bundle();
////        Intent i=new Intent(getApplication(), DetailTransActivity.class);
////        Toast.makeText(this, "Upload Bukti Transaksi Berhasil",
////                Toast.LENGTH_LONG).show();
////        i.putExtras(b);
////        startActivity(i);
//
//    }

    public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data


                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filename = cursor.getString(columnIndex);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();

                File f= new File(""+imgDecodableString);
                f.getName();
                ImageView imgView = (ImageView) findViewById(R.id.imgView);
                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "Pilih Bukti Transaksi",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Upload Bukti Transaksi Gagal", Toast.LENGTH_LONG)
                    .show();
        }
    }

    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("/assets/");

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

        } catch (Exception e) {
            e.printStackTrace();
            try {
                client.disconnect(true);
                Toast.makeText(MainActivity.this,"disconnect",Toast.LENGTH_LONG).show();
            } catch (Exception e2) {
                e2.printStackTrace();
                Toast.makeText(MainActivity.this,"failed",Toast.LENGTH_LONG).show();
            }
        }

    }

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

    public class MyTransferListener implements FTPDataTransferListener {

        public void started() {

            btn.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() {

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

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

        public void aborted() {

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

        public void failed() {

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

    }
}

堆栈跟踪

05-08 23:12:51.465 17345-17348/com.amobi.imageuploading D/dalvikvm: GC_CONCURRENT freed 470K, 4% free 15281K/15776K, paused 3ms+1ms, total 14ms
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err: android.os.NetworkOnMainThreadException
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.net.InetAddress.getByName(InetAddress.java:289)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.net.InetSocketAddress.<init>(InetSocketAddress.java:105)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.net.InetSocketAddress.<init>(InetSocketAddress.java:90)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at it.sauronsoftware.ftp4j.FTPConnector.tcpConnectForCommunicationChannel(FTPConnector.java:208)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at it.sauronsoftware.ftp4j.connectors.DirectConnector.connectForCommunicationChannel(DirectConnector.java:39)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1036)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.amobi.imageuploading.MainActivity.uploadFile(MainActivity.java:116)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.amobi.imageuploading.MainActivity$1.onClick(MainActivity.java:44)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.view.View.performClick(View.java:4204)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.view.View$PerformClick.run(View.java:17355)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.os.Handler.handleCallback(Handler.java:725)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.os.Looper.loop(Looper.java:137)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at dalvik.system.NativeStart.main(Native Method)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err: java.lang.IllegalStateException: Client not connected
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at it.sauronsoftware.ftp4j.FTPClient.disconnect(FTPClient.java:1123)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.amobi.imageuploading.MainActivity.uploadFile(MainActivity.java:126)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.amobi.imageuploading.MainActivity$1.onClick(MainActivity.java:44)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.view.View.performClick(View.java:4204)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.view.View$PerformClick.run(View.java:17355)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.os.Handler.handleCallback(Handler.java:725)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.os.Looper.loop(Looper.java:137)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 23:15:29.771 17345-17345/com.amobi.imageuploading W/System.err:     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案