使用simpleFTP上传到FTP失败

时间:2016-06-20 15:34:18

标签: java android ftp runtime-error

我想使用简单的FTP将图像上传到fto。但它总是无法上传到FTP。该错误始终指向ftp.connect。我不知道为什么。所以我希望你能帮助我。请。提前致谢。 Upload.java

public class ImageUpdate extends AppCompatActivity {
        private static final String TAG_ID = "id";
        private static final String TAG_PESAN = "message";
        private static final String TAG_HASIL = "result";
        private static final String TAG_IMAGE_ID = "id_image";
        private static final String TAG_IMAGE_NAME= "image_name";
        ProgressDialog pDialog;

        JSONParser jparser = new JSONParser();
        ArrayList<HashMap<String, String>> namelist, idList, imageList;
        JSONArray names, names1, names2;

        private static int RESULT_LOAD_IMG = 1;
        String imgDecodableString = null;
        Button submit;


        static final String FTP_HOST = "xxxxxxxxxx";
        static final String FTP_USER = "xxxxxxxxxxxx";
        static final String FTP_PASS = "xxxxxxxxxxx";
        String name, vid;
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        private GoogleApiClient client2;

        SessionManagement session;
        String nm,addr,pos,tlp,mail,usr,pass,id,image;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_image_update);

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            client2 = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
            session =new SessionManagement(ImageUpdate.this);
            HashMap<String, String> user = session.getUserDetails();
            id=user.get(SessionManagement.KEY_ID);
            nm=user.get(SessionManagement.KEY_NAME);
            addr=user.get(SessionManagement.KEY_ALAMAT);
            mail=user.get(SessionManagement.KEY_EMAIL);
            tlp=user.get(SessionManagement.KEY_TELP);
            usr=user.get(SessionManagement.KEY_USERNAME);
            pass=user.get(SessionManagement.KEY_PASS);
            submit=(Button) findViewById(R.id.buttonUploadPicture);
            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (imgDecodableString == null) {
                        Toast.makeText(ImageUpdate.this, "Choose image first, please", Toast.LENGTH_LONG);
                    } else {
                        File f = new File(imgDecodableString);
                        name = f.getName();
                        uploadFile(f);
                    }
                }
            });

        }
        public void loadImagefromGallery(View view) {
            // Create intent to Open Image applications like Gallery, Google Photos
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                    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, "Failed to Choose", Toast.LENGTH_LONG)
                        .show();
            }
        }

        public void uploadFile(File fileName) {

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            SimpleFTP ftp=new SimpleFTP();

            try {
                ftp.connect("xxxxxxx", 21, "xxxxxxxx", "xxxxxxx");
                ftp.bin();
                ftp.cwd("img/imageProfil/");
                ftp.stor(fileName);
                ftp.disconnect();

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

        }

        @Override
        public void onStart() {
            super.onStart();

            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client2.connect();
            Action viewAction = Action.newAction(
                    Action.TYPE_VIEW, // TODO: choose an action type.
                    "Upload Page", // TODO: Define a title for the content shown.
                    // TODO: If you have web page content that matches this app activity's content,
                    // make sure this auto-generated web page URL is correct.
                    // Otherwise, set the URL to null.
                    Uri.parse("http://host/path"),
                    // TODO: Make sure this auto-generated app deep link URI is correct.
                    Uri.parse("android-app://com.amobi.newlomapodfix/http/host/path")
            );
            AppIndex.AppIndexApi.start(client2, viewAction);
        }

        @Override
        public void onStop() {
            super.onStop();

            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            Action viewAction = Action.newAction(
                    Action.TYPE_VIEW, // TODO: choose an action type.
                    "Upload Page", // TODO: Define a title for the content shown.
                    // TODO: If you have web page content that matches this app activity's content,
                    // make sure this auto-generated web page URL is correct.
                    // Otherwise, set the URL to null.
                    Uri.parse("http://host/path"),
                    // TODO: Make sure this auto-generated app deep link URI is correct.
                    Uri.parse("android-app://com.amobi.newlomapodfix/http/host/path")
            );
            AppIndex.AppIndexApi.end(client2, viewAction);
            client2.disconnect();
        }
    }

栈跟踪

06-20 22:32:00.692 2845-2845/com.amobi.newlomapodfix W/EGL_emulation: eglSurfaceAttrib not implemented
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err: java.io.IOException: SimpleFTP received an unknown response when connecting to the FTP server: 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:74)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at com.amobi.newlomapodfix.UploadActivity.uploadFile(UploadActivity.java:167)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at com.amobi.newlomapodfix.UploadActivity$1.onClick(UploadActivity.java:100)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at android.view.View.performClick(View.java:4204)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at android.view.View$PerformClick.run(View.java:17355)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at android.os.Handler.handleCallback(Handler.java:725)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at android.os.Looper.loop(Looper.java:137)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 22:32:04.900 2845-2845/com.amobi.newlomapodfix W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 22:32:04.904 2845-2845/com.amobi.newlomapodfix W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 22:32:04.908 2845-2845/com.amobi.newlomapodfix W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 22:32:04.912 2845-2845/com.amobi.newlomapodfix W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 22:32:04.912 2845-2845/com.amobi.newlomapodfix W/System.err:     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

这个答案解释了简单FTP中的不合格FTP规范,实际上,服务器应该从220开始,但是这个库会出现异常。 (https://stackoverflow.com/a/24386510/6093353)。

本教程实现了一个简单的FTP上传,尝试遵循这一点 http://androidexample.com/FTP_File_Upload_From_Sdcard_to_server/index.php?view=article_discription&aid=98