簡體   English   中英

無法寫入連接到URLConnection的BufferedOutputStream

[英]Can't write to BufferedOutputStream that is connected to URLConnection

我正在嘗試使用URLConnection將文件上傳到FTP服務器。 沒有例外。 我似乎能夠連接到服務器。 如果我“手動”(使用FTP客戶端)上傳文件,我可以毫無問題地下載文件(程序的另一個安靜)。

我使用BufferedOutputStream.write(int)。 日志顯示我可以讀取本地文件(包含“Hej hej”)並訪問其中的字節。 問題是:沒有任何內容寫入服務器文件。 當我在服務器上讀取文件時,它仍然包含預上載內容。 該文件未更新。 我嘗試過BufferedOutputStream.write(String.getBytes()),但這沒有幫助。

static public void upload(String string, String file) {
    File file_ = new File(Environment.getExternalStorageDirectory() + "/Android/data/org.sionlinkoping.pingstprayer/files/thanks.txt");
try {
    upload("ftphome.mah.se", user, passwd, "thanks.txt", file_);
}
catch(Exception e) { Log.d("",e.toString()); }
}


static public void upload( String ftpServer, String user, String password,
        String fileName, File source ) throws MalformedURLException,
    IOException
{
    if (ftpServer != null && fileName != null && source != null)
{
    StringBuffer sb = new StringBuffer( "ftp://" );
    // check for authentication else assume its anonymous access.
    if (user != null && password != null)
    {
        sb.append( user );
        sb.append( ':' );
        sb.append( password );
        sb.append( '@' );
    }
    sb.append( ftpServer );
    sb.append( '/' );
    sb.append( fileName );

        BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try
        {
        URL url = new URL( sb.toString() );
        URLConnection urlc = url.openConnection();

            bos = new BufferedOutputStream( urlc.getOutputStream() );
        bis = new BufferedInputStream( new FileInputStream( source ) );

            int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) != -1)
        {
            Log.d("","i:"+i);
            bos.write( i );
        }
        bos.flush();
    }
    finally
    {
        if (bis != null)
            try
            {
                bis.close();
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
        if (bos != null)
            try
            {
               bos.close();
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
    }
    }
else
{
    System.out.println( "Input not available." );
}
}

日志顯示:

01-07 14:00:50.662: DEBUG/(6925): i:239
01-07 14:00:50.662: DEBUG/(6925): i:187
01-07 14:00:50.662: DEBUG/(6925): i:191
01-07 14:00:50.662: DEBUG/(6925): i:72
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.662: DEBUG/(6925): i:32
01-07 14:00:50.662: DEBUG/(6925): i:104
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.672: DEBUG/(6925): i:10

問題在於您假設寫入FTP URL將導致文件保存到服務器。 它不會。 結果只是未定義 - 它不起作用。

有FTP庫可用; 你需要使用一個。 例如,請看這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM