连接到FTP服务器时出现未知主机异常

时间:2018-10-15 06:38:23

标签: android ftp-client

我已经在我的应用程序上实现了FTP客户端,并尝试连接到现有的FTP服务器,但是不知道为什么会引发错误。

我正在 build.gradle

中使用此库
implementationfiles('libs/apache-commons-net.jar')

这是我的FTP客户端连接部分的代码

 private void configFTPClient(String user, String password)
    {

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


        FTPClient ftpClient = new FTPClient();

       //final String serverFinal=server;
        BufferedInputStream buffIn = null;

        String fileNameDirectory="BPL_CARDIART";

        File file =new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(),fileNameDirectory);
        String firstRemoteFile =file+"/"+ "Cardiart__LPP1_ECG.pdf";

        try {
            buffIn = new BufferedInputStream(new FileInputStream(firstRemoteFile));

            ftpClient.connect(server,port);
            ftpClient.login(user, password);
           // ftpClient.changeWorkingDirectory(serverRoad);

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.storeFile("test.txt", buffIn);
            buffIn.close();
            ftpClient.logout();
            ftpClient.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

我的maifest.xml文件是代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cardiact.bpl.pkg.com.bplcardiactconnect"

    >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        />
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <uses-feature android:name="android.hardware.usb.host" />
    <uses-feature android:name="android.hardware.camera2" android:required="false"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:fullBackupContent="false"
        android:label="@string/app_name"
        android:hardwareAccelerated="true"
        android:name="application.BaseApplicationClass"
        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".SplashScreenActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name=".BaseLoginActivityClass"
            android:launchMode="singleInstance"
            android:configChanges="orientation|keyboardHidden|screenSize"

            />

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

正如我已经检查过的那样,手机上的wifi和Internet连接正常,但仍然会引发错误

java.net.UnknownHostException: Unable to resolve host "ftp://14.141.84.241/": No address associated with hostname
2018-10-15 11:58:40.669 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:141)
2018-10-15 11:58:40.669 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:787)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err:     at java.net.Socket.<init>(Socket.java:217)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err:     at org.apache.commons.net.DefaultSocketFactory.createSocket(DefaultSocketFactory.java:53)
2018-10-15 11:58:40.670 2407-2407/cardiact.bpl.com.bplcardiactconnect W/System.err:     at org.apache.commons.net.SocketClient.connect(SocketClient.java:162)

0 个答案:

没有答案