上传文件ftp client commons net ftp 3.6 / 3.1 / 2.2

时间:2018-04-18 12:39:52

标签: java android ftp

嗨,这是我的第一篇帖子,对不起我的基本英语,我对此操作有问题,我尝试将文件从手机传输到特定目录中的服务器HTTP。我必须进行存储/下载,因为另一个应用程序将文件保存在此目录中,我无法修改此路径。

package com.example.mufas.bookonline;`

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.SocketException;

import es.dmoral.toasty.Toasty;

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

import static android.content.ContentValues.TAG;


public class MainActivity extends Activity {

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

    public void deleteFile(View view) {


        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS);

        File file = new File(path, "esempio.txt");

        if (path.exists()) {
            System.out.println("Il file " + path + " esiste");
            file.delete();
            System.out.println("Il file " + path + " è stato eliminato");
            Toasty.success(getApplication(), "File elimininato", Toast.LENGTH_LONG, true).show();
        }
        // Make sure the Pictures directory exists.
        path.mkdirs();

    }

    public void newFile(View view) {

//          String path = "/Pictures/html.txt";

        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS);

        try {
//         File file = new File(getFilesDir(),path);
            File file = new File(path, "esempio.txt");
            Toasty.success(getApplication(), "File Creato", Toast.LENGTH_LONG, true).show();
            if (file.exists()) {
                Toasty.success(getApplication(), "Il file è gia stato creato", Toast.LENGTH_LONG, true).show();
                System.out.println("Il file " + path + " esiste");
            } else if (file.createNewFile()) {
                System.out.println("Il file " + path + " è stato creato");
            } else {
                System.out.println("Il file " + path + " non può essere creato");
            }

            // Make sure the Download directory exists.
            path.mkdirs();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void trasferisciFile(View view) {

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {

            FTPClient mFTP = new FTPClient();


          File path = Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_DOWNLOADS);

 //               File path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

                        // Connect to FTP Server
                        mFTP.connect("<host>", 21);
                        mFTP.login("<user>", "<password>");
                        //mFTP.setFileType(FTP.BINARY_FILE_TYPE);
                        mFTP.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
                        mFTP.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                        mFTP.enterLocalPassiveMode();


                        // Prepare file to be uploaded to FTP Server

                        File file = new File(path, "esempio.txt");

//                    System.out.println(" sono pronto a trasferire da questo percorso" + path + file);

                        FileInputStream ifile = new FileInputStream(file);

                    // Upload file to FTP Server
                    mFTP.storeFile("online", ifile);

                    mFTP.disconnect();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toasty.success(getApplication(), "Il file è stato trasferito", Toast.LENGTH_LONG, true).show();
                        }
                    });

                } catch (SocketException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        thread.start();


    }

} // fine classe main 

这是我的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mufas.bookonline">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:isolatedProcess="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在logat我没有错误,应用程序重新响应我&#34;转移成功&#34;我正在使用Android Studio和设备是Smasung galaxy tab 3 android 6.0我做错了什么?提前致谢

1 个答案:

答案 0 :(得分:0)

很抱歉,但我发了帖子,现在我无法编辑它。问题是远程目录中没有文件到进程结束我尝试在手机中安装应用程序但没有结果。对不起重复的帖子