URLConnection不适用于Android中的公共FTP服务器

时间:2016-08-24 15:52:09

标签: java android url ftp urlconnection

我的问题:我的Android设备无法使用URLConnection连接到公共FTP服务器。但是,当设备尝试连接到公共FTP服务器(anonymous用于用户名而无密码)时,它无法从urlConnection.getInputStream();检索InputStream。

我不使用FTPClient库中的Apache Commons,因为速度非常有限...有关FTPClient速度有限的相关问题在这里 FTPClient's download is slower than URLConnection's download
无论我尝试使用urlString:

urlString = protocol+"://" + username+"@" +address+":"+port + filepath + file;
// ftp://anonymous@speedtest.tele2.net:21/100GB.zip
urlString = protocol+"://" + username+":" + password + "@" +address+":"+port + filepath + file;
// ftp://anonymous:@speedtest.tele2.net:21/100GB.zip
urlString = protocol+"://" + username+":" + "password" + "@" +address+":"+port + filepath + file;
// ftp://anonymous:password@speedtest.tele2.net:21/100GB.zip
urlString = protocol+"://" + address+":"+port + filepath + file;
// ftp://speedtest.tele2.net:21/100GB.zip
使用开放的FTP服务器永远不会记录

downloadAndBroadcastURL:after getinputStream。我也试过这两个公共FTP服务器:

ftp://speedtest.tele2.net/100GB.zip
ftp://ftp.funet.fi/dev/1GBnull

但它也没有连接。

我的代码:

private void downloadAndBroadcastURL() {
    beginTime = System.currentTimeMillis();
    try {
        String urlString;
        if (username.equals("anonymous")){
            urlString = protocol+"://" + username+"@" +address+":"+port + filepath + file;
        } else {
            urlString = protocol+"://" + username+":" + password + "@" +address+":"+port + filepath + file;
        }
        Log.d(TAG, "downloadAndBroadcastURL: "+ urlString);
        URL url = new URL(urlString);
        URLConnection urlConnection = url.openConnection();
        Log.d(TAG, "downloadAndBroadcastURL:after connect ");
        inputStream = new BufferedInputStream(urlConnection.getInputStream());
        Log.d(TAG, "downloadAndBroadcastURL:after getinputStream");

        /*******************************
          The rest is about handling the input stream, the download...
         *****************************/

        long difference;
        byte data[] = new byte[4094];
        int count;
        while ((count = inputStream.read(data)) != -1 && download) {
            downloadCount += count;
            long stopTime = System.currentTimeMillis();
            difference = stopTime - beginTime;
            if (difference > 1000 && download) {
                currentSpeed = downloadCount / (difference / 1000L); //for precision, because difference can be more than 1000 sometimes
                averageSpeed = (averageSpeed + currentSpeed) / 2;
                Log.d(TAG, "Speed: " + downloadCount + " bytes/s. " + (downloadCount / 1024) + "kB/s " + (downloadCount / 128) + "kbit/s " + (downloadCount / 1048576) + "Mb/s " + ((downloadCount) / (1024 * 128)) + "Mpbs" + (downloadCount / 131072) + "Mbit/s difference:" + difference + "ms");
                if (download) {
                    broadcastSpeed();
                }
                downloadCount = 0; //Resetoidaan määrä
                beginTime = stopTime;
            }
            if (!download) {
                Log.d(TAG, "downloadAndBroadcast: download stops");
                clearInputStream();
            }
        }
        clearInputStream();
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "FAIL " + e.toString());
    } finally {
        Log.d(TAG, "downloadAndBroadcastURL: ");
        clearInputStream();
        Log.d(TAG, "downloadAndBroadcast: finally");
    }
}

有没有人有任何想法?提前谢谢!

0 个答案:

没有答案